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

. /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 2 ]; then
  SUFFIX=".$2"
fi

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

case $CMD in
  install)
    install_readcg
    ;;

  start)
    install_readcg

    #start readcg
    pushd $AUTODIR/sources/readcg > /dev/null
    ./readcg -c	# Clear counters
    ./readcg -e	# Enable counters
    popd > /dev/null
    ;;

  print-start)
	echo "pushd $AUTODIR/sources/readcg > /dev/null"
        echo './readcg -c'
	echo './readcg -e'
	echo 'popd'
    ;;

  stop)
    #stop readcg
    pushd $AUTODIR/sources/readcg > /dev/null
    ./readcg -d # Disable counters
    if find_system_map; then
      ./readcg -m $SYSTEM_MAP > $LOGDIR/analysis/call_graph.txt
      ./readcg -c
    fi
    popd > /dev/null
    ;;

  print-stop)
    echo "pushd $AUTODIR/sources/readcg > /dev/null"
    echo './readcg -d'
    echo 'if find_system_map; then'
    echo '  ./readcg -m $SYSTEM_MAP > $LOGDIR/analysis/call_graph.txt'
    echo '  ./readcg -c'
    echo 'fi'
    echo 'popd'
    ;;

  report)
    ;;

  postprocess)
    ;;

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

