#
#	aegis - project change supervisor
#	Copyright (C) 2004 Peter Miller;
#	All rights reserved.
#
#	As a specific exception to the GPL, you are allowed to copy
#	this source file into your own project and modify it, without
#	releasing your project under the GPL, unless there is some other
#	file or condition which would require it.
#
# MANIFEST: shell script to commit changes to CVS
#
# It is assumed that your CVSROOT and CVS_RSH environment variables have
# already been set appropriately.
#
# This script is expected to be run as by integrate_pass_notify_command
# and as such the baseline has already assumed the shape asked for by
# the change.
#
#	integrate_pass_notify_command =
#    	    "$bin/ae-cvs-ci $project $change";
#
# Alternatively, you may wish to tailor this script to the individual
# needs of your project.  Make it a source file, e.g. "etc/ae-cvs-ci.sh"
# and then use the following:
#
#	integrate_pass_notify_command =
#    	    "$sh ${s etc/ae-cvs-ci} $project $change";
#

USAGE="Usage: $0 <project> <change>"

PRINT="echo"
EXECUTE="eval"

while getopts "hnq" FLAG
do
    case ${FLAG} in
    h )
        echo "${USAGE}"
        exit 0
        ;;
    n )
        EXECUTE=":"
        ;;
    q )
        PRINT=":"
        ;;
    * )
        echo "$0: unknown option ${FLAG}" >&2
        exit 1
        ;;
    esac
done

shift `expr ${OPTIND} - 1`

case $# in
2)
    project=$1
    change=$2
    ;;
*)
    echo "${USAGE}" 1>&2
    exit 1
    ;;
esac

here=`pwd`

AEGIS_PROJECT=$project
export AEGIS_PROJECT
AEGIS_CHANGE=$change
export AEGIS_CHANGE

module=`echo $project | sed 's|[.].*||'`

baseline=`aegis -cd -bl`

if test X${TMPDIR} = X; then TMPDIR=/var/tmp; fi

TMP=${TMPDIR}/ae-cvs-ci.$$
mkdir ${TMP}
cd ${TMP}

PWD=`pwd`
if test X${PWD} != X${TMP}; then
    echo "$0: ended up in ${PWD}, not ${TMP}" >&2
    exit 1
fi

fail()
{
    set +x
    cd $here
    rm -rf ${TMP}
    echo "FAILED" 1>&2
    exit 1
}
trap "fail" 1 2 3 15

Command()
{
    ${PRINT} "$*"
    ${EXECUTE} "$*"
}

#
# Create a new CVS work area.
#
# Note: this assumes the module is checked-out into a directory of the
# same name.  Is there a way to ask CVS where is is going to put a
# modules, so we can always get the "cd" right?
#
${PRINT} cvs co $module
${EXECUTE} cvs co $module > LOG 2>&1
if test $? -ne 0; then cat LOG; fail; fi
${EXECUTE} cd $module

#
# Now we need to extract the sources from Aegis and drop them into the
# CVS work area.  There are two ways to do this.
#
# The first way is to use the generated tarball.
# This has the advantage that it has the Makefile.in file in it, and
# will work immediately.
#
# The second way is to use aetar, which will give exact sources, and
# omit all derived files.  This will *not* include the Makefile.in,
# and so will not be readily compilable.
#
# gunzip < $baseline/export/${project}.tar.gz | tardy -rp ${project} | tar xf -
aetar -send -comp-alg=gzip -o - | tar xzf -

#
# If any new directories have been created we will need to add them
# to CVS before we can add the new files which we know are in them,
# or they would not have been created.  Do this only if the -n option
# isn't used, because if it is, we won't have actually checked out the
# source and we'd erroneously report that all of them need to be added.
#
if test "X${EXECUTE}" != "X:"
then
    find . \( -name CVS -o -name Attic \) -prune -o -type d -print |
    xargs --max-args=1 |
    while read dir
    do
        if [ ! -d "$dir/CVS" ]
        then
	    Command cvs add "$dir"
        fi
    done
fi

#
# Use the Aegis meta-data to perform some CVS commands that CVS can't
# figure out for itself.
#
aegis -l cf -unf | sed 's| -> [0-9][0-9.]*||' |
while read usage action rev filename
do
    if test "x$filename" = "x"
    then
        filename="$rev"
    fi
    case $action in
    create)
	Command cvs add $filename
	;;
    remove)
	Command rm -f $filename
	Command cvs remove $filename
	;;
    *)
	;;
    esac
done

#
# Extract the brief description.  We'd like to do this using aesub
# or something, like so:
#
#      message=`aesub '${version} - ${change description}'`
#
# but the expansion of ${change description} has a lame hard-coded max of
# 80 characters, so we have to do this by hand.  (This has the slight
# benefit of preserving backslashes in front of any double-quotes in
# the text; that will have to be handled if we go back to using aesub.)
#
description=`aegis -ca -l | sed -n 's/brief_description = "\(.*\)";$/\1/p'`
version=`aesub '${version}'`
message="$version - $description"

#
# Now commit all the changes.
#
Command cvs -q commit -m \"$message\"

#
# All done.  Clean up and go home.
#
cd $here
rm -rf ${TMP}
exit 0