#!/bin/bash
#
# chkconfig:    345 31 69
# description:  nginx webserver
#
### BEGIN INIT INFO
# Provides:          nginx httpd
# Required-Start:    $syslog $network $remote_fs
# Required-Stop:     $syslog $network $remote_fs
# Default-Start:     3 5
# Default-Stop:      0 1 2 4 6
# Short-Description: nginx webserver
# Description:       nginx, a HTTP and reverse proxy server
### END INIT INFO

NGINX_BIN=/usr/sbin/nginx
NGINX_PID=/var/run/nginx.pid

if [ -r /etc/rc.status ]
then
  source /etc/rc.status
  START="/sbin/startproc"
  STATUS="/sbin/checkproc"
  SUCCESS="echo \$rc_done"
  FAILURE="echo \$rc_failed"
else
  source /etc/rc.d/init.d/functions
  START="daemon"
  STATUS="status"
  SUCCESS="success; echo"
  FAILURE="failure; echo"
fi

[ -r /etc/sysconfig/nginx ] && source /etc/sysconfig/nginx

if [ -n "${PARMS}" ]
then
  ARGS="-g ${PARMS}"
fi

RETVAL=0

case "$1" in
  start)
    echo -n "Starting nginx: "
    ${START} ${NGINX_BIN} -c ${CONF_FILE:-/etc/nginx/nginx.conf} -p ${PREFIX:-/var/www} ${ARGS} > /var/log/nginx/startup.out 2>&1
    RETVAL=$?
    ;;
  stop)
    echo -n "Stopping nginx: "
    ${NGINX_BIN} -s quit 2> /dev/null
    RETVAL=$?
    ;;
  restart)
    if $0 checkconf
    then
      $0 stop
      sleep 2
      $0 start
      unset RETVAL
    else
      exit 1
    fi
    ;;
  reload)
    if $0 checkconf
    then
      echo -n "Reloading configuration of nginx: "
      ${NGINX_BIN} -s reload 2> /dev/null
      RETVAL=$?
    else
      exit 1
    fi
    ;;
  status)
    echo -n "Status of nginx: "
    ${STATUS} ${NGINX_BIN} > /dev/null 2>&1
    RETVAL=$?
    ;;
  checkconf)
    echo -n "Checking configuration of nginx: "
    ${NGINX_BIN} -t 2> /dev/null
    RETVAL=$?
    ;;
  *)
    echo "Usage: $0 {start|stop|status|restart|reload|checkconf}"
    exit 1
    ;;
esac
[ -z ${RETVAL} ] && exit
[ ${RETVAL} -eq 0 ] && eval "${SUCCESS}" || eval "${FAILURE}"
exit ${RETVAL}
