#!/bin/sh
#
### BEGIN INIT INFO
# Provides:			 nginx
# Required-Start:	 $syslog $remote_fs $network
# Should-Start: $time ypbind sendmail
# Required-Stop:	 $syslog $remote_fs $network
# Should-Stop: $time ypbind sendmail
# Default-Start:	 3 5
# Default-Stop:		 0 1 2 6
# Short-Description: nginx
# Description:		 nginx
### END INIT INFO


# Check for missing binaries (stale symlinks should not happen)
# Note: Special treatment of stop for LSB conformance
NGINX_BIN=/usr/sbin/nginx
test -x $NGINX_BIN || { echo "$NGINX_BIN not installed"; 
		if [ "$1" = "stop" ]; then exit 0;
		else exit 5; fi; }
NGINX_PID=/var/run/nginx.pid

if [ -f /etc/sysconfig/nginx ]; then
	. /etc/sysconfig/nginx
fi

. /etc/rc.status

# Reset status of this service
rc_reset

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

test_nginx_config() {
	if $NGINX_BIN -q -t >/dev/null 2>&1; then
		return 0
	else
		rc_status -v
		$NGINX_BIN -t $DAEMON_OPTS
		rc_exit
	fi
}

# Return values acc. to LSB for all commands but status:
# 0		  - success
# 1		  - generic or unspecified error
# 2		  - invalid or excess argument(s)
# 3		  - unimplemented feature (e.g. "reload")
# 4		  - user had insufficient privileges
# 5		  - program is not installed
# 6		  - program is not configured
# 7		  - program is not running
# 8--199  - reserved (8--99 LSB, 100--149 distrib, 150--199 appl)
# 
# Note that starting an already running service, stopping
# or restarting a not-running service as well as the restart
# with force-reload (in case signaling is not supported) are
# considered a success.

case "$1" in
	start)
		echo -n "Starting nginx "
		test_nginx_config
		/sbin/startproc -p $NGINX_PID $NGINX_BIN
		rc_status -v
		;;
	stop)
		echo -n "Shutting down nginx "
		/sbin/killproc -p $NGINX_PID -TERM $NGINX_BIN
		rc_status -v
		;;
	try-restart|condrestart)
		if test "$1" = "condrestart"; then
			echo "${attn} Use try-restart ${done}(LSB)${attn} rather than condrestart ${warn}(RH)${norm}"
		fi
		$0 status
		if test $? = 0; then
			$0 restart
		else
			rc_reset		# Not running is not a failure.
		fi
		rc_status
		;;
	restart)
		test_nginx_config
		$0 stop
		$0 start
		rc_status
		;;
	reload|force-reload)
		echo -n "Reload service nginx "
		test_nginx_config
		/sbin/killproc -p $NGINX_PID -HUP $NGINX_BIN
		#touch /var/run/nginx.pid
		rc_status -v
		;;
	reopen)
		echo -n "Reopen the logfiles "
		/sbin/killproc -p $NGINX_PID -USR1 $NGINX_BIN
		rc_status -v
		;;
	upgrade)
		echo -n "Starting new master nginx process "
		test_nginx_config
		/sbin/killproc -p $NGINX_PID -USR2 $NGINX_BIN
		rc_status -v
		sleep 1
		if [ -f $NGINX_PID.oldbin -a -f $NGINX_PID ]; then
			echo -n "Graceful shutdown of old nginx "
			/sbin/killproc -p $NGINX_PID.oldbin -QUIT $NGINX_BIN
			rc_status -v
		else
			echo "Nginx upgrade failed! Attempting hard restart."
			$0 restart
			rc_status
		fi
		;;
	status)
		echo -n "Checking for service nginx "
		# Return value is slightly different for the status command:
		# 0 - service up and running
		# 1 - service dead, but /var/run/  pid	file exists
		# 2 - service dead, but /var/lock/ lock file exists
		# 3 - service not running (unused)
		# 4 - service status unknown :-(
		# 5--199 reserved (5--99 LSB, 100--149 distro, 150--199 appl.)
		/sbin/checkproc -p $NGINX_PID $NGINX_BIN
		rc_status -v
		;;
	probe)
		test /etc/nginx/nginx.conf -nt /var/run/nginx.pid && echo reload
		;;
	*)
		echo "Usage: $0 {start|stop|status|try-restart|restart|force-reload|reload|probe|upgrade|reopen}"
		exit 1
		;;
esac
rc_exit
