#!/bin/bash

# Download pounder components and build them.

# Copyright (C) 2003-2006 IBM
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 2 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
# 02111-1307, USA.

source libpounder.sh

export USE_CACHE=1

# do we actually _have_ a cache server?
if [ -z "$POUNDER_CACHE" -o "$POUNDER_CACHE" == "0" ]; then
	export USE_CACHE=0
fi

#check utilities needed to run the tests before getting started.
#If you test uses something else or a build fails add
#the command to the list manually.
COMMANDS="make g++ lex gcc python wget sudo diff patch egrep rm echo test which cp mkdir"
MISSING_TOOLS=""
echo -en "Looking for tools..."
for cmd in $COMMANDS
do
	echo -en "$cmd "
	if which $cmd > /dev/null
	then
		true;
	else
		MISSING_TOOLS="$cmd $MISSING_TOOLS"
	fi
done

echo .

if [ -n "$MISSING_TOOLS" ]; then
	echo "Please install these tools: $MISSING_TOOLS."
	exit 1
fi

echo "All tools were found."

# Parse arguments
while getopts n? o
do
	case "$o" in
		n) echo "Not using tarball cache."; export USE_CACHE=0;;
	esac
done

# Set up optdir
mkdir -p "$POUNDER_OPTDIR"
if [ ! -d "$POUNDER_OPTDIR" ]; then
	echo "Could not create $POUNDER_OPTDIR; aborting."
	exit 1
fi

UNPACKED=0

while [ "$UNPACKED" -lt 1 ]; do
	# Unpack default test configuration?
	SCHEDULERS=`ls $POUNDER_HOME/schedulers | awk -F"-tests.tar.gz" '{print $1}'`
	echo "WHICH TEST SCHEDULER SETUP DO YOU WANT TO UNPACK?"
	echo "[Choose from:"
	echo "$SCHEDULERS"
	echo "[Or simply press ENTER for the default scheduler]"
	echo -n "Scheduler selection: "
	read f
	if [ -z "$f" ]; then
		SCHEDPACK="$DEFAULT_SCHEDPACK"
	else
		SCHEDPACK="$f"
	fi

	rm -rf tests/*

	tar -xzvf "$POUNDER_HOME/schedulers/$SCHEDPACK-tests.tar.gz"

	if [ "$?" -ne 0 ]; then
		echo "Unable to untar $SCHEDPACK-tests.tar.gz; please try again."
	else
		echo "Untarred $SCHEDPACK-tests.tar.gz successfully."
		UNPACKED=1
	fi
done

echo -en "Would you like to automate skipping of failed subtests? (subtests that failed to build will automatically be removed from test scheduler) [y/n]? "
read f
if [ "$f" == "y" -o "$f" == "Y" ]; then
	AUTO_SKIP=1
else
	AUTO_SKIP=0
fi

# start builds...
for i in $BUILDS
do
	if [ "$i" = "CVS" ]; then
		continue
	fi
	if [ -x "build_scripts/$i" ]; then
		FOUND=`find $POUNDER_HOME/tests -name *$i`
		if [ -n "$FOUND" ]; then
			"build_scripts/$i" $*
			BUILD_ERR=$?
			if [ "$BUILD_ERR" -ne 0 ]; then
				if [ $AUTO_SKIP -eq 0 ]; then
					echo -en "$i build failed with Error $BUILD_ERR. If this is a subtest, would you like to skip it [y/n]? "
					read f
					if [ "$f" == "y" -o "$f" == "Y" ]; then
                        			rm `find $POUNDER_HOME/tests -name *$i` > /dev/null 2>&1
						if [ "$?" -ne 0 ]; then
							echo "Failed to remove $i from test scheduler. $i is either not a subtest included in $SCHEDPACK-tests.tar.gz or is incorrectly defined in $POUNDER_HOME/tests (See SCHEDULER for naming rules if this is the case)."
							echo -en "Skip anyway? [y/n] "
							read f
							if [ "$?" == "y" -o "$f" == "Y" ]; then
								exit 0
							elif [ "$f" == "n" -o "$f" == "N" -o -z "$f" ]; then
								exit 1
							fi
						fi
                			elif [ "$f" == "n" -o "$f" == "N" -o -z "$f" ]; then
						echo "Exiting install ..."
						exit 1
					fi
				else
					rm `find ./tests -name *$i` > /dev/null 2>&1
				fi
			fi
		fi
	fi
done

# build our little helper program
make helpers

echo Pounder is done building. ENJOY!