#! /bin/sh
### BEGIN INIT INFO
# Provides: OpenSCADA
# Required-Start:
# Should-Start:
# Required-Stop:
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: OpenSCADA
# Description: Start OpenSCADA
### END INIT INFO

PATH=/sbin:/usr/sbin:/bin:/usr/bin
DESC="openscada"
NAME=openscada
DAEMON=/usr/bin/$NAME
DAEMON_ARGS="--demon"
PIDFILE=/var/run/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME

# Exit if the package is not installed
[ -x "$DAEMON" ] || exit 0

# Load the VERBOSE setting and other rcS variables
. /lib/init/vars.sh

# Define LSB log_* functions.
# Depend on lsb-base (>= 3.2-14) to ensure that this file is present
# and status_of_proc is working.
. /lib/lsb/init-functions

#
# Function that starts the daemon/service
#
do_start()
{
	export LANG=en_US.UTF-8
	export LANGUAGE=en_US.UTF-8
	export LC_ALL=en_US.UTF-8
	# Return
	#   0 if daemon has been started
	#   1 if daemon was already running
	#   2 if daemon could not be started
	start-stop-daemon --start --make-pidfile --pidfile $PIDFILE --exec $DAEMON --test > /dev/null \
		|| return 1
	start-stop-daemon --start --make-pidfile --pidfile $PIDFILE --exec $DAEMON -- \
		$DAEMON_ARGS \
		|| return 2
	pidproc=$(pidof $NAME)
	if [ $pidproc ]; then
	echo "$pidproc" > $PIDFILE
	fi
}

#
# Function that stops the daemon/service
#
do_stop()
{
	# Return
	#   0 if daemon has been stopped
	#   1 if daemon was already stopped
	#   2 if daemon could not be stopped
	#   other if a failure occurred
	start-stop-daemon --stop --retry=TERM/30/KILL/5 --pidfile $PIDFILE --name $NAME
	RETVAL="$?"
	if [ "$RETVAL" -ne 0 ]; then
	start-stop-daemon --stop --oknodo --retry=0/30/KILL/5 --exec $DAEMON
	RETVAL="$?"
	fi
	rm -f $PIDFILE
	return "$RETVAL"
}

case "$1" in
  start)
	log_daemon_msg "Starting $DESC"
	do_start
	;;
  stop)
	log_daemon_msg "Stopping $DESC"
	do_stop
	;;
  status)
       status_of_proc "$DAEMON" "$NAME" && exit 0 || exit $?
       ;;
  restart|reload|force-reload)
	log_daemon_msg "Restarting $DESC"
	do_stop
	do_start
	;;
  *)
	echo "Usage: $SCRIPTNAME {start|stop|status|restart|force-reload}"
	exit 3
	;;
esac

:
