#!/bin/sh

# rundfmon    controls Hi-Track Monitor (V4.4)
#
# chkconfig:   2345 99 99
# description: Hi-Track Monitor continuously checks the status of HDS\
#              -supported devices and reports error conditions to HDS\
#              The 'rundfmon' name is used for historical reasons.



# check for any program using the HiTrack.jar Java program
PID=`/bin/ps -ef |grep HiTrack.jar |grep -v grep|awk '{print $2}'`


start() {
    # Check that Hi-Track Monitor is not currently running
    # then start it.
    if [ -n "$PID" ]; then
        /bin/echo "Hi-Track Monitor is already running with pid=${PID}"
    else
        OLDPATH=`/bin/pwd`
        cd MYPATH
        LD_LIBRARY_PATH=jre/lib
        export LD_LIBRARY_PATH
        jre/bin/java -Djava.net.preferIPv4Stack=true -jar HiTrack.jar 2>HiTrack.errout & 
        cd ${OLDPATH}
    fi   
    }
    
status() {
    if [ -n "$PID" ]; then
        /bin/echo "Hi-Track Monitor is running with pid=${PID}"
    else
        /bin/echo "Hi-Track Monitor is not running"
    fi
    }
   
stop()  {
    if [ -n "$PID" ]; then
        /bin/echo "Stopping Hi-Track Monitor, pid=${PID}"
        /bin/kill -TERM ${PID}
        /bin/sleep 2
        PID=`/bin/ps -ef |grep HiTrack.jar |grep -v grep|awk '{print $2}'`
        if [ -n "$PID" ]; then
          /bin/kill -KILL ${PID}
          /bin/echo "Killed Hi-Track Monitor"
          fi
    else
        /bin/echo "Hi-Track Monitor is already stopped"
    fi
    }
    
impexport() {
    if [ -n "$PID" ]; then
        /bin/echo "Hi-Track Monitor is already running with pid=${PID}"
        /bin/echo "Use '$0 stop' to terminate before running $1"
    else
        OLDPATH=`/bin/pwd`
       cd MYPATH
        jre/bin/java -jar HiTrack.jar $1 $2
       cd ${OLDPATH}
    fi
    }


case "$1" in
  restart)
    stop $1 $2
    start $1 $2
    ;;
  start)
    start $1 $2
    ;;
  stop)
    stop $1 $2
    ;;
  status)
    status $1 $2
    ;;
  import|export)
    impexport $1 $2
    ;;
*)
    echo "Usage:  $0 [start|stop|restart|3status|import|export] importExportFileName"
    ;;
esac

