#!/bin/sh 
#
# This file: /usr/lib/sat/benchmark/lloops/run
#

if [ x"${SAT_DEBUG-0}" != x0 ] ; then
   echo "*** SAT_DEBUG Environmental variable = $SAT_DEBUG"
   echo "Environment is:"
   env
fi

# run just one thread
DFLT_NCPUS=1
export DFLT_NCPUS

# Initialize local variables
exitCode=0
testError=1
miscError=2
abortCode=3
title="`sed -n '1p' README`"

# Source directory
sourceDir=`pwd`

# working directory for sats (default is /usr/tmp)
SAT_USR_TMP=${SAT_USR_TMP-/usr/tmp}

# Define temporary directory to run program in
programWorkDir=$SAT_USR_TMP/lloops.$$

# Define temporary scratch files
#        Must be in "$SAT_USR_TMP" and allow for multiple invocations
programScratchFile=$SAT_USR_TMP/lloops.scratch.$$
programErrorFile=$SAT_USR_TMP/lloops.errors.$$

programResultsFile=${programWorkDir}/output

#
# Signal handling - trap typical signals and special signal from sat driver
#
# Leave logs alone if interrupted for debugging purposes. Tell sat driver
# we were interrupted via special exit code.
#
trap "Interrupt 1" 1
trap "Interrupt 2" 2
trap "Interrupt 3" 3
trap "Interrupt 15" 15
trap "Interrupt 30" 30  # sat wants us to abort

Interrupt() {

        echo "SAT run shell script interrupted by signal $1"
	cleanup $abortCode
}

# Remove temporary file(s) function: expected cleanup
removeFiles() {

   # Remove scratch/result files
   rm -f $programScratchFile
   rm -f $programResultsFile
   rm -f $programErrorFile

   # Remove temporary directory
   cd $sourceDir
   rm -fr $programWorkDir
}

# General cleanup and exit routine (optional arg 1 is exit code)
cleanup() {

   case "$#" in
   0)  exitCode=$miscError;;
   *)  exitCode=$1;;
   esac

   if test -f $programWorkDir/core -o -d $programWorkDir/core
   then
      echo "lloops sat dumped core" 1>&2
      coreinfo $programWorkDir/core 1>&2
   fi

   if [ x"${SAT_DEBUG-0}" = x0 -o "$exitCode" -eq 0 -o \
	 "$#" -ge 2 -a "$2" = nosave ]; then
      removeFiles
   fi

   exit $exitCode
}


# Prepare
removeFiles

# Create and change to temporary directory
if mkdir $programWorkDir
then
   cd $programWorkDir
else
   echo "Cannot create temporary directory \"$programWorkDir\"" 1>&2
   cleanup $miscError
fi

# Check for partition name, passed from sat command
if test -z "$1"
then
   echo "No partition argument supplied." 1>&2
   cleanup $miscError
fi

# Initialize empty results files
touch $programScratchFile
touch $programResultsFile

# Verify program is executable
if test -x $sourceDir/lloops2
then
   # Execute program
   if $sourceDir/lloops2 > $programScratchFile 2> $programErrorFile
   then
      # Verify results, see filter results below
      :
   else
      # Non-zero test exit, pass to sat
      exitCode=$?
      echo "$sourceDir/lloops2 exit code: $exitCode" >> $programScratchFile

      cat $programScratchFile
      cat $programErrorFile 1>&2

      cleanup $testError
   fi
else
   echo "No \"lloops2\" executable found." 1>&2
   cleanup $miscError
fi

# Harmonic mean, first occurance
Mflops="`grep 'Harmonic  Mean =' $programResultsFile | \
         awk 'NR == 1 { printf \"         %s %s %s %.2f Mflops\n\", $1, $2, $3, $4 }'`"

# Report PASS/FAIL results, Mflops non-null
if test -n "$Mflops" -a ! -f core -a ! -d core
then
   # Program PASSed, echo results
   echo "PASS: $title."

   echo "$Mflops"
else
   # Program FAILed, cat scratch file back to sat
   echo "FAIL: $title."

   cat $programResultsFile
   cat $programErrorFile 1>&2

   cleanup $testError
fi

# Finish and exit
cleanup $exitCode
