#!/bin/bash
# $1 . to reuse log directry, anything else for new log dir
#     If not .  then $1 will become part of the log directory name.
# $2 benchmark script name
# $3+  extra args passed to benchmark script.
. /etc/autobench.conf || . functions

# Install our dependancies
getcommand getsysinfo
getcommand streamfilter
getcommand watchfor

# Try to install the command if it's not in $AUTODIR
getcommand $2

OLDLOGDIR=$LOGDIR
RUNLOG_SCRIPT=$AUTODIR/var/runlog
LOGDIR_SCRIPT=$AUTODIR/var/logdir

if [ "$1" != "." -a "$1" != \".\" -o ! -a "$LOGDIR_SCRIPT"  ]
then
    echo new log requested
    LOGDIR=$(scrub_string "$1")
    echo "export LOGDIR=$AUTODIR/logs/$2.$LOGDIR.`date +%y-%m-%d_%H.%M.%S`$DOT_JOB_ID" \
    	> $LOGDIR_SCRIPT
    echo "export RUN_NUMBER=001" > $RUNLOG_SCRIPT
else 
    . $RUNLOG_SCRIPT
    RUN_NUMBER=`echo $RUN_NUMBER | awk '{printf ("%03d", $1 + 1)}'`
    echo "export RUN_NUMBER=$RUN_NUMBER" > $RUNLOG_SCRIPT
fi
. $LOGDIR_SCRIPT
. $RUNLOG_SCRIPT
mkdir -p $LOGDIR 
mkdir -p $LOGDIR/benchmark 
mkdir -p $LOGDIR/analysis 

shift
log "Running command $@"

# capture proc information
getsysinfo before $LOGDIR $RUN_NUMBER

# removed extra streamfilter, don't know why it was here, but
# it is messing up killall on ppc with sles9
#{
	# run the actual command
	"$@" | streamfilter

	echo "rc = $? from $@" >> $NONTESTLOGDIR/run-result.log
#} | streamfilter

# capture proc info after the run
getsysinfo after $LOGDIR $RUN_NUMBER

LOGDIR=$OLDLOGDIR

