#!/bin/bash
#
# lightstreamer    Lightstreamer Server
#
# chkconfig: - 85 15
# description: Lightstreamer Server, http://www.lightstreamer.com
#
### BEGIN INIT INFO
# Provides:          Lightstreamer
# Required-Start:    $network
# Required-Stop:     $network
# Should-Start:
# Should-Stop:
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Lightstreamer Server
# Description:       Lightstreamer Server, Real-Time Data Push - http://www.lightstreamer.com
### END INIT INFO

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

# Lightstreamer specific settings
# LS_HOME=/opt/Lightstreamer
LS_HOME=%LS_HOME%
# LS_USER=lightstreamer
LS_USER=%LS_USER%
# LS_GROUP=lightstreamer
LS_GROUP=%LS_GROUP%

DAEMON="${LS_HOME}/bin/unix-like/LS.sh"
PIDFILE="/var/run/lightstreamer.pid"
PROG="lightstreamer"

RETVAL=0
STOP_TIMEOUT=${STOP_TIMEOUT-10}

# For SELinux we need to use 'runuser' not 'su'
if [ -x "/sbin/runuser" ]; then
    SU="/sbin/runuser"
else
    SU="/bin/su"
fi

ls_start() {
	echo $"Starting Lightstreamer"
  $SU -s /bin/bash $LS_USER -c " ${DAEMON} run" > /dev/null &
	PID=$!
	RETVAL=$?
        [ $RETVAL -eq 0 ] && {
		touch /var/lock/subsys/lightstreamer;
		echo "${PID}" > "${PIDFILE}";
	}
}

ls_stop() {
	echo $"Stopping Lightstreamer"
	killproc -p "${PIDFILE}" -d "${STOP_TIMEOUT}" "${DAEMON}"
	RETVAL=$?
        [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/lightstreamer
}

case "$1" in
  start)
	ls_start
        ;;
  stop)
	ls_stop
        ;;
  restart)
	ls_stop && ls_start
	RETVAL=$?
	;;
  status)
    status -p "${PIDFILE}" "${PROG}"
	;;
 *)
        echo $"Usage: $0 {start|stop|restart|status}"
        exit 1
        ;;
esac

exit ${RETVAL}
