#!/bin/bash
### BEGIN INIT INFO
# Provides:          uwsgi-init
# Required-Start:    $syslog $remote_fs
# Should-Start: $time
# Required-Stop:     $syslog $remote_fs
# Should-Stop: $time 
# Default-Start:     3 5
# Default-Stop:      0 1 2 6
# Short-Description: uwsgi-init
# Description:       uwsgi-init
### END INIT INFO

export SYSTEMD_NO_WRAP="1"

if [ -f /etc/rc.status ] ; then
    . /etc/rc.status
else
    . /etc/rc.status_suse
fi

if [ -f /etc/redhat-release ] ; then
    export RH=1
    . /etc/init.d/functions
else
    export RH=0
fi

# check for debian
if [ -f /etc/debian_version ] ; then
    export DB=1
else
    export DB=0
fi

PID_FILE=/var/run/uwsgi-init.pid
UWSGI_BIN=/usr/bin/uwsgi-init
LOG_FILE=/var/log/uwsgi-emperor.log
UWSGI_OPTIONS="--py-programname=python-init --emperor /opt/cluster/etc/uwsgi --pidfile ${PID_FILE} -d ${LOG_FILE} --log-syslog"
rc_reset

case "$1" in
    start)
        echo -n "Starting uwsgi-init "
        if [ "${RH}" = "1" ] ; then
            daemon ${UWSGI_BIN} ${UWSGI_OPTIONS}
        elif [ "${DB}" = "1" ] ; then
            ${UWSGI_BIN} ${UWSGI_OPTIONS}
        else
            ${UWSGI_BIN} ${UWSGI_OPTIONS}
        fi

        rc_status -v
        ;;
    stop)
        echo -n "Shutting down uwsgi-init "
        if [ -f ${PID_FILE} ] ; then
            kill -INT $(cat ${PID_FILE})
        else
            echo "not running"
            rc_failed
        fi
        rc_status -v
        ;;
    restart)
        $0 stop
        if [ -f ${PID_FILE} ] ; then
            master_pid=$(cat ${PID_FILE})
            echo -n "waiting for process ${master_pid} to vanish "
            for i in $(seq 40) ; do
                echo -n "."
                if [ -x /bin/usleep ] ; then
                    usleep 500000
                else
                    sleep 1
                fi
                ps -c ${master_pid} > /dev/null || break
            done
            ps -c ${master_pid} > /dev/null && {
                echo "still there"
            } || {
                echo "gone"
            }
            rm -f ${PID_FILE}
        fi
        $0 start
    
        # Remember status and be quiet
        rc_status
        ;;
    reload)
        echo -n "Reloading uwsgi-init "
        if [ -f ${PID_FILE} ] ; then
        #    kill -HUP $(cat ${PID_FILE})
            echo
            echo "SORRY, RELOADING UWSGI EMPEROR VIA INIT-SCRIPT CURRENTLY NOT SUPPORTED."
            echo "To reload an instance with chain-reload (little downtime, if supported by the instance):"
            echo "  touch /opt/cluster/etc/uwsgi/reload/<<instance>>.touch"
            echo "To reload an instance the hard way (possibly longer downtime):"
            echo "  touch /opt/cluster/etc/uwsgi/<<instancename>>.ini"
            exit 1
        else
            echo "not running"
            rc_failed
        fi
        rc_status -v
        ;;
    status)
        echo -n "Checking for service uwsgi-init "
        if [ "${RH}" = "1" ] ; then
            status $UWSGI_BIN
        elif [ "${DB}" = "1" ] ; then
            if [ -f ${PID_FILE} ] ; then
                true
            else
                rc_failed
            fi  
        else
            /sbin/checkproc $UWSGI_BIN
        fi
        rc_status -v
        ;;
    *)
        echo "Usage: $0 {start|stop|status|restart|reload|hard-reload}"
        exit 1
        ;;
esac

rc_exit

