#!/usr/bin/bash
# gitpkg common shell functions and variables

# DISTRO should go to some config file
DISTRO=${DISTRO:-mer}

pkg_branch=pkg-$DISTRO

pkg_dir=${PKG_DIR:-rpm/}

ME=$(basename $0)

# Function for verbose output
log(){
    [[ "x$_V" == "x1" ]] && echo "$@"
}

get_pkgfile(){
    # Find the yaml or spec (or abort)
    # Use the environment PKG_FILE if provided
    pkgfile=${PKG_FILE:-$(find $pkg_dir -maxdepth 1 -name '*yaml' -print -quit)}
    if [[ ! $pkgfile ]]; then
	pkgfile=$(find $pkg_dir -maxdepth 1 -name '*spec' -print -quit)
    fi
    pkgfile=$(readlink -f $pkgfile)
}

# Input: field name
# Needs: $pkgfile
# Output: field value
get_field_from_packaging() {
    [[ -f $pkgfile ]] && {
	grep "${1}:" $pkgfile | sed -e '/^#/d' -e "s/\s*$1:\s*//i" -e 's/\s*$//g' | head -1
    }
}

# Input: 
# Output: 
# http://stackoverflow.com/a/3879077/337649
count_commits() {
    git rev-list $1..$2 -- | wc -l
}

# Input: branch name
# Output: returns success/fail 
git_branch_exists() { git show-ref --quiet --verify -- "refs/heads/$1"; }

git_tag_cleanly() {
    tag=$1
    old=$(git rev-list -1 $1 2>/dev/null || echo none)
    git tag -f $@
    tag_cleanup_list+=" $1:$old"
}

cleanup(){
    # If there is no .git directory there is nothing to cleanup.
    [[ -d .git ]] || return

    if [[ $tag_cleanup_list ]] ; then
	echo "cleaning up tags: $tag_cleanup_list"
	for tagpair in $tag_cleanup_list; do
	    eval IFS=: read tag sha1 <<< $tagpair
	    if [[ $sha1 == "none" ]]; then
		git tag -d $tag
	    else
		git tag -f $tag $sha1
	    fi
	done
    fi
    if [[ $branch_cleanup_list ]]; then
	echo "cleaning up branches: $branch_cleanup_list"
	for branch in $branch_cleanup_list; do
	    git branch -D $branch
	done
    fi
}

# If the first arg is 'usage' then usage() is called right before exit
fatal() {
    local usage=
    if [[ $1 == "usage" ]]; then
	shift
	usage=1
    fi
    if [[ $1 ]]; then
	echo "ERROR: $@"
    else # support fatal << EOF
	cat
    fi
    [[ $usage ]] && usage
    exit 1
}

# Setup rpm dir if not present
# optionally supply a branch to checkout to rpm/
ensure_rpm_subdir() {

    local rel_git_dir=../../.git
    local branch=$1

    mkdir -p "$pkg_dir/.git" || fatal "unable to create git repo in \"$pkg_dir\"!"

    [[ -e $pkg_dir/.git/main_git_repo ]] && return 0 # Already setup
    [[ -e $pkg_dir/.git/refs ]] && fatal "$pkg_dir/.git is a git dir but not a packaging tracking directory. Aborting."

    # From https://github.com/cypher/dotfiles/blob/master/bin/git-new-workdir
    #
    # create the links to the original repo. explicitly exclude index,
    # HEAD and logs/HEAD from the list since they are purely related
    # to the current working directory, and should not be shared.
    log "Setting up $pkg_dir as a packaging tracking directory"
    ln -s $rel_git_dir $pkg_dir/.git/main_git_repo
    for x in config refs logs/refs objects info hooks packed-refs remotes rr-cache svn; do
	case $x in
	    */*)
		mkdir -p "$(dirname "$pkg_dir/.git/$x")"
		ln -s "../main_git_repo/$x" "$pkg_dir/.git/$x" ;;
	    *)
		ln -s "main_git_repo/$x" "$pkg_dir/.git/$x" ;;
	esac
    done

    # now put us in detached HEAD state and then force checkout pkg_dir/ to $branch
    if [[ $branch ]]; then (
	    cd $pkg_dir
	    echo 0000000000000000000000000000000000000000 > .git/HEAD
	    git checkout -f $branch 2>/dev/null
	)
    fi
}

export GIT_AUTHOR_EMAIL=${GIT_AUTHOR_EMAIL:-$(git config user.email)}
export GIT_AUTHOR_NAME=${GIT_AUTHOR_NAME:-$(git config user.name)}
