#!/bin/bash
### BEGIN INIT INFO
# Provides:          ncachebridged 
# Required-Start:    mountkernfs $local_fs
# Required-Stop:
# Default-Start:     2 3 4 5
# Default-Stop:
# Short-Description: NCache Bridge Daemon service
### END INIT INFO
# Source function library.
. /lib/lsb/init-functions

start() {
   	PID="$(pgrep -f Alachisoft.NCache.BridgeDaemon)"
	if [ -z $PID ]
	then
   		<INSTALLDIR>/bin/service/Alachisoft.NCache.BridgeDaemon start
	fi
}

stop() {

	PID="$(pgrep -f Alachisoft.NCache.BridgeDaemon)"
	if [[ ! -z $PID ]]
	then
		kill -9 $PID
	fi
}

status() {

	PID="$(pgrep -f Alachisoft.NCache.BridgeDaemon)"
	if [ -z $PID ]
	then
	echo "Service: ncached"
	echo "Active: inactive (dead)" 
	else
	started="$(ps -p $PID -o lstart | tail -1)"
	memory="$(ps -p $PID -o %mem | tail -1)"
	cpu="$(ps -p $PID -o %cpu | tail -1)"
	echo "Service:   ncached"
	echo "Active:    active (running) since $started"
	echo "Main PID:  $PID"
	echo "Memory:   $memory"
	echo "CPU:       $cpu"
	fi
}

case "$1" in 
    start)
       start
       ;;
    stop)
       stop
       ;;
    restart)
       stop
       start
       ;;
    status)
	status
       ;;
    *)
       echo "Usage: $0 {start|stop|status|restart}"
esac

exit 0