#!/bin/sh
#
# Given a version of charm++, look up and build that version
# on a remote machine listed in system_list.
# This script handles sending out files, ssh, etc.
# Orion Sky Lawlor, olawlor@acm.org, 2002/2/3
#

source config

########################## SETUP #########################
if [ $# -lt 1 ]
then
	echo "Usage: ./build_on_system <charm++ version>"
	exit 1
fi

# Charm++ version without spaces like "net-sol-cc-local"
version="$1"
# Charm++ version with spaces like "net-sol cc local"
versSpace=`echo $1 | awk -F- '{
	for (i=1;i<=NF;i++) {
		printf("%s",$i);
		if (i==NF)
			{}
		else if (i<2 || $(i+1) == "ia64" || $(i+1) == "darwin" || $(i+1) == "amd64")
			printf("-");
		else
			printf(" ");
	}
	printf("\n");
}'`

echo "Will build $version as $versSpace"

# Set up status:
status="status/$version"
messages="messages/$version"
scripts="scripts/$version"
echo "Building..." > $status
echo "Started build on" `date` > $messages
rm -fr $scripts
mkdir $scripts

# Print out this informative message:
Message() {
#	echo "$version: " $@
	echo $@ >> $messages
}

# Set the status to this
Status() {
	echo "$version status:" "$@"
	echo "$@" > $status
	Message $@
}

# Exit giving this reason for failure.
Die() {
	Message "fatal> " "$@" 
	if test -n "$errorString"
	then
		Status "$errorString"
	else
		Status "Bad:" "$@"
	fi
	./update_web
	exit 1
}

# Run this command without doing redirection
DoNoRedir() {
	Message "local> " "$@"
	"$@"
	err="$?"
	if [ $err -ne 0 ]
	then
		Die "error code $err during local> " "$@"
	fi
}

# Run this command or die
Do() {
	DoNoRedir "$@" >> $messages 2>&1
}


# Parse out our configuration file line
l=`grep ^$version":" system_list`
[ $? -eq 0 ] || Die "Unrecognized version $version"
buildMachine=`echo $l | awk -F: '{print $2}'`
sshVer=`echo $l | awk -F: '{print $3}'`
buildDir=`echo $l | awk -F: '{print $4}'`
buildPre=`echo $l | awk -F: '{print $5}'`
makeCmd=`echo $l | awk -F: '{print $6}'`
[ x"$makeCmd" = x ] && makeCmd=make
extraBuildOPT=`echo $l | awk -F: '{print $7}'`
extraCFlags=`echo $l | awk -F: '{print $8}'`
testPre=`echo $l | awk -F: '{print $9}'`

ssh="ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o CheckHostIP=no -o BatchMode=yes"
scp="scp -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o CheckHostIP=no -o BatchMode=yes"

if [ "$sshVer" != "" ] 
then
  ssh="$ssh -$sshVer"
  scp="$scp -oProtocol=$sshVer"
fi

Message "Building $version using $ssh $buildMachine:$buildDir"

######################## CREATE BUILD SCRIPTS ###########################

cat > $scripts/prefix << END_PREFIX
#!/bin/bash
# Standard remote job script header: automatically generated by build_on_system

echo "Ssh'd to $buildMachine on" \`date\`
$buildPre

# Exit, giving this reason for failure.
Die() {
	echo "fatal> \$@" 
	exit 1
}

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

Do cd $buildDir

END_PREFIX

cat > $scripts/clean << END_CLEAN
echo "remote> Cleaning out old build"
mkdir -p $buildDir
cd $buildDir || exit 1
rm -fr ${cvsModule} ${cvsModule}.tar ${cvsModule}*.tar
exit 0

END_CLEAN

cat > $scripts/build < $scripts/prefix 
cat >> $scripts/build << END_BUILD
# Main build sequence:
echo "remote hostname>"
hostname
echo "remote id>" 
id
echo "remote uptime>" 
uptime
echo "remote disk space>" 
df -k .
echo "remote path> \$PATH"

Do tar xf ${cvsModule}.tar

echo "Starting main build at" \`date\`

Do cd ${cvsModule}
Do echo \$SHELL
MAKE="$makeCmd"
export MAKE
Do ./build AMPI $versSpace $extraBuildOPT $extraCFlags 

echo "Main build finished at" `date`

END_BUILD

if echo $version | grep bluegene > /dev/null 2>/dev/null;
then target=bgtest;
else target=test;
fi

######################## CREATE TEST SCRIPTS ###########################

cat > $scripts/test < $scripts/prefix 
cat >> $scripts/test << END_BUILD
# Main test sequence:
echo "Starting testing at" \`date\`

if [ -x ./instead_test.sh ]
then
	Do ./instead_test.sh ${cvsModule}/$version/tmp "$makeCmd" "$extraCFlags" $target
else
(
	Do cd ${cvsModule}/$version/tmp 
	Do $makeCmd OPTS="$extraCFlags" $target
)
fi

if [ -x ./post_test.sh ] 
then
	Do ./post_test.sh 
fi

echo "Testing finished at" \`date\`

exit 0
END_BUILD


cat > $scripts/tar < $scripts/prefix 
cat >> $scripts/tar << END_TAR
# Cleanup and tar-up:
Do cd ${cvsModule}
# ( cd $version/pgms; make clean )
echo "Charm++ Automated Build" > README.version
echo " from CVS version of" `date` >> README.version
echo " for architecture $version" >> README.version
Do cd ..

Do rm -fr ${cvsModule}-$version
Do mv ${cvsModule} ${cvsModule}-$version
Do tar cf ${cvsModule}-$version.tar ${cvsModule}-$version 
Do chmod 644 ${cvsModule}-$version.tar
Do rm -fr ${cvsModule}-$version

exit 0
END_TAR




######################## RUN BUILD SCRIPTS ###########################

# Run this script on the remote machine:
SshScript() {
	stat=$1
	scr=$2
	Status $stat
	Message "local>" $ssh $buildMachine /bin/sh '<' $scr
	$ssh $buildMachine /bin/sh < $scr >> $messages 2>&1
	err=$?
	[ $err -eq 255 ] && err="0"
	if [ $err -ne 0 ]
	then
		Die "$stat failed ($err)"
	fi
	if `grep 'fatal> ' $messages > /dev/null 2>&1`
 	then
		Die "$stat failed ($err)"
	fi
	if `grep 'Connection reset by peer' $messages > /dev/null 2>&1`
 	then
		Die "$stat failed (ssh broken)"
	fi
}

errorString="Machine not available"
SshScript "Clean out build directory" $scripts/clean
errorString=

Status "Copying source to remote host"
#Do $ssh $buildMachine cat ">" $buildDir/${cvsModule}.tar < ${cvsModule}.tar
errorString="Machine not available"
Do $scp ${cvsModule}.tar $buildMachine:$buildDir/${cvsModule}.tar
errorString=

SshScript "Build on remote host" $scripts/build

./update_web

SshScript "Test on remote host" $scripts/test

SshScript "Final tar-up" $scripts/tar

Status "Grabbing binary file"
#DoNoRedir $ssh $buildMachine cat $buildDir/${cvsModule}-$version.tar > bin/${cvsModule}-$version.tar
Do $scp $buildMachine:$buildDir/${cvsModule}-$version.tar bin/

Status "Final gzip and copy"
Do gzip -f bin/${cvsModule}-$version.tar
Do $scp bin/${cvsModule}-$version.tar.gz $webMachine:$webMachBin/

Status "Good"

# Send our good status out:
./update_web

