#!/bin/sh
PLAT=$1
PATH="/usr/local/bin:$PATH"
if [ $PLAT = "linux" ]
then
   PATH="/Net/Proj/Xyce/arch/linux/cross-tools/bin:$PATH"
fi
datestr=`date | awk '{printf("%s_%d_%d",$2,$3,$6)}'`
timestr=`date | awk '{printf("%s %s",$4,$5)}'`
outfile=`pwd`"/build_stats_${PLAT}_${datestr}"
echo > $outfile
echo "***********************************************************" >> $outfile
echo "${PLAT} DEVELOPMENT builds: $datestr at $timestr" >> $outfile
echo "***********************************************************" >>$outfile
cd /Net/Proj/Xyce/people/tvrusso/nightly_builds/PLAT_${PLAT}
touch WORKING
for i in `ls -d Xyce_${PLAT}*`
do
  if [ -d $i ]
  then
    echo $i >> WORKING
    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}
      make > ../${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
	    make 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
         make install >> ../${i}_make_${datestr}
        fi
        rm ../${i}_make_${datestr}
      fi
    fi  
    cd ..
  fi
done
rm WORKING
