#!/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

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

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

case "$1" in
	start)
		test_nginx_config
		log_daemon_msg "Starting $DESC" $NAME
		# Check if the ULIMIT is set in /etc/default/nginx
		if [ -n "$ULIMIT" ]; then
			# Set the ulimits
			ulimit $ULIMIT
		fi
		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 start-stop-daemon --stop --quiet --oknodo --pidfile /var/run/$NAME.pid --exec $DAEMON ; then
			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 --exec $DAEMON || 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 --exec $DAEMON ; 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 --exec $DAEMON && \
			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 --exec $DAEMON ; 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
