#!/bin/sh
# /etc/init.d/AlertProcessor
# install with: update-rc.d AlertProcessor defaults

### BEGIN INIT INFO
# Provides:          AlertProcessor
# Required-Start:    $local_fs $network $time $syslog
# Required-Stop:     $local_fs $network
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start AlertProcessor at boot time
# Description:       AlertProcessor
### END INIT INFO

case "$1" in
  start)
    if [ -f /home/dkfqs/alertprocessor/log/AlertProcessor.log ]; then
       mv /home/dkfqs/alertprocessor/log/AlertProcessor.log /home/dkfqs/alertprocessor/log/AlertProcessor.log_$(date +"%Y_%m_%d_%H_%M")
    fi
    sudo -H -u dkfqs bash -c 'CLASSPATH=/home/dkfqs/alertprocessor/bin/bcpkix-jdk15on-160.jar:/home/dkfqs/alertprocessor/bin/bcprov-jdk15on-160.jar:/home/dkfqs/alertprocessor/bin/bctls-jdk15on-160.jar:/home/dkfqs/alertprocessor/bin/com.dkfqs.oshialertprocessor.jar;export CLASSPATH;nohup java -Xmx512m -DoshiAlertProcessorProperties=/home/dkfqs/alertprocessor/config/alertprocessor.properties -Dnashorn.args="--no-deprecation-warning" com.dkfqs.oshialertprocessor.internal.StartOshiAlertProcessor 1>/home/dkfqs/alertprocessor/log/AlertProcessor.log 2>&1 &'
    ;;
  stop)
       PID=`ps -o pid,args -e | grep "StartOshiAlertProcessor" | egrep -v grep | awk '{print $1}'`
       if [ ! -z "$PID" ] ; then
          echo "AlertProcessor stopped with pid(s) : $PID"
          kill -9 ${PID} 1> /dev/null 2>&1
       fi
    ;;
  status)
       PID=`ps -o pid,args -e | grep "StartOshiAlertProcessor" | egrep -v grep | awk '{print $1}'`
       if [ ! -z "$PID" ] ; then
          echo "AlertProcessor running with pid(s) : $PID"
       else
          echo "No AlertProcessor running"
       fi
    ;;
  *)
    echo "Usage: /etc/init.d/AlertProcessor {start|stop|status}"
    exit 1
    ;;
esac

exit 0
