#!/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")
			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> " $@ 
	Status "Bad:" $@
	./update_web
	exit 1
}

# Run this command or die
Do() {
	Message "local> " $@
	$@ >> $messages 2>&1
	err="$?"
	if [ $err -ne 0 ] 
	then
		Die "error code $err during local> " $@ 
	fi
}


# 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}'`

ssh="ssh"
scp="scp"

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
# 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> " \$@ 
	fi
}

Do cd $buildDir

END_PREFIX


cat > $scripts/build < $scripts/prefix 
cat >> $scripts/build << END_BUILD
# Main build sequence:
rm -fr charm charm-*
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 charm.tar

echo "Starting main build at" `date`

Do cd charm
Do ./build AMPI $versSpace -O

echo "Main build finished at" `date`

END_BUILD

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

Do cd charm/$version/tmp 
Do make OPTS=-O test

echo "Testing finished at" `date`

END_BUILD


cat > $scripts/tar < $scripts/prefix 
cat >> $scripts/tar << END_TAR
# Cleanup and tar-up:
Do cd charm
# ( 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 mv charm charm-$version
Do tar cf charm-$version.tar charm-$version 
Do chmod 644 charm-$version.tar
Do rm -fr charm-$version

END_TAR




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

Status "Making build directory"
echo "mkdir -p $buildDir" | $ssh $buildMachine /bin/sh 

# 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
}

Status "Copying charm to remote host"
Do $scp charm.tar $buildMachine:$buildDir/charm.tar

SshScript "Build charm on remote host" $scripts/build

SshScript "Test charm on remote host" $scripts/test

SshScript "Final tar-up" $scripts/tar

Status "Grabbing binary file"
Do $scp $buildMachine:$buildDir/charm-$version.tar bin/

Status "Final gzip and copy"
Do gzip bin/charm-$version.tar
Do $scp bin/charm-$version.tar.gz $webMachine:$webMachBin/

Status "Good"

# Send our good status out:
./update_web

