#!/bin/bash
#
# nginx - starts and stops the nginx daemon
#
# BFW-SPEC
# chkconfig: 2 85 15
# description: starts and stops the nginx daemon

. /etc/rc.d/init.d/functions

NGINX="/usr/local/nginx/sbin/nginx"
prog=$(basename $NGINX)
NGINX_CONF_FILE="/home/product/code/config/nginx.conf"
NGINX_NOSSL_CONF_FILE="/home/product/code/config/nginx_nossl.conf"
lockfile=/var/lock/subsys/nginx
PRODUCT="bfw"
UPLOAD_DIR=/tmp/nginx_uploads

export LD_LIBRARY_PATH=/usr/lib64:/home/product/code/firmware/current/lib


set -a
[ -f /etc/phion/phion.sh ] &&  source /etc/phion/phion.sh
set +a



if [ $PRODUCT == 'bfw' ]; then
    # IP address changes are not immediate on the Barracuda Firewall
    # IP changes result in a "pending" conf file and the network 
    # stack manipulates "active" config & reloads nginx when ready
    NGINX_CONF_FILE="/home/product/code/config/nginx.conf.active"
fi

function croak()
{
    local CALLING_LINENO="$1"
    local MESSAGE="$2"
    local ERRCODE="${3:-1}"
    test -t 1 && tput setf 4
    if [[ -n "$MESSAGE" ]]; then
        echo "$prog [$CALLING_LINENO]: ERROR - $MESSAGE; exit status $ERRCODE"
    else
        echo "$prog [$CALLING_LINENO]: ERROR - exit status $ERRCODE"
    fi

    test -t 1 && tput sgr0
    exit $ERRCODE
}

function configtest()
{
    $NGINX -t -c $NGINX_CONF_FILE
}

function configtest_nossl()
{
    $NGINX -t -c $NGINX_NOSSL_CONF_FILE
}

function start()
{
    # webui-ng
    # if box.conf/ismanagedbywebui = y -> don't start
    iswebui=$(/opt/phion/bin/boxinfo config /opt/phion/config/configroot/box.conf / ismanagedbywebui)
    if [ x"$iswebui" != x"y" ]; then
    	echo "Warning: nginx start aborted because this box isn't managed by webui."
    	exit 0 
    fi

    # BFW-SPEC
    # tprochaska: BNF-6151: wait until gui migrations have finished before starting up
    /opt/phion/bin/webuicmd lock waitfor bfw/premigration 1800 # 30 minutes
    [ -x $NGINX ] || croak ${LINENO} "$NGINX not found" 5
    [ -f $NGINX_CONF_FILE ] || croak ${LINENO} "$NGINX_CONF_FILE not found" 6

    #ensure nginx upload dirs exist
    mkdir -p /mail/log/nginx
    for subdir in $(seq 0 9); do
        mkdir -p $UPLOAD_DIR/$subdir
    done
    chmod -R a+rwx $UPLOAD_DIR

    echo -n $"Starting $prog: "
    daemon $NGINX -c $NGINX_CONF_FILE
    retval=$?
	if [ $retval -ne 0 ]; then
		echo
		echo "Failed to start with valid SSL configuration - attempting to start without SSL"
   		daemon $NGINX -c $NGINX_NOSSL_CONF_FILE
    	retval=$?
	fi
        echo
    [ $retval -eq 0 ] && touch $lockfile

    return $retval
}

get_nginx_pids()
{
	pgrep -f nginx:
}

function stop()
{
    echo -n $"Stopping $prog: "
    killproc $prog -QUIT
	echo
	sleep 1
	# give 5 seconds to shut down before we get forceful
	retval=1
	i=5
	while [ $((i--)) -ne 0 ] ; do
		pids="$(get_nginx_pids)"
		if [ "$pids" == "" ]; then
			retval=0
			break
		fi
		echo -n "."
		sleep 1
	done 

	# if we still have any process lingering, be forceful
	if [ $retval -eq 1 ]; then
		echo "Manually killing nginx"
		pkill -9 -f nginx:

		#in case anyone cares, see if we've really terminated it
		sleep 1
		pids="$(get_nginx_pids)"
		if [ "$pids" == "" ]; then
			retval=0
		fi
	fi

    echo
    [ $retval -eq 0 ] && rm -f $lockfile
    return $retval
}

function restart()
{
    if configtest; then
        stop
        start
    else
        croak ${LINENO} "Configtest failed: $?"
    fi
}

function reload()
{
    configtest || croak ${LINENO} "Configtest failed: $?"
    echo -n $"Reloading $prog: "
    pkill $prog -HUP
    RETVAL=$?
    echo
}

case "$1" in
    start)
        $1
    ;;
    stop)
        $1
    ;;
    restart|configtest)
        $1
    ;;
    reload)
        $1
    ;;
    *)
        echo $"Usage: $0 {start|stop|restart|reload|configtest}"
        exit 2
    ;;
esac

