#!/usr/local/bin/bash

# This batch file runs pga lots of times and saves the last few lines
# of output of each run in a given log file.

if [ $# -le 3 ] ;
then
  echo Usage: pga-batch log-file no-of-runs lines-to-save pga-args..
  echo "      " This runs pga the given number of times,
  echo "      " and notes the final state of each run in log-file.
  echo "      " That is, after each run, the last few lines of output are
  echo "      " appended to the given log-file.
  exit 0
fi

logFile=$1
nRuns=$2
nSaveLines=$3
shift
shift
shift
pgaArgs=$*
tmpFile=/tmp/pga-batch-$$

trap "rm -f $tmpFile" 1 2 3 4 5 6 7 8 9 10 11 12 13

echo "++++++++++++++++++++++++++++++++++++++++++" >> $logFile
echo Batch of $nRuns runs of PGA with args: >> $logFile
echo "    " $pgaArgs >> $logFile
echo started on `date` >> $logFile

while [ $nRuns -gt 0 ] ;
do
  pga $pgaArgs $tmpFile ;
  echo >> $logFile ;
  grep random $tmpFile >> $logFile ;
  tail -$nSaveLines $tmpFile >> $logFile ;
  rm $tmpFile ;
  nRuns=$[ $nRuns - 1 ] ;
done

