#initial attempt at an automation script: ltprun
#       3/12/02 William Jay Huie (creation)
#       3/28/02 William Jay Huie minor updates
#this will be kicked off by ltp_master from the master control machine which
#uploads this script and then telnets into each machine and kick this script off
#perhaps by passing a uniq ID to name the LTP_OUTPUT_TAR file by, or 
#allowing the script to create a unique name itself (reccommended for now)
#Check ltp_master for details
#FIXME: One problem is that the tests need to be run as root and this script
#       doesn't as of yet su
#
#CHANGEME:
LTP_HOST=ltp_host.somewhere.org
#This is the user to get the ltp.tgz file from, not who we're running as,
#     that's ocntrolled by ltp_master
LTP_USER=ltp
LTP_PASS=ltp
LTP_TARFILE=ltp.tgz
LTP_RUNALL_OUT=runall.output
LTP_LOGFILE=ltp-logfile
LTP_RUN_OUTPUT=ltprun.out
SAR_OUTFILE=sar.data

#This script passes the -l ~/ltp/ltp-logfile option to runalltests.sh

if [ -z $1 ]; then
   SHORT_HOSTNAME=`hostname | perl -e 'while(<>){ m/(\w+)[.\\$]?/ && print $1;}'`
   TIMESTAMP=`date +%s`
   LTP_OUTPUT_TAR="$SHORT_HOSTNAME-$TIMESTAMP-ltpoutput.tgz"
else
   LTP_OUTPUT_TAR=$1
fi

download_ltp()
{
   echo "Attempting to download the LTP testcases";
   cd ~ 
   rm -Rf ltp $LTP_TARFILE 
   ftp -n $LTP_HOST << END_GET
   	user $LTP_USER $LTP_PASS
	bin
	get $LTP_TARFILE
	bye
END_GET

   if [ -s $LTP_TARFILE ]; then 
      echo "              downloaded sucessfully"; 
   else
      echo "FAILED        download of LTP Testcases"; return 0;
   fi
   return 1;
}
 
untar_ltp()
{
   echo "Untarring $LTP_TARFILE now";
   cd ~
   tar -zxf $LTP_TARFILE &> /dev/null
   if [ $? != "0" ]; then
      echo "Problems untarring the archive"; return 0;
   else
      echo "          successfully untarred $LTP_TARFILE";
   fi
   return 1;
}
    
build_ltp()
{
   cd ~/ltp
   echo "Building LTP Testsuite version: `head -n1 ChangeLog`";
   make clean install &> /dev/null
   if [ $? != "0" ]; then
      echo "FAILED   LTP Testsuite compilation"; return 0;
   else
      echo "         LTP Testsuite compilation successful"
   fi
   return 1;
}   

run_ltp()
{
   cd ~/ltp
   rm -f $LTP_RUNALL_OUT $LTP_LOGFILE $SAR_OUTFILE
   echo "Trying to start sar"
   sar -o $SAR_OUTFILE 60 0 &
   echo "Running LTP testsuite"
   ./runalltests.sh -l ~/ltp/$LTP_LOGFILE &> $LTP_RUNALL_OUT
   echo "Done running tests"
   killall -9 sadc
   echo "Killing sar if it is running"
   return 1;
}   

#FIXME:
#collect results has a hack to copy the &>ltprun file into the ~/ltp dir then
#tar it up with everything else, but this seems to work so far.
collect_results()
{
   echo "Collecting LTP output"
   cd ~/ltp
   cp ~/$LTP_RUN_OUTPUT .
   tar --ignore-failed-read -czf ~/$LTP_OUTPUT_TAR $LTP_RUNALL_OUT $LTP_LOGFILE $LTP_RUN_OUTPUT $SAR_OUTFILE

   if [ -s ~/$LTP_OUTPUT_TAR ]; then 
      echo "LTP output tarfile created sucessfully";
   else
      echo "FAILED        tar of LTP results"; return 0;
   fi
   return 1;
}

upload_results()
{   
   echo "Uploading LTP output"
   cd ~
   ftp -n $LTP_HOST << END_PUT
	user $LTP_USER $LTP_PASS
	bin
	put $LTP_OUTPUT_TAR
	bye
END_PUT
#FIXME! Right now don't have a way to verify the upload worked, but
#we'd like to know so we can delete the OUTPUT_TAR file
#   rm -f $LTP_OUTPUT_TAR 
    rm -f $LTP_TARFILE
}

#Start the work!

download_ltp
if [ $? = 1 ]; then 
   untar_ltp
   if [ $? = 1 ]; then 
      build_ltp
      if [ $? = 1 ]; then 
         run_ltp
      fi
   fi
fi

#Want to upload results even if things didn't run
collect_results
upload_results

echo "Done"
