
#!/bin/bash

function check_okay {
	if [ $? -ne 0 ]
	then
		exit 1
	fi
}

function clean_one {
	cd $1
	check_okay
	echo "clean" $1
  ./clean
  check_okay
	cd ..
}

function run_star_tests {

  echo \*\*\*\*\*\*\* RUN STAR TESTS \*\*\*\*\*\*\*
  echo \*\*\*\*\*\*\* RUN STAR TESTS \*\*\*\*\*\*\* >> $OUTPUT

  cd star/test_suite
  ./each_test_run

  echo \*\*\*\*\*\*\* report star tests \*\*\*\*\*\*\*
  echo \*\*\*\*\*\*\* report star tests \*\*\*\*\*\*\* >> $OUTPUT
  ./report >> $OUTPUT

  cd ../..

}

function run_binary_tests {

  echo \*\*\*\*\*\*\* RUN BINARY TESTS \*\*\*\*\*\*\*
  echo \*\*\*\*\*\*\* RUN BINARY TESTS \*\*\*\*\*\*\* >> $OUTPUT

  cd binary/test_suite
  ./each_binary_test_run

  echo \*\*\*\*\*\*\* report binary tests \*\*\*\*\*\*\*
  echo \*\*\*\*\*\*\* report binary tests \*\*\*\*\*\*\* >> $OUTPUT
  ./report >> $OUTPUT

  cd ../..
  
}

#some definitions
HOSTNAME=`hostname`
DATE=`date '+%d/%m/%y'`
TESTDIR='mesa_test'
MESADIR=`pwd`
OUTPUT=$MESADIR/test_output
VERSION=`cat $MESADIR/test_version`

#clean up from last time
cd $MESADIR

rm -rf $OUTPUT

echo output $OUTPUT

#print email header
echo ---------------------------------------------- >> $OUTPUT
echo MESA test script for $DATE >> $OUTPUT
echo $HOSTNAME >> $OUTPUT 
echo current test version = $VERSION >> $OUTPUT
ifort -V >> $OUTPUT
echo ---------------------------------------------- >> $OUTPUT
echo >> $OUTPUT
echo check if find '"'failed'"' in text >> $OUTPUT
echo >> $OUTPUT


if [ "$1" = checkout ]
then

  rm -rf $TESTDIR
  #checkout and install
  echo \*\*\*\*\*\*\* INSTALL \*\*\*\*\*\*\* >> $OUTPUT
  svn co -r $VERSION svn://svn.code.sf.net/p/mesa/code/trunk $TESTDIR
  cd $TESTDIR
  cp utils/makefile_header.mesasdk utils/makefile_header

else
  
  cd $TESTDIR
	if [ $? -ne 0 ]
	then
	  echo failed to find $TESTDIR
		exit 1
	fi
  
  echo \*\*\*\*\*\*\* UPDATE \*\*\*\*\*\*\* >> $OUTPUT
  svn update --accept 'theirs-full' -r $VERSION

  if [ "$1" = update ]
  then
    echo \*\*\*\*\*\*\* CLEAN \*\*\*\*\*\*\*
    echo \*\*\*\*\*\*\* CLEAN \*\*\*\*\*\*\* >> $OUTPUT
    ./clean
  fi

  if [ "$1" = touch ]
  then
    echo \*\*\*\*\*\*\* TOUCH \*\*\*\*\*\*\*
    echo \*\*\*\*\*\*\* TOUCH \*\*\*\*\*\*\* >> $OUTPUT
    ./touch
  fi

fi

if [ "$1" != justrerun ]
then
  echo \*\*\*\*\*\*\* INSTALL \*\*\*\*\*\*\*
  echo \*\*\*\*\*\*\* INSTALL \*\*\*\*\*\*\* >> $OUTPUT
  ./install | tee install.out
  grep FAILED install.out >> $OUTPUT
  if [ $? -eq 0 ]
  then
    cat $OUTPUT | mutt -s "MESA test failed during install" paxton@kitp.ucsb.edu
  	exit 1
  fi
fi

echo >> $OUTPUT

run_star_tests
run_binary_tests

cd $MESADIR

echo \*\*\*\*\*\*\* send email and all done \*\*\*\*\*\*\*
echo \*\*\*\*\*\*\* send email and all done \*\*\*\*\*\*\* >> $OUTPUT

cat $OUTPUT | mutt -s "MESA test $DATE, version $VERSION" paxton@kitp.ucsb.edu

exit

#notes on mutt for sending emails:
#sends email with the results for us to check
#use mutt to send attachments
#mutt -i filename puts filename into message body
#mutt -a filename attaches filename to message
#mutt -s subject
