#!/bin/sh
PATH=$PATH':/usr/local/bin'
datestr=`date | awk '{printf("%s_%d_%d",$2,$3,$6)}'`
timestr=`date | awk '{printf("%s %s",$4,$5)}'`
outfile=`pwd`"/build_stats_freebsd_${datestr}"
outfile2=`pwd`"/build_stats_${datestr}"
thisdir=`pwd`
domail=1
noisy=0

while [ $# -gt 0 ]
do
  case $1 in
    --nomail) domail=0;;
    --noisy) noisy=1;;
  esac
  shift
done

echo > $outfile
echo "***********************************************************" >> $outfile
echo "freebsd DEVELOPMENT builds: $datestr at $timestr" >> $outfile
echo "***********************************************************" >> $outfile
cd /Net/Proj/Xyce/people/tvrusso/nightly_builds/Xyce
touch ../LOCK_SOURCE
cvs update -d -P 
if [ $? != 0 ] 
then
  echo "CVS Update failed!" >> $outfile
  exit 1
fi
rm ../LOCK_SOURCE
cd ..
ssh -f lifshitz 'cd /Net/Proj/Xyce/people/tvrusso/nightly_builds; ./do_nightly_slave linux'
ssh -f bardeen.mp.sandia.gov 'cd /Net/Proj/Xyce/people/tvrusso/nightly_builds; ./do_nightly_slave sgi'
/usr/bin/rsh -n electron "cd /Net/Proj/Xyce/people/tvrusso/nightly_builds; ./do_nightly_slave dec" > /dev/null &
cd PLAT_freebsd
for i in `ls -d Xyce_freebsd*`
do
  if [ -d $i ]
  then
    echo $i
    cd $i
    ./reconfigure > ../${i}_configure_${datestr} 2>&1
    if [ $? != 0 ] 
    then
      echo >> $outfile
      timestr=`date | awk '{printf("%s %s",$4,$5)}'`
      echo " Configure failed for $i at $timestr" >> $outfile
    else
      rm ../${i}_configure_${datestr}
      gmake > ../${i}_make_${datestr} 2>&1
      if test $? != 0
      then
        echo >> $outfile
        timestr=`date | awk '{printf("%s %s",$4,$5)}'`
        echo "**BUILD FAILED FOR $i at $timestr" >> $outfile
      else
	if [ -x runthetests ]
        then
          ./runthetests
          EXITSTAT=$?
          if [ $EXITSTAT = 0 ]
          then
            echo >> $outfile
            timestr=`date | awk '{printf("%s %s",$4,$5)}'`
            echo "$i: Built and passed all tests at $timestr" >> $outfile
	    gmake install >> ../${i}_make_${datestr}
          else
            echo >> $outfile
            timestr=`date | awk '{printf("%s %s",$4,$5)}'`
	    echo " $i: BUILT BUT FAILED TESTS --- NEW BINARY NOT INSTALLED at $timestr" >> $outfile
            if [ $EXITSTAT -lt 16 ]
            then
               timestr=`date | awk '{printf("%s %s",$4,$5)}'`
               echo "    -- All tests ran to completion, $EXITSTAT failed compare at $timestr" >> $outfile
            else
               NBOMB=`echo $EXITSTAT/16 | bc`
               NFAIL=`echo $EXITSTAT-$NBOMB*16 | bc`
               timestr=`date | awk '{printf("%s %s",$4,$5)}'`
	       echo "    ** $NBOMB TESTS EXITED WITH ERROR, $NFAIL others failed compare at $timestr">>$outfile
            fi
	    awk '{print "      ",$0}' Xyce_Test/test_results >> $outfile
          fi
        else
         echo >> $outfile
         timestr=`date | awk '{printf("%s %s",$4,$5)}'`
         echo "$i built but not tested, installed anyway at $timestr" >> $outfile
         gmake install >> ../${i}_make_${datestr}
        fi
        rm ../${i}_make_${datestr}
      fi
    fi  
    cd ..
  fi
done

while [ -e /Net/Proj/Xyce/people/tvrusso/nightly_builds/PLAT_linux/WORKING  -o -e /Net/Proj/Xyce/people/tvrusso/nightly_builds/PLAT_sgi/WORKING -o -e /Net/Proj/Xyce/people/tvrusso/nightly_builds/PLAT_dec/WORKING ]
do
  sleep 300
done

cat /Net/Proj/Xyce/people/tvrusso/nightly_builds/build_stats_sgi_${datestr} > $outfile2
cat /Net/Proj/Xyce/people/tvrusso/nightly_builds/build_stats_dec_${datestr} >> $outfile2
cat /Net/Proj/Xyce/people/tvrusso/nightly_builds/build_stats_linux_${datestr} >> $outfile2
cat $outfile >> $outfile2

rm /Net/Proj/Xyce/people/tvrusso/nightly_builds/build_stats_linux_${datestr} /Net/Proj/Xyce/people/tvrusso/nightly_builds/build_stats_sgi_${datestr} /Net/Proj/Xyce/people/tvrusso/nightly_builds/build_stats_dec_${datestr} /Net/Proj/Xyce/people/tvrusso/nightly_builds/build_stats_freebsd_${datestr}

subject="DEVELOPMENT BRANCH: build result"
failures=0

grep -q -i fail $outfile2
if [ $? = 0 ]
then
   subject="TEST FAILURES IN DEVELOPMENT BRANCH!"
   failures=1
fi

if [ $failures = 1 -o $noisy = 1 ]
then
 if [ $domail = 1 ]
 then
   Mail -s "$subject" xyce-developers@sandia.gov < $outfile2
 else
   echo "would have sent mail with $subject to xyce-developers"
 fi
fi

cd $thisdir
mv $outfile2 stats_archive
/usr/sbin/chown -R :xyce .
/bin/chmod -R g+r .
