#!/bin/sh

### BEGIN INIT INFO
# Provides:          nginx
# Required-Start:    $local_fs $remote_fs $network $syslog
# Required-Stop:     $local_fs $remote_fs $network $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: starts the nginx web server
# Description:       starts nginx using start-stop-daemon
### END INIT INFO

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/sbin/nginx
NAME=nginx
DESC="NGINX"

# Include nginx defaults if available
if [ -f /etc/default/nginx ]; then
	. /etc/default/nginx
fi
http_port="${FRONTEND_HTTP_PORT:-80}"
https_port="${FRONTEND_HTTPS_PORT:-443}"
lsof="/usr/bin/lsof"


test -x $DAEMON || exit 0

set -e

. /lib/lsb/init-functions

if [ "$NGINX_ENABLED" != "yes" -a \( "$1" = "start" -o "$1" = "restart" -o "$1" = "force-reload" \) ]; then
	log_action_msg "Not starting $DESC as it is disabled in config"
	exit 0
fi

if [ -n "$NGINX_ULIMIT" ]; then
	# Can be customized via /etc/default/nginx. E.g. NGINX_ULIMIT="-n 4096" will set both hard and soft quota for number of open files.
	ulimit $NGINX_ULIMIT
fi

get_occupied_ports()
{
	"${lsof}" -t -a -i :"${http_port}" -i :"${https_port}" -sTCP:LISTEN "$@" 2>/dev/null | xargs
}

test_nginx_config() {
	if $DAEMON -t $DAEMON_OPTS >/dev/null 2>&1; then
		return 0
	else
		$DAEMON -t $DAEMON_OPTS
		exit $?
	fi
}

status_q()
{
	status_of_proc -p /var/run/$NAME.pid "$DAEMON" nginx >/dev/null 2>&1
	return $?
}

case "$1" in
	start)
		test_nginx_config
		log_daemon_msg "Starting $DESC" $NAME
		if start-stop-daemon --start --quiet --oknodo --pidfile /var/run/$NAME.pid --exec $DAEMON -- $DAEMON_OPTS ; then
			log_end_msg 0
		else
			log_end_msg 1
		fi
		;;

	stop)
		log_daemon_msg "Stopping $DESC" $NAME
		if ! status_q; then
			log_end_msg 0
			exit 0
		fi

		if start-stop-daemon --stop --quiet --oknodo --pidfile /var/run/$NAME.pid --name $NAME ; then
			pids="`get_occupied_ports -c ${NAME}`"
			if [ -n "${pids}" ]; then
				/bin/kill -s KILL ${pids} >/dev/null 2>&1
			else
				log_end_msg 0
				exit 0
			fi
			
			pids="`get_occupied_ports`"
			if [ -n "${pids}" ]; then
				# most probably process which still freezes port(s) is not nginx, anyway report
				log_action_begin_msg " ports ${http_port}/${https_port} are still not free."
				log_end_msg 1
			fi
			log_end_msg 0
		else
			log_end_msg 1
		fi
		;;

	restart|force-reload)
		test_nginx_config
		log_daemon_msg "Restarting $DESC" $NAME
		start-stop-daemon --stop --quiet --oknodo --pidfile /var/run/$NAME.pid --name $NAME || true
		sleep 1
		if start-stop-daemon --start --quiet --pidfile /var/run/$NAME.pid --exec $DAEMON -- $DAEMON_OPTS ; then
			log_end_msg 0
		else
			log_end_msg 1
		fi
		;;

	reload)
		test_nginx_config
		log_daemon_msg "Reloading $DESC configuration" $NAME
		if start-stop-daemon --stop --signal HUP --quiet --oknodo --pidfile /var/run/$NAME.pid --name $NAME ; then
			log_end_msg 0
		else
			log_end_msg 1
		fi
		;;

	configtest|testconfig)
		log_daemon_msg "Testing $DESC configuration" $NAME
		if test_nginx_config; then
			log_end_msg 0
		else
			log_end_msg 1
		fi
		;;

	status)
		status_of_proc -p /var/run/$NAME.pid "$DAEMON" nginx && exit 0 || exit $?
		;;

	upgrade)
		test_nginx_config
		log_daemon_msg "Starting new master $NAME" $NAME
		start-stop-daemon --stop --signal USR2 --quiet --pidfile /var/run/$NAME.pid --name $NAME && \
			log_end_msg 0 || echo " failed!"
		sleep 1
		if [ -f /var/run/$NAME.pid.oldbin -a -f /var/run/$NAME.pid ]; then
			log_daemon_msg "Graceful shutdown of old $NAME" $NAME
			if start-stop-daemon --stop --signal QUIT --quiet --oknodo --pidfile /var/run/$NAME.pid.oldbin --name $NAME ; then
				log_end_msg 0
			else
				log_end_msg 1
			fi
		else
			log_action_msg "$DESC upgrade failed. Attempting hard restart."
			$0 restart
		fi
		;;

	reopen)
		echo -n "Reopen $DESC log files: "
		if start-stop-daemon --stop --signal USR1 --quiet --oknodo --pidfile /var/run/$NAME.pid --exec $DAEMON ; then
			log_end_msg 0
		else
			log_end_msg 1
		fi
		;;

	*)
		log_action_msg "Usage: $NAME {start|stop|restart|reload|force-reload|status|configtest|upgrade|reopen}" >&2
		exit 1
		;;
esac

exit 0
