#!/bin/bash
#
# xray-agent        Startup script for the X Ray Agent Service
#
# chkconfig: - 99 01
# description: xray-agent - X Ray Agent Service
# processname: xray-agent


DAEMON=/usr/sbin/cloudlinux-xray-agent
DAEMON_NAME=xray-agent
DAEMON_USER=root

DIR=/opt/alt/php-xray/run
DAEMON_SOCKET=$DIR/xray.sock
LOCK=$DIR/$DAEMON_NAME.lock
PIDFILE=$DIR/$DAEMON_NAME.pid
RETVAL=0


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

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

do_start() {
    if [ -f $LOCK ]
        then
            echo "$DAEMON_NAME is already started!"
            RETVAL=1;
    else
        echo -n $"Starting $DAEMON_NAME: "
        nohup ${DAEMON} >/dev/null 2>&1 &
        RETVAL=$?
        [ $RETVAL -eq 0 ] && touch ${LOCK}
        echo $"started."
    fi
}

do_stop() {
    echo -n $"Stopping $DAEMON_NAME: "
    killproc $DAEMON
    RETVAL=$?
    echo
    [ $RETVAL = 0 ] && rm -f ${DAEMON_SOCKET} && rm -f ${LOCK}
    echo $"stopped."
}

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

exit $RETVAL
