#!/bin/bash
#automated check of ipp build

#write step and return status to summary log file
writeStatus()
{
if [ $2 = "0" ]; then
  status="PASS"
else
  status="FAIL"
fi
echo $1$status >> $logfile
}

#write step to summary, build, and error files
writeStep()
{
  echo $1 >> $logfile
  echo -e "\n++++"$1"++++" >> $buildfile 
  echo -e "\n++++"$1"++++" >> $errorfile
}

PATH=$PATH:/bin:/usr/bin:/usr/ucb:/etc:.
export PATH

#log files
buildfile="/data/ipp002.0/ippTests/buildtest/logs/buildlog.$(date -u +%Y%m%d)"
errorfile="/data/ipp002.0/ippTests/buildtest/logs/errorlog.$(date -u +%Y%m%d)"
logfile="/data/ipp002.0/ippTests/buildtest/logs/summlog.$(date -u +%Y%m%d)"
echo " Build file name: "$buildfile > $logfile
echo "Error file name: "$errorfile >> $logfile

#remove old psconfig
writeStep "Remove old psconfig dir"
rm -rf /data/ipp002.0/ippTests/buildtest/psconfig/*.lin64
writeStatus "Remove old psconfig dir status: " $?

#remove old ipp build
writeStep "Remove old ipp build"
cd /data/ipp002.0/ippTests/buildtest
rm -rf /data/ipp002.0/ippTests/buildtest/ipp
writeStatus "Remove old ipp build status: " $?

#svn checkout
writeStep "Begin svn co ipp" 
svn co http://svn.pan-starrs.ifa.hawaii.edu/repo/ipp/trunk/ ipp >> $buildfile 2>> $errorfile 
writeStatus "SVN checkout status: " $?

#psconfig bootstrap
writeStep "Change to psconfig dir"
cd /data/ipp002.0/ippTests/buildtest/ipp/psconfig
writeStatus "Change to psconfig dir status: " $?
PSCONFDIR=/data/ipp002.0/ippTests/buildtest/psconfig
export PSCONFDIR

PSCONFIG="buildtest.lin64"
export PSCONFIG

echo "PSCONFDIR =" $PSCONFDIR >> $logfile
echo "PSCONFIG =" $PSCONFIG >> $logfile

writeStep "Running psbuild -bootstrap"
psbuild -bootstrap $PSCONFDIR >> $buildfile 2>> $errorfile
writeStatus "Run psbuild -bootstrap status: " $?

#psconfig realtest build
alias psconfig='source /data/ipp002.0/ippTests/buildtest/psconfig/psconfig.bash'
source /data/ipp002.0/ippTests/buildtest/psconfig/psconfig.bash buildtest
writeStatus "Run psconfig buildtest status: " $?

#get cfitsio
writeStep "Running pschecklibs -build"
pschecklibs -build >> $buildfile 2>> $errorfile
writeStatus "Run pschecklibs -build status: " $?

#run psbuild -dev -extlibs
writeStep "Running psbuild -dev -extlibs"
psbuild -dev -extlibs >> $buildfile 2>> $errorfile
writeStatus "Run psbuild -dev -extlibs status: " $?

#mail results
writeStep "Mail results"
/data/ipp002.0/ippTests/buildtest/mailresults.pl
