#! /bin/sh
### BEGIN INIT INFO
# Provides:          pids.rb
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start pids.rb
# Description:       Start and daemonize pids.rb for prod use.
### END INIT INFO

# On Debian install with:
#  * cp examples/pids.init.d.sh /etc/init.d/pids
#  * update-rc.d pids defaults

# PATH should only include /usr/* if it runs after the mountnfs.sh script
PATH=/sbin:/usr/sbin:/bin:/usr/bin
NAME=pids
DESC="$NAME init script"
# TODO: change path
DAEMON=/PATH/TO/SPONGES/FILE/$NAME.rb
# TODO: change user
APP_USER=CHANGE_ME
# TODO: ruby env file path will be sourced by $APP_USER
# for bundler and ruby env loading
PROFILE=.profile
SCRIPTNAME=/etc/init.d/$NAME

# Read configuration variable file if it is present
[ -r /etc/default/$NAME ] && . /etc/default/$NAME

# 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()
{
	su -l -c "source $PROFILE; $DAEMON start -d" $APP_USER
	RETVAL="$?"
	return "$RETVAL"
}

#
# Function that stops the daemon/service
#
do_stop()
{
	su -l -c "source $PROFILE; $DAEMON stop" $APP_USER
	RETVAL="$?"
	return "$RETVAL"
}

#
# Function that restarts the daemon/service
#
do_restart()
{
	su -l -c "source $PROFILE; $DAEMON restart" $APP_USER
	RETVAL="$?"
	return "$RETVAL"
}

case "$1" in
  start)
	[ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME"
	do_start
	case "$?" in
		0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
		2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
	esac
	;;
  stop)
	[ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME"
	do_stop
	case "$?" in
		0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
		2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
	esac
	;;
  status)
	status_of_proc "${NAME}_supervisor" && exit 0 || exit $?
	;;
  restart|force-reload)
	#
	# If the "reload" option is implemented then remove the
	# 'force-reload' alias
	#
	log_daemon_msg "Restarting $DESC" "$NAME"
	do_restart
	;;
  *)
	echo "Usage: $SCRIPTNAME {start|stop|status|restart|force-reload}" >&2
	exit 3
	;;
esac

:
