#! /bin/sh
# Start/stop the acgvision-agent daemon.
#
### BEGIN INIT INFO
# Provides:          acgvisiond
# Required-Start:    $remote_fs $syslog $network
# Required-Stop:     $remote_fs $syslog 
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: background  monitoring program
# Description:       acgvision is system monitoring sofware over webservices.
### END INIT INFO
### set -x

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON="/usr/sbin/acgvision-agent"
NAME="acgvision-agent"
DESC="Agent ACGVision"
PROC="/etc/alternatives/java-Dlog4j.configuration=/etc/acgvision/agent-log4j.properties-jar/usr/share/java/acgvision-agent.jar-configurationfile/etc/acgvision/agent.ini"

test -x $DAEMON || exit 2

LOGDIR=/var/log/acgvision
PIDFILE=/var/run/$NAME.pid
DODTIME=3                   # Time to wait for the server to die, in seconds
                            # If this value is set too low you might not
                            # let some servers to die gracefully and
                            # 'restart' will not work

# Include plumi defaults if available
if [ -f /etc/default/acgvision ] ; then
        . /etc/default/acgvision
fi

. /lib/lsb/init-functions

set -e

running_pid()
{
    # Check if a given process pid's cmdline matches a given name
    pid=$1
    name=$2
    ### name=${PROC}
    [ -z "$pid" ] && return 1
    [ ! -d /proc/$pid ] &&  return 1
    cmd=`cat /proc/$pid/cmdline |head -n 1 |cut -d : -f 1`
    # Is this the expected child?
    [ "$cmd" != "$name" ] &&  return 1
    return 0
}

running()
{
# Check if the process is running looking at /proc
# (works for all users)

    # No pidfile, probably no daemon present
    [ ! -f "$PIDFILE" ] && return 1
    # Obtain the pid and check it against the binary name
    pid=`cat $PIDFILE`
    running_pid $pid ${PROC} || return 1
    return 0
}

start(){
        log_daemon_msg "Starting $DESC: "
        ps -efwwwwwwwwwwwwwwww | grep -v grep| grep -v init.d | grep $NAME | if read LIG; then
                echo "Already started"
                exit 1;
        else
		start-stop-daemon --start --startas $DAEMON --name $NAME --make-pidfile --pidfile $PIDFILE --exec $DAEMON -- $DAEMON_OPTS

                log_end_msg $?
                PIDS0=`cat $PIDFILE` && PIDS0=`expr ${PIDS0} + 1` && echo ${PIDS0} > $PIDFILE
        fi
}

stop(){
        log_daemon_msg "Stopping $DESC: "
        ps -efwwwwwwwwwwwwwwww | grep -v grep| grep -v init.d | grep $NAME | while read LIG
        do
        proc=`echo $LIG| awk '{print $2}'`
        #echo "killing $proc"
        kill  $proc
        done
}

case "$1" in
  start)
        start
        ;;
  stop)
        stop
        ;;
  restart)
        stop
         sleep 2
        start
        ;;
  force-reload)
	restart
	;;
  status)
    echo -n "$NAME is "
    if running ;  then
        echo "running"
    else
        echo " not running."
        exit 1
    fi
    ;;
  *)
        N=/etc/init.d/$NAME
        # echo "Usage: $N {start|stop|restart|reload|force-reload}" >&2
        echo "Usage: $N {start|stop|restart|force-reload|status}" >&2
        exit 1
        ;;
esac

exit 0


