#!/bin/bash
# Exmaples:
# ltp cvs user@host.com/var/cvs/path/to/ltp
# ltp url http://system.com/kernel.tgz 
# ltp url ftp://system.com/kernel.tgz
#

#. /etc/autobench.conf || . functions

if [ $# -lt 2 ]; then
  echo "You must specify at least two arguments: type, and source"
  exit 86
fi

#echo LSB source type: "'$SRC_TYPE'"
#echo LSB source url: "'$SRC_URL'"

LSB_TMP_DIR=$TMPDIR/lsb-tmp
mkdir -p $LSB_TMP_DIR
pushd $LSB_TMP_DIR

UNAME=`uname -m`
case "$UNAME" in 
	i?86)	ARCH=i386   ;;
	ppc*)	ARCH=ppc    ;;
	*)	ARCH=$UNAME ;;
esac

log "Using LSB architecture $ARCH.  Make sure your architecure is supported."

while [ $# -ge "0" ]; do
	case $1 in
		cvs)
			log "LSB does support CVS yet"
			shift
			SRC_URL=$1
			log "fetching LSB from cvs: $SRC_URL"
			# SRC_URL should be like:
			# :pserver:anonymous@cvs.sourceforge.net:/cvsroot/ltp
			log "fetching LSB from cvs"
			cvs -z9 -d$SRC_URL co ltp
			check_status "fetch of LSB from cvs: $SRC_URL"
			break
			;;
		url)
			shift
			SRC_URL=$1.$ARCH.rpm
			UNINSTALL="rpm -e lsb-runtime-test"
			log "fetching LSB rpm from url: $SRC_URL"
			wget --dot-style=mega $SRC_URL
			check_status "fetch of LSB from $SRC_URL"
			RPM=`basename $SRC_URL`

			which rpm > /dev/null 2>&1
			check_status "locate rpm command"

			# if a previous one didn't get cleaned up
			if rpm -q lsb-runtime-test > /dev/null; then
				log "LSB rpm already installed, removing"
				$UNINSTALL;
			fi
				
			rpm -i $RPM
			check_status "rpm installation of $RPM"
			rm $RPM
			break;
			;;
		clean)
			rpm -e lsb-runtime-test
			shift
			;;
		*)
			echo invalid LSB source specified
			exit 1
			;;
	esac
done;

# lsb requires that this link is made before it will run
if ! [ -s /lib/ld-lsb.so.1 ]; then
	ln -s /lib/ld-linux.so.2 /lib/ld-lsb.so.1;
fi;

pushd ~vsx0;
. ./.profile
(yes '' |  ./run_tests) | tee -a $LOGDIR/lsb.log
popd # ~vsx0

$UNINSTALL

popd # $LSB_TMP_DIR
rm -rf $LSB_TMP_DIR
