#!/bin/bash
# Author: Enrique Gaona Jr.
#         IBM LTC Infrastructure
# Date:   08/19/2003
# optparam prefix "-k" value type location: Where type and location can be passed directly into the get_kernel function in functions
# optparam prefix "-c" value A URL to the config file to use
# optparam prefix "-t" value Specify the tmporary file to use for io loads
# optparam prefix "-o" value Specify the tmporary file to use for io_other (required if io_other specified)
# optparam prefix "-n" value The number of iterations (default 3)
# optparam value repeat The load to use.  Values are: no_load, cacherun, pro-cess_load, ctar_load, xtar_load, io_load, read_load, list_load, mem_load and dbench_load (default all loads)
#

. /etc/autobench.conf || . functions

. $AUTODIR/var/server-repository
BINDIR=$AUTODIR/sources/contest
# Defaults
KERNEL="linux-2.4.19.tar.gz"
CONFIG_URL="$AUTODIR/sources/contest/contest.config"

# Parse the arguments
while [ $# -gt 1 ]; do
  case $1 in
  -k)
      KERNEL="$2"
      shift 2
      ;;
  -c)
      CONFIG_URL="$2"
      shift 2
      ;;
  *)
      CONTEST_ARGS="$CONTEST_ARGS $1"
      shift
      ;;
  esac
done

if [ ! \( -d $AUTODIR/sources/contest -a \
          -x $AUTODIR/sources/contest/contest \) ]; then
  $AUTODIR/scripts/benchmarks/install/contest
fi

# One of the workloads require dbench
if [ ! \( -d $AUTODIR/sources/dbench -a \
          -x $AUTODIR/sources/dbench/dbench \) ]; then
  $AUTODIR/scripts/benchmarks/install/dbench 
  # Silently fail if dbench could not be installed. Only one workload needs it
fi
PATH=$AUTODIR/sources/debnch:$PATH

getcommand doprofilers
doprofilers install

# Prepare the benchmark ######################################################
case `uname -m | sed -e s/i.86/i386/ -e s/sun4u/sparc64/ -e s/arm.*/arm/ \
      -e s/sa110/arm/` in
  i386)
          true # Don't need to do anything here
          ;;
  *)
          # Unless we say otherwise, we'll have to cross compile.
          MAKE_OPTS="CROSS_COMPILE=i386-linux- ARCH=i386"
          PATH=$AUTODIR/sources/cross-compiler/bin:$PATH
          ;;
esac

mkdir -p $TMPDIR/contest
#get_kernel $TMPDIR/contest $KERNEL
#check_status "Getting and unpacking the kernel..."

pushd $TMPDIR/contest > /dev/null
wget --progress=bar -q $SERVER_REPO/sources/$KERNEL
#get_file_as $TMPDIR/$KERNEL $SERVER_REPO/sources/$KERNEL
tar -xzf $TMPDIR/$KERNEL
DST_DIR=`tar ztf $KERNEL | head -n 1`
pushd $DST_DIR > /dev/null
get_file_as .config $CONFIG_URL
yes "" | make $MAKE_OPTS oldconfig >/dev/null
make $MAKE_OPTS dep > /dev/null

startprofilers
$BINDIR/contest $CONTEST_ARGS | tee $LOGDIR/benchmark/contest.$RUN_NUMBER
stopprofilers

doprofilers report
doprofilers postprocess
$BINDIR/contest -r > $LOGDIR/benchmark/contest-report.$RUN_NUMBER 

# clean up the tree to save space
#rm -rf $TMPDIR/contest
popd > /dev/null
popd > /dev/null
