#!/bin/sh
### Copyright 1999-2017. Plesk International GmbH. All rights reserved.
#
# nginx		   Startup script for nginx
#
# chkconfig: - 85 15
# processname: nginx
# config: /etc/nginx/nginx.conf
# config: /etc/sysconfig/nginx
# pidfile: /var/run/nginx.pid
# description: nginx is a HTTP and reverse proxy server
#
### BEGIN INIT INFO
# Provides: nginx
# Required-Start: $local_fs $remote_fs $network
# Required-Stop: $local_fs $remote_fs $network
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: start and stop nginx
### END INIT INFO

# Source function library.
. /etc/rc.d/init.d/functions

CONFFILE="/etc/nginx/nginx.conf"

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

nginx=${NGINX-/usr/sbin/nginx}
conffile=${CONFFILE-/etc/nginx/nginx.conf}
lockfile=${LOCKFILE-/var/lock/nginx.lock}
pidfile=${PIDFILE-/var/run/nginx.pid}
http_port="${FRONTEND_HTTP_PORT:-80}"
https_port="${FRONTEND_HTTPS_PORT:-443}"
prog=$(basename $nginx)
lsof="/usr/sbin/lsof"
SLEEPMSEC=1000000
RETVAL=0

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

if [ -n "$NGINX_ULIMIT" ]; then
	# Can be customized via /etc/sysconfig/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
}

start() {
	[ -x $nginx ] || exit 5
	[ -f $conffile ] || exit 6

	echo -n $"Starting $prog: "

	daemon --pidfile=${pidfile} ${nginx} -c ${conffile}
	RETVAL=$?
	echo
	[ $RETVAL = 0 ] && touch ${lockfile}
	return $RETVAL
}

stop() {
	echo -n $"Stopping $prog: "
	killproc -p ${pidfile} ${prog}
	RETVAL=$?
	[ $RETVAL -eq 0 ] || return $RETVAL

	# do not trust that nginx is really stopped, need to check occupied ports
	# for example main process is not killed by TERM signal, but killed by KILL
	# in such case worker is still uses 80 port, but service status is stopped

	# perform hard stop
	local pids="`get_occupied_ports -c ${prog}`"
	if [ -n "${pids}" ]; then
		/bin/kill -s KILL ${pids} >/dev/null 2>&1
	else
		return 0
	fi

	pids="`get_occupied_ports`"
	if [ -n "${pids}" ]; then
		# most probably process which still freezes port(s) is not nginx, anyway report
		echo "ERROR: ports ${http_port}/${https_port} are still not free."
		RETVAL=1;
	else
		rm -f ${lockfile} ${pidfile}
	fi

	return $RETVAL
}

restart() {
	configtest -q || exit 6
	stop
	start
}

reload() {
	echo -n $"Reloading $prog: "
	killproc -p ${pidfile} ${prog} -HUP
	RETVAL=$?
	echo
}

reopen() {
	echo -n $"Reopen log files of $prog: "
	killproc -p ${pidfile} ${prog} -USR1
	RETVAL=$?
	echo
}

upgrade() {
	local oldbinpidfile=${pidfile}.oldbin

	configtest -q || return 6
	echo -n $"Staring new master $prog: "
	killproc -p ${pidfile} ${prog} -USR2
	RETVAL=$?
	echo
	/bin/usleep $SLEEPMSEC
	if [ -f ${oldbinpidfile} -a -f ${pidfile} ]; then
		echo -n $"Graceful shutdown of old $prog: "
		killproc -p ${oldbinpidfile} ${prog} -QUIT
		RETVAL=$?
		echo
	else
		echo $"NGINX upgrade failed! Attempting hard restart."
		$0 restart
		RETVAL=$?
	fi
}

configtest() {
	if [ "$#" -ne 0 ] ; then
		case "$1" in
			-q)
				FLAG=$1
				;;
			*)
				;;
		esac
		shift
	fi
	${nginx} -t -c ${conffile} $FLAG
	RETVAL=$?
	return $RETVAL
}

rh_status() {
	status -p ${pidfile} ${nginx}
}

rh_status_q() {
	rh_status >/dev/null 2>&1
}

# See how we were called.
case "$1" in
	start)
		rh_status_q && exit 0
		start
		;;
	stop)
		rh_status_q || exit 0
		stop
		;;
	status)
		rh_status
		RETVAL=$?
		;;
	restart)
		restart
		;;
	upgrade)
		rh_status_q || exit 7
		upgrade
		;;
	reopen)
		reopen
		;;
	condrestart|try-restart)
		rh_status_q || exit 7
		restart
		;;
	force-reload|reload)
		configtest -q || exit 6
		reload
		;;
	configtest)
		configtest
		;;
	*)
		echo $"Usage: $prog {start|stop|restart|condrestart|try-restart|force-reload|upgrade|reload|status|help|configtest|reopen}"
		RETVAL=2
esac

exit $RETVAL

