#!/usr/bin/bash
#
# Create a new development branch for a package.

umask 0002

# Figure out the environment we're running in
RUNDIR=$(cd "$(dirname "$0")" && pwd)

eval "$(crudini --format=sh --get /etc/dist-git/dist-git.conf dist-git)"
REPODIR=$gitroot_dir
SRC_BRANCH="$default_branch"

# check if a moron is driving me
if [ ! -d $REPODIR ] ; then
    # we're not on the git server (this check is fragile)
    echo "ERROR: This script has to be run on the git server."
    echo "ERROR: Homer sez 'Duh'."
    exit -9
fi

# Local variables
VERBOSE=0
TEST=
IGNORE=
BRANCH=""
PACKAGES=""

Usage() {
    cat <<EOF
Usage:
    $0 [ -s <src_branch>] <branch> <package_name>...

    Creates a new branch <branch> for the list of <package_name>s.
    The /$SRC_BRANCH suffix on branch names is assumed.

Options:
    -n,--test           Don't do nothing, only test
    -i,--ignore         Ignore erroneous modules
    -h,--help           This help message
    -v,--verbose        Increase verbosity
EOF
}

# parse the arguments
while [ -n "$1" ] ; do
    case "$1" in
    -h | --help )
        Usage
        exit 0
        ;;

    -v | --verbose )
        VERBOSE=$(($VERBOSE + 1))
        ;;

    -i | --ignore )
        IGNORE="yes"
        ;;

    -n | --test )
        TEST="yes"
        ;;

    -b | --branch )
        shift
        BRANCH=$1/$SRC_BRANCH
        ;;

    * )
        if [ -z "$BRANCH" ] ; then
        BRANCH="$1"
        else
        PACKAGES="$PACKAGES $1"
        fi
        ;;
    esac
    shift
done

# check the arguments
if [ -z "$BRANCH" -o -z "$PACKAGES" ] ; then
    Usage
    exit -1
fi

# prepend default namespace if set
NEWP=
for PACKAGE in $PACKAGES ; do
    PACKAGE=`echo $PACKAGE | sed -e "s+^/*\([^/]*\)/*$+\1+"`
    parts=($(echo $PACKAGE | tr "/" " "))
    parts_len=${#parts[@]}
    if [ -n "$default_namespace" ] && [ $parts_len -le 1 ]; then
        PACKAGE=$default_namespace/$PACKAGE
    fi
    NEWP="$NEWP $PACKAGE"
done
PACKAGES="$(echo $NEWP)"

# Sanity checks before we start doing damage
NEWP=
for p in $PACKAGES ; do
    [ $VERBOSE -gt 1 ] && echo "Checking package $p..."
    if [ ! -d $REPODIR/$p.git ] ; then
    echo "ERROR: Package module $p is invalid" >&2
    [ "$IGNORE" = "yes" ] && continue || exit -1
    fi
    if GIT_DIR=$REPODIR/$p.git git rev-parse -q --verify $BRANCH >/dev/null; then
        echo "IGNORING: Package module $p already has a branch $BRANCH" >&2;
        [ "$IGNORE" = "yes" ] && continue || exit 128
    fi
    NEWP="$NEWP $p"
done
PACKAGES="$(echo $NEWP)"

if [ -z "$PACKAGES" ] ; then
    echo "NOOP: no valid packages found to process"
    exit -1
fi

if [ -n "$TEST" ] ; then
    echo "Branch $BRANCH valid for $PACKAGES"
    exit 0
fi

# "global" permissions check
if [ ! -w $REPODIR ] ; then
    echo "ERROR: You can not write to $REPODIR"
    echo "ERROR: You can not perform branching operations"
    exit -1
fi

# Now start working on creating those branches

# For every module, "create" the branch
for NAME in $PACKAGES ; do
    echo
    echo "Creating new module branch '$BRANCH' for '$NAME'..."

    # permissions checks for this particular module
    if [ ! -w $REPODIR/$NAME.git/refs/heads/  ] ; then
        echo "ERROR: You can not write to $d"
        echo "ERROR: $NAME can not be branched by you"
        continue
    fi
    [ $VERBOSE -gt 0 ] && echo "Creating $NAME $BRANCH from $NAME ..."
    $(pushd $REPODIR/$NAME.git >/dev/null && \
    git branch --no-track $BRANCH `git rev-list "$SRC_BRANCH" | head -1` && \
    popd >/dev/null) || {
    echo "ERROR: Branch $NAME $BRANCH could not be created" >&2
        popd >/dev/null
    exit -2
    }
    if [[ $grok && $grok != "False" ]]; then
        /usr/bin/grok-manifest -m $REPODIR/manifest.js.gz -t $REPODIR -n $REPODIR/$NAME.git
    fi
done

echo
echo "Done."
