#! /bin/sh
### BEGIN INIT INFO
# Provides:          httpstorm
# Required-Start:    $local_fs $remote_fs $network $syslog $named
# Required-Stop:     $local_fs $remote_fs $network $syslog $named
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: httpstorm web server (C) Georgi Valkov
# Description:       Start the web server
#  This script will start the httpstorm web server.
### END INIT INFO

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DESC="httpstorm web server"
APP="httpstorm"
NAME="httpstorm"

DAEMON=/usr/sbin/$APP
PARAM=-d:$NAME

PIDFILE=/run/$APP/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME

# Gracefully exit if the package has been removed
test -x $DAEMON || exit 0


use_internal_api()
{
	# not Ubuntu
	alias log_daemon_msg=echo
	alias log_progress_msg=echo
	alias log_end_msg=echo
	alias status_of_proc=echo
	alias start-stop-daemon=start_stop_daemon_alt
}

# Ubuntu
test -f /lib/lsb/init-functions && . /lib/lsb/init-functions ||
{
	# CentOS
	test -f /etc/sysconfig/network && . /etc/sysconfig/network
	test -f /etc/init.d/functions && . /etc/init.d/functions

	use_internal_api
}

test -z "$(type -t log_daemon_msg)" && use_internal_api


# run daemon configuration script
test -f /etc/default/$APP && . /etc/default/$APP

DAEMON_OPTS=$PARAM $DAEMON_OPTS


#
#	start-stop-daemon
#
start_stop_daemon_alt()
{
	local ACTION=
	local DAEMON=
	local DAEMON_OPTS=
	local PIDFILE=
	local SIGNAL=INT
	local RETRY=0
	local QUIET=

	while [ "$1" != "${1##[-+]}" ]; do
		case "$1" in
			--start)
				ACTION=$1
				shift
			;;

			--stop)
				ACTION=$1
				shift
			;;

			--exec)
				DAEMON=$2
				shift 2
			;;

			--)
				shift
				DAEMON_OPTS=$*
				shift
				break
			;;

			--signal)
				SIGNAL=$2
				shift 2
			;;

			--pidfile)
				PIDFILE=$2
				shift 2
			;;

			--retry)
				RETRY=$2
				shift 2
			;;

			--quiet)
				QUIET=1
				shift
			;;

			*)
				shift
			;;
		esac
	done

	case "$ACTION" in
		--start)
			[ ! -z $DAEMON ] && [ -x $DAEMON ] &&
			{
				$DAEMON $DAEMON_OPTS
			} ||
			{
				[ -z "$QUIET" ] && echo "\$DAEMON: " $DAEMON " not found or not executable"
			}
		;;

		--stop)
			[ ! -z $PIDFILE ] && [ -r $PIDFILE ] &&
			{
				kill -s $SIGNAL $( cat $PIDFILE )
			} ||
			{
				[ -z "$QUIET" ] && echo "\$PIDFILE: " $PIDFILE " invalid or not found"
			}
		;;

		*)
			[ -z "$QUIET" ] && echo "\$ACTION: " $ACTION " not valid"
		;;
	esac
}

#
#	Function that starts the daemon/service
#
d_start()
{
	start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON -- $DAEMON_OPTS
}

#
#	Function that stops the daemon/service
#
d_stop()
{
	start-stop-daemon --stop --signal INT --quiet --pidfile $PIDFILE --exec $DAEMON --retry 5
}

#
#	Function that reloads the configuration of the daemon/service
#
d_reload()
{
	start-stop-daemon --stop --signal HUP --quiet --pidfile $PIDFILE --exec $DAEMON
}

#
#	Function that pauses the daemon/service
#
d_pause()
{
	start-stop-daemon --stop --signal STOP --quiet --pidfile $PIDFILE --exec $DAEMON
}

#
#	Function that continues the daemon/service
#
d_continue()
{
	start-stop-daemon --stop --signal CONT --quiet --pidfile $PIDFILE --exec $DAEMON
}

#
#	Function that enables the daemon/service
#
d_enable()
{
	systemctl enable $NAME
}

#
#	Function that disables the daemon/service
#
d_disable()
{
	systemctl disable $NAME
	rm -f /etc/rc?.d/K??$NAME
}


#
#	Process command
#
case "$1" in
  start)
	log_daemon_msg "Starting $DESC" "$NAME"
	d_start
	case "$?" in
		0) log_end_msg 0 ;;
		1) log_progress_msg "already started"
		   log_end_msg 0 ;;
		*) log_end_msg 1 ;;
	esac
	;;
  stop)
	log_daemon_msg "Stopping $DESC" "$NAME"
	d_stop
	case "$?" in
		0) log_end_msg 0 ;;
		1) log_progress_msg "already stopped"
		   log_end_msg 0 ;;
		*) log_end_msg 1 ;;
	esac
	;;
  reload|force-reload)
	log_daemon_msg "Reloading $DESC" "$NAME"
	d_reload
	log_end_msg $?
	;;
  pause)
	log_daemon_msg "Pausing $DESC" "$NAME"
	d_pause
	log_end_msg $?
	;;
  continue|cont|resume)
	log_daemon_msg "Continuing $DESC" "$NAME"
	d_continue
	log_end_msg $?
	;;
  restart)
	$0 stop
	$0 start
	;;
  enable)
	log_daemon_msg "Enabling $DESC" "$NAME"
	d_enable
	log_end_msg $?
	;;
  disable|delete)
	log_daemon_msg "Disabling $DESC" "$NAME"
	d_disable
	log_end_msg $?
	;;
  status)
	status_of_proc -p $PIDFILE $DAEMON $NAME && exit 0 || exit $?
	;;
  *)
	echo "Usage: $SCRIPTNAME {start|stop|restart|reload|force-reload|pause|continue|enable|disable|delete|status}" >&2
	exit 1
	;;
esac

exit 0
