#!/bin/bash
### BEGIN INIT INFO
# If you wish the Daemon to be lauched at boot / stopped at shutdown :
#
#    On Debian-based distributions:
#      INSTALL : update-rc.d scriptname defaults
#      (UNINSTALL : update-rc.d -f  scriptname remove)
#
#    On RedHat-based distributions (CentOS, OpenSUSE...):
#      INSTALL : chkconfig --level 35 scriptname on
#      (UNINSTALL : chkconfig --level 35 scriptname off)
#
# chkconfig:         2345 90 60
# Provides:          epson-edge-dashboard
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: running /usr/sbin/Epson/EpsonEdgeDashboard/bin/node
# Description:       /usr/sbin/Epson/EpsonEdgeDashboard/scripts/bin/www.js
### END INIT INFO
#
# initd a node app
# Based on a script posted by https://gist.github.com/jinze at https://gist.github.com/3748766
#

if [ -e /lib/lsb/init-functions ]; then
	# LSB source function library.
	. /lib/lsb/init-functions
fi;

# PATH should only include /usr/* if it runs after the mountnfs.sh script
PATH="/sbin":"/usr/sbin":"/bin":"/usr/bin":"/usr/local/bin":"/usr/sbin/Epson/EpsonEdgeDashboard/bin"
EXEC=node                          # Introduce a executable 
NAME=epson-edge-dashboard             # Introduce the short server's name here
DAEMON="/usr/sbin/Epson/EpsonEdgeDashboard/bin/node" # Introduce the server's location here
DAEMON_PATH="/usr/sbin/Epson/EpsonEdgeDashboard/scripts"
DAEMON_ARGS="/usr/sbin/Epson/EpsonEdgeDashboard/scripts/bin/www.js"             # Arguments to run the daemon with
PIDFILE=/var/run/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME

start() {
	echo "Starting $NAME"
	# Return
	#   0 if daemon has been started
	#   1 if daemon was already running
	#   2 if daemon could not be started
	start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON --test > /dev/null \
		|| return 1
	start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON --chdir $DAEMON_PATH -b -- \
		$DAEMON_ARGS \
		|| return 2
	# Add code here, if necessary, that waits for the process to be ready
	# to handle requests from services started subsequently which depend
	# on this one.  As a last resort, sleep for some time.
}

restart() {
	echo -n "Restarting $NAME"
	log_daemon_msg "Restarting $EXEC" "$NAME"
	stop
	case "$?" in
	  0|1)
		start
		case "$?" in
			0) log_end_msg 0 ;;
			1) log_end_msg 1 ;; # Old process is still running
			*) log_end_msg 1 ;; # Failed to start
		esac
		;;
	  *)
		# Failed to stop
		log_end_msg 1
		;;
	esac
}

stop() {
	echo -n "Shutting down $NAME"
	# 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 --quiet --retry=TERM/30/KILL/5 --pidfile $PIDFILE --name $NAME
	start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 --name $EXEC
	RETVAL="$?"
	[ "$RETVAL" = 2 ] && return 2
	# Wait for children to finish too if this is a daemon that forks
	# and if the daemon is only ever run from this initscript.
	# If the above conditions are not satisfied then add some other code
	# that waits for the process to drop all resources that could be
	# needed by services started subsequently.  A last resort is to
	# sleep for some time.
	#start-stop-daemon --stop --quiet --oknodo --retry=0/30/KILL/5 --exec $NAME
	#[ "$?" = 2 ] && return 2
	# Many daemons don't delete their pidfiles when they exit.
	rm -f $PIDFILE
	return "$RETVAL"
}

case "$1" in
   start)
        start
        ;;
    stop)
        stop
        ;;
   status)
       echo -n "Status $NAME"
       status_of_proc "$DAEMON" "$NAME" && exit 0 || exit $?
       ;;
   restart)
   	restart
        ;;
	*)
       echo "Usage:  {start|stop|status|restart}"
       exit 1
        ;;
esac
exit $RETVAL
