#! /bin/bash
#
# nginx          Start/Stop the nginx daemon.
#
# chkconfig: 2345 90 60
# description: nginx
# processname: nginx
# config: /usr/local/nginx/conf/nginx.conf
# pidfile: /usr/local/nginx/logs/nginx.pid

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

# Progran name
prog="nginx"

start() {
	echo -n $"Starting $prog: "
        if [ -e /var/lock/subsys/nginx ]; then
	    if [ -e /usr/local/nginx/logs/nginx.pid ] && [ -e /proc/`cat /usr/local/nginx/logs/nginx.pid` ]; then
		echo -n $"cannot start $prog: nginx is already running."
		failure $"cannot start $prog: nginx is already running."
		echo
		return 1
	    fi
	fi
	/usr/local/nginx/sbin/nginx
	RETVAL=$?
	[ $RETVAL -eq 0 ] && success $"$prog start" || failure $"$prog start"
	[ $RETVAL -eq 0 ] && touch /var/lock/subsys/nginx
	echo
	return $RETVAL
}

stop() {
	echo -n $"Stopping $prog: "
        if [ ! -e /var/lock/subsys/nginx ] || [ ! -e /usr/local/nginx/logs/nginx.pid ]; then
	    echo -n $"cannot stop $prog: nginx is not running."
	    failure $"cannot stop $prog: nginx is not running."
	    echo
	    return 1
	fi
	PID=`cat /usr/local/nginx/logs/nginx.pid`
	if checkpid $PID 2>&1; then
	    # TERM first, then KILL if not dead
	    kill -TERM $PID >/dev/null 2>&1
	    usleep 100000
	    if checkpid $PID && sleep 1 && checkpid $PID && sleep 3 && checkpid $PID; then
		kill -KILL $PID >/dev/null 2>&1
		usleep 100000
	    fi
	fi
	checkpid $PID
	RETVAL=$((! $?))
	[ $RETVAL -eq 0 ] && success $"$prog shutdown" || failure $"$prog shutdown"
        [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/nginx;
	echo
	return $RETVAL
}

status() {
	status $prog
}

restart() {
  	stop
	start
}

reload() {
	echo -n $"Reloading $prog: "
	if [ ! -e /var/lock/subsys/nginx ] || [ ! -e /usr/local/nginx/logs/nginx.pid ]; then
	    echo -n $"cannot reload $prog: nginx is not running."
	    failure $"cannot reload $prog: nginx is not running."
	    echo
	    return 1
	fi
	kill -HUP `cat /usr/local/nginx/logs/nginx.pid` >/dev/null 2>&1
	RETVAL=$?
	[ $RETVAL -eq 0 ] && success $"$prog reload" || failure $"$prog reload"
	echo
	return $RETVAL
}

case "$1" in
  start)
  	start
	;;
  stop)
  	stop
	;;
  restart)
  	restart
	;;
  reload)
  	reload
	;;
  status)
  	status
	;;
  condrestart)
  	[ -f /var/lock/subsys/nginx ] && restart || :
	;;
  configtest)
	/usr/local/nginx/sbin/nginx -t
	;;
  *)
	echo $"Usage: $0 {start|stop|status|reload|restart|condrestart|configtest}"
	exit 1
esac
