#!/bin/bash
#
# ${prefix}/etc/init.d/nginx
# Handles starting and stopping nginx web server.
#
# Provides:          nginx
# Required-Start:    $local_fs $remote_fs $network
# Required-Stop:     $local_fs $remote_fs $network
# Default-Start:     3 5
# Default-Stop:      0 1 2 6
# Description:       Provides nginx service

set -e
CONF_FILE="${prefix}/etc/nginx/nginx.conf"
PATH=${prefix}/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=${prefix}/bin/nginx
NAME=$(basename $DAEMON)
SCRIPTNAME=/etc/init.d/$NAME

test -x $DAEMON || exit 0

start() {
    configtest || exit 1
    echo -n "Starting $NAME ... "
    $DAEMON -c $CONF_FILE >/dev/null 2>&1  && echo "Done." || echo "is already running."
}

stop() {
    echo -n "Stopping $NAME ... "
    $DAEMON -c $CONF_FILE -s quit >/dev/null 2>&1 && echo "Done." || echo "is not running."
}

reload() {
    configtest || exit 1
    echo -n "Reloading $NAME configuration ... "
    $DAEMON -c $CONF_FILE -s reload >/dev/null 2>&1 && echo "Done."|| echo "could not reload."
}

status() {
    configtest || exit 1
    echo -n "Status $NAME ... "
    pidof $NAME >/dev/null 2>&1 && echo "is running" || echo "is not running."
}

configtest() {
    $DAEMON -t -c $CONF_FILE
}

case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    reload)
        reload
        ;;
    restart)
        stop
        sleep 2
        start
        ;;
    status)
        status
        ;;
    test)
        configtest
        ;;
    *)
        echo "Usage: $SCRIPTNAME {start|stop|restart|reload|status|test}" >&2
        exit 1
        ;;
esac
exit 0



