#!/bin/bash
#
# proxyexec        Startup script proxyexec
#
# chkconfig: - 11 90
# description: proxyexec 
# processname: proxyexec
# config: 

# Source function library.
. /etc/rc.d/init.d/functions

prog=/usr/sbin/proxyexec
pidfile=/var/run/proxyexec.pid
RETVAL=0
export HOME=/root

function checkprog(){
    rs=$(pgrep proxyexec)
    if [ ! -z "$rs" ];then
       killproc $prog
    fi

}

current_user=$(whoami | grep root)
if [ -z "$current_user" ];then
        echo "Starting $prog: Script must run under root only"
        failure
        RETVAL=1
        echo
        exit $RETVAL
fi

start() {
        if [ -e "$pidfile" ];then
            echo "Service already started. Stop it first "
            failure
            return 1
        fi
        echo -n $"Starting $prog: "
        daemon --pidfile=${pidfile} $prog -q -d -s /var/lib/proxyexec/cagefs.sock/socket /bin/cagefs.server
        RETVAL=$?
        echo
        return $RETVAL
}

stop() {
    echo -n $"Stopping $prog: "
    killproc $prog
    RETVAL=$?
    if [ $RETVAL -ne 0 ]; then
        killall -s 9 proxyexec
        RETVAL=$?
    fi
    echo
    [ $RETVAL = 0 ] && rm -f ${pidfile}
}

# See how we were called.
case "$1" in
  start)
    start
    ;;
  stop)
    stop
    ;;
  restart)
    stop
    start
    ;;
  status)
    status $prog
    RETVAL=$?
  ;;
  *)
    echo $"Usage: $prog {start|stop|restart|status}"
    exit 1
esac

exit $RETVAL
