#!/bin/bash
# This is the script to start and stop the iostat profiler.
#
# reqparam value start|stop|report|postprocess|install Which action you'd like to perform
# reqparam value Collection interval for iostat
# optparam value Suffix - an optional string to append to the name of the log file 
#

# ripped from sar script, S. Rao 

. /etc/autobench.conf || . functions

if [ $# -lt 1 ]; then
  echo "You must specify start, stop, report, postprocess, or install"
  exit 1
fi

CMD="$(echo $1 | tr -t A-Z a-z)"

if [ $# -eq 3 ]; then
  SUFFIX=".$3"
fi

function install_sar() {
  # Install iostat/sar if it isn't already...
  if [ ! \( -d $AUTODIR/sources/sysstat -a -x $AUTODIR/sources/sysstat/iostat \) ]; then
    getcommand sar
    $AUTODIR/scripts/profilers/install/sar
  fi
}

case $CMD in
  install)
    install_sar
    ;;

  print-start)
    if [ $# -ge 2 ]; then
      if [ $2 != 0 ]; then
        echo 'PATH=$AUTODIR/sources/sysstat:$PATH'
        echo 'iostat -x '$2' >> "$LOGDIR/analysis/iostat.$RUN_NUMBER$SUFFIX"  &'
      fi
	else 
        echo 'PATH=$AUTODIR/sources/sysstat:$PATH'
        echo 'iostat -x 5 >> "$LOGDIR/analysis/iostat.$RUN_NUMBER$SUFFIX" &'
    fi
    ;;

#  stop)
    #stop sar
                 # Full path is specifies here so that we don't kill ourselves
#    killall $AUTODIR/sources/sysstat/iostat
#    ;;

  print-stop)
    echo 'killall $AUTODIR/sources/sysstat/iostat'
    ;;

  report)
    ;;

  *)
    echo "The first argument must be start, stop, report, postprocess, or install"
    ;;
esac
