#! /bin/csh
#
# usage: startnightly tname1[:branch1] tname2[:branch2] ...
#
#  tname1 is the name of a file in $PETSC_DIR/lib/petsc/bin/maint/confignightly that defines
#  a machine, a PETSC_DIR on that machine, and PETSC_ARCH on which the script buildtest
#  should be run.
#
#  Organization of nightly tests
#
#      startnightly - run on sets of test cases, launched by crontab
#         buildtest - started up by startnightly actually configures, builds, tests a particular configuration
#      copylognightly - copies the resulting output files generated by buildnightly to a central directory, launched by crontab
#      killnightly - kills and buildtest jobs that are still running at the deadline, launch by crontab
#      runhtml.py - generates the dashboard of test results using the files copieed by copylognightly, launched by crontab
#
#  Question: why does this script do the git fetch/git checkout, seems better to have
#            buildtest do that?
#  Answer: The repo could be in the wrong state [i.e a different branch - or a prior snapshot] - so the buildtest script
#          and the datafiles that get used can be out-of-sync with the 'latest' state of the branch.
#
set dir=`dirname $0`
set bdir=`dirname $dir`
set pdir=`dirname $bdir`

foreach TEST ($*)
  source $dir/confignightly/default
  source $dir/confignightly/${TNAME}

  echo "Starting $TEST : $PETSC_ARCH build at `date +'%a, %d %b %Y %H:%M:%S %z'`"
  if ("${GIT}" == "yes") then
    $SSH $USR@$MACH $DASHN test "\( -d  $PETSC_DIR \)"
    if ("$?" != "0") then
      if ("$URL" != "") then
      echo "$PETSC_DIR does not exist! Creating clone using $URL"
      $SSH $USR@$MACH $DASHN "git clone -q $URL $PETSC_DIR"
      endif
    endif
    echo "Cleaning and updating clone at $USR@$MACH $PETSC_DIR"
    $SSH $USR@$MACH $DASHN "cd $PETSC_DIR ; git fetch -p -q origin; git checkout -f origin/${BRANCH} "
  else
    rsync -e ssh -az --delete  $pdir/ ${USR}@${MACH}:${PETSC_DIR}
  endif
  echo "Starting: $SSH $USR@$MACH $DASHN $PETSC_DIR/lib/petsc/bin/maint/buildtest $PETSC_ARCH"
  $SSH $USR@$MACH $DASHN "cd $PETSC_DIR; $PETSC_DIR/lib/petsc/bin/maint/buildtest $PETSC_ARCH" &
  sleep 15 # perhaps ease the nfs load
end
wait
