#!/bin/sh
#
# Does a complete automated build: checks out source, 
# builds on each system, and sends the final e-mail.
#
# Orion Sky Lawlor, olawlor@acm.org, 2002/2/3
#

source config

echo "Autobuild initiated" `date` by `id`

# Run this command or die
Do() {
	echo "autobuild> " $@
	"$@"
	err="$?"
	if [ $err -ne 0 ] 
	then
		echo "error code $err during autobuild> " $@ 
		exit 1
	fi
}

# wipe out processes
./kill_all

# Clean out old files
rm -fr $cvsModule* bin messages scripts status web
mkdir bin messages scripts status web
./update_web_start

# Do a clean cvs checkout:
export CVSROOT=$cvsRoot
echo "autobuild> Checking out fresh version from $CVSROOT $cvsModule"
Do cvs -d $CVSROOT co -P $cvsModule > /dev/null
# Overwrite CVS/Root files with path to stable nightly branch,
#  not development copy:
Do find $cvsModule -name "Root" -exec cp $HOME/Root '{}' ';'
Do tar cf $cvsModule.tar $cvsModule

Do ssh $cvsMachine << EOF
cd $cvsRootPath
rm -fr $cvsModule-nightly-test
cp -r $cvsModule $cvsModule-nightly-test
echo "autobuild> Remote cvs copy made."
EOF

echo "autobuild> Copying source to $webMachine"
Do cp $cvsModule.tar ${cvsModule}_src.tar
Do gzip ${cvsModule}_src.tar
Do scp ${cvsModule}_src.tar.gz $webMachine:$webMachBin
Do scp ${cvsModule}_src.tar.gz $webMachine:$webMachCur
Do rm ${cvsModule}_src*

Do ssh $webMachine << END_REMOTE
cd $webMachCur
uuencode ${cvsModule}_src.tar.gz ${cvsModule}_src.tar.gz > ${cvsModule}_src.uu
END_REMOTE

# Build on all systems (in parallel)
echo "autobuild> Building..."
./build_on_all

# After waiting four hours, send out results via e-mail
echo "Waiting to send e-mail:"
sleep `expr 3600 '*' 6`
./update_email
./update_cvs

