#!/sbin/sh
#
# whatap-infra daemon control script.
# Written for SunOS by whatap

WHATAP_HOME=/usr/whatap/infra/conf
export WHATAP_HOME
BIN=/usr/whatap/infra/whatap_infrad
CONF=/usr/whatap/infra/conf/whatap.conf
PID=/usr/whatap/infra/conf/whatap.pid

whatapinfra_start() {
  # Sanity checks.
  if [ ! -r $CONF ]; then # no config file, exit:
    echo "$CONF does not appear to exist. Abort."
    exit 1
  fi

  if [ -s $PID ]; then
    echo "whatap-infra appears to already be running?\nTo override, you could delete pid file like following.\n\nrm -f $PID \n"
    exit 1
  fi

  echo "Starting whatap-infra daemon..."
  if [ -x $BIN ]; then
    $BIN 
  fi
}

whatapinfra_test_conf() {
  echo "Checking configuration for correct syntax and"
  echo "then trying to open files referenced in configuration..."
  $BIN -t -c $CONF
}

whatapinfra_term() {
  echo "Shutdown whatap-infra quickly..."
  kill -TERM `cat $PID`
}

whatapinfra_stop() {
    echo "Shutdown whatap-infra gracefully..."
    kill -QUIT `cat $PID`
    rm -f $PID
}

whatapinfra_reload() {
  echo "Reloading whatap-infra configuration..."
  kill -HUP `cat $PID`
}

whatapinfra_upgrade() {
  echo "Upgrading to the new whatap-infra binary."
  echo "Make sure the whatap-infra binary has been replaced with new one"
  echo "or whatap-infra modules were added/removed."
  kill -USR2 `cat $PID`
  sleep 3
  kill -QUIT `cat $PID.oldbin`
}

whatapinfra_rotate() {
  echo "Rotating whatap-infra logs..."
  kill -USR1 `cat $PID`
}

whatapinfra_restart() {
  whatapinfra_stop
  sleep 3
  whatapinfra_start
}

case "$1" in
  check)
    whatapinfra_test_conf
    ;;
  start)
    whatapinfra_start
    ;;
  term)
    whatapinfra_term
    ;;
  stop)
    whatapinfra_stop
    ;;
  reload)
    whatapinfra_reload
    ;;
  restart)
    whatapinfra_restart
    ;;
  upgrade)
    whatapinfra_upgrade
    ;;
  rotate)
    whatapinfra_rotate
    ;;
  *)
  echo "usage: `basename $0` {check|start|term|stop|reload|restart|upgrade|rotate}"
esac
