#!/bin/bash

LC_COLLATE="C"
progname="${0##*/}"

warn ()
{
    [ "$1" ] && echo >&2
    [ "$1" ] && echo "$progname: $1" >&2
    [ "$1" ] && echo >&2
}

fatal ()
{
    warn "$2"
    exit "$1"
}

usage ()
{
    cat <<EOF
Usage: $progname <lustretag> <pindate>
  where <lustretag> is a tag of the lustre-core module
  and <pindate> is an optional quoted timestamp suitable for cvs -D
EOF
}

if [ -z "$LUSTRECVS_UPDATED" ] ; then
    echo "$progname: updating lustrecvs"

    # If checking out a specific tag, make sure all of the files here are also
    # checked out with the same tag to avoid later changes breaking things.
    case "$1" in
    v*|b_release_*) TAG="-r $1" ;;
    esac

    cvs update -l $TAG
    export LUSTRECVS_UPDATED=yes
    exec "$0" "$@"
fi

[ "$1" = "-r" ] && shift

buildtag="HEAD"
lustretag="$1"
shift
pindate=$1
shift

if [ "$*" ] ; then
    usage >&2
    exit 1
fi

case "$lustretag" in
    '')
        warn "a lustretag is required."
	usage >&2
	exit 1
	;;
    --help | -h)
	usage
	exit 0
	;;

    # this is the branch table
    # keep this list sorted alphabetically!

    # These use special build directories 

    b1_4*) buildtag="b1_4" ;;

    b_release_1_4_6-patchless) buildtag="b1_4" ;;
    b_release_1_4_7-test) buildtag="b_release_1_4_7" ;;

    b_release*) buildtag=$lustretag ;;

    b_uoss) buildtag=$lustretag ;;

    # These releases did not get build tagged for them because they
    # this build system didn't exist when they were tagged
    v1_2_8|v1_4_0) 
        buildtag="b1_4"
        ;;
    
    v*) buildtag=$lustretag ;;

    # this is the branch table
    # keep this list sorted alphabetically!

    *_gate) buildtag="b_build_gate" ;;

    *)
        buildtag="HEAD"
	;;
esac

error_modules=
cvs_cmd ()
{
    dir="$1"
    module="$2"
    tag="$3"
    cotag=""
    update=""

    if [ "$tag" = "HEAD" ] ; then
        cotag=""
        uptag="-A"
    elif [ "$tag" ] ; then
        cotag="-r $tag"
        uptag="-r $tag"
    else
        # silently skip if no tag was specified
        return
    fi

    # create a cvs date format that will survive shell expansion
    if [ -n "$pindate" ]; then
        datecmd=$(date -u +%s -d "$pindate")
        datecmd="-D @$datecmd"
    else
        datecmd=""
    fi

    if [ -d "$dir" ] ; then
        echo "$progname: Updating $dir to $tag"
        ( cd "$dir" && cvs up $datecmd -dAP $uptag )
    else
        echo "$progname: Checking out $dir from $tag"
        cvs co $datecmd -P $cotag -d "$dir" "$module"
    fi
    if [ $? != 0 ] ; then
        error_modules="$dir $error_modules"
    fi
}

hg_cmd ()
{
    dir="$1"
    base_url="$2"
    repository="$3"

    if [ ! "$repository" ]; then
        return
    fi

    if ! which hg &> /dev/null; then
    cat <<EOF

Error: Mercurial is missing, try 'yum install mercurial', 'apt-get install
mercurial' or try http://rpmfind.net/linux/rpm2html/search.php?query=mercurial
EOF
        error_modules="$dir $error_modules"
        return
    fi

    url="$base_url/$repository"

    # create a cvs date format that will survive shell expansion
    if [ -n "$pindate" ]; then
        datecmd=$(date -u +%s -d "$pindate")
        datecmd="-d \"$datecmd 0\""
    else
        datecmd=""
    fi

    if [ -d "$dir" ]; then
        echo "$progname: Updating $dir"
        if [ -f "$dir/update.sh" ]; then
            ( cd "$dir" && ./update.sh $datecmd )
        else
            ( cd "$dir" && hg pull && hg update $datecmd )
        fi
    else
        echo "$progname: Checking out $dir"
        hg clone $url $dir
        if [ -f "$dir/setup.sh" ]; then
            ( cd "$dir" && ./setup.sh $datecmd )
        else
            ( cd "$dir" && hg update $datecmd )
        fi
    fi

    if [ $? != 0 ] ; then
        error_modules="$dir $error_modules"
    fi
}

cvs_cmd build lustre-build "$buildtag"

if [ -f build/buildcvs ] ; then
    . build/buildcvs
else
    fatal 1 "build/buildcvs does not exist; not updating other modules."
fi

if [ "$error_modules" ] ; then
    fatal 1 "There were errors checking out the following directories: $error_modules"
fi