#!/bin/sh
### BEGIN INIT INFO
# Provides:          h2o
# Required-Start:    $local_fs $network $remote_fs $syslog $named
# Required-Stop:     $local_fs $network $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Optimized HTTP/1.x, HTTP/2 server
# Description:       Manages the h2o daemon, the optimized HTTP/1.x and HTTP/2
#                    server.
### END INIT INFO

# Author: PICCORO Lenz McKAY <mckaygerhard@gmail.com>

# Do NOT "set -e"

# PATH should only include /usr/* if it runs after the mountnfs.sh script
PATH=/sbin:/usr/sbin:/bin:/usr/bin
DESC="Powered VenenuX HTTP 1/2 server"
NAME=h2o
CONFIG=/etc/h2o/h2o.conf
DAEMON=/usr/sbin/h2o
SCRIPTNAME=/etc/init.d/$NAME

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

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

DAEMON_ARGS="-m daemon -c $CONFIG"
PIDFILE=`sed -ne 's|pid-file:\s*\([-_./0-9a-zA-Z]\{1,\}\)|\1|p' $CONFIG`
if [ -z "$PIDFILE" ]; then
	echo "pid-file must be defined in $CONFIG"
	exit 1
fi

# 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()
{
	# the real daemon start by a perl implementation so we trap the return as:
	#   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 --name $NAME -- \
		$DAEMON_ARGS \
		|| return 2
}

#
# Function that stops the daemon/service
#
do_stop()
{
	# the starting of the server is xplained here https://github.com/h2o/h2o/issues/84#issuecomment-68315426
	# is a perl process in fact..  is a set of superdaemon program (named start_server) 
	# and a support library written in Perl, for hot-deploying TCP servers, 
	# so the perl process is the real program to kill but only with pid reported by h2o:
	start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 --pidfile $PIDFILE --exec /usr/bin/perl
	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 --pidfile $PIDFILE --exec /usr/bin/perl
	[ "$?" = 2 ] && return 2
	# Many daemons don't delete their pidfiles when they exit.
	rm -f $PIDFILE
	return "$RETVAL"
}

#
# Function that checks the daemon/service configuration file
#
do_configtest() {
	$DAEMON -t -c $CONFIG
}

#
# Function that sends a SIGHUP to the daemon/service
#
do_reload() {
	# as the running h2o is started by a perl process, (from the start_server) 
	# so the perl process is the real program to send the signal:
        start-stop-daemon --oknodo --stop --signal HUP --quiet --pidfile "$PIDFILE" --exec "/usr/bin/perl"
        return "$?"
}

case "$1" in
  start)
	log_daemon_msg "Starting $DESC"
	do_start
	case "$?" in
		0|1) log_end_msg 0 ;;
		2) log_end_msg 1 ;;
	esac
	;;
  stop)
	log_daemon_msg "Stopping $DESC"
	do_stop
	case "$?" in
		0|1) log_end_msg 0 ;;
		2) log_end_msg 1 ;;
		*) log_end_msg 0 ;;
	esac
	;;
  status)
	status_of_proc ${PIDFILE:+-p ${PIDFILE}} /usr/bin/perl h2o && exit 0 || exit $?
	;;
  reload|force-reload)
	#
	# If do_reload() is not implemented then leave this commented out
	# and leave 'force-reload' as an alias for 'restart'.
	#
	log_daemon_msg "Reloading $DESC"
	if ! do_configtest >/dev/null 2>&1; then
		log_end_msg 1 # Configuration error
		exit 0
	fi
	do_reload
	log_end_msg $?
	;;
  restart)
	#
	# If the "reload" option is implemented then remove the
	# 'force-reload' alias
	#
	log_daemon_msg "Restarting $DESC"
	do_stop
	case "$?" in
	  0|1)
		do_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
	;;
  configtest)
	log_daemon_msg "Testing $DESC configuration"
	do_configtest
	log_end_msg $?
	;;
  *)
	echo "Usage: $SCRIPTNAME {start|stop|status|restart|reload|force-reload|configtest}" >&2
	exit 3
	;;
esac

:
