#!/bin/bash
#  _    _          ___           __ _     (R)
# | |  (_)_ _____ / __|___ _ _  / _(_)__ _
# | |__| \ V / -_) (__/ _ \ ' \|  _| / _` |
# |____|_|\_/\___|\___\___/_||_|_| |_\__, |
#                                    |___/
# Copyright (c) 2009-2014 Keppler IT GmbH.
# ----------------------------------------------------------------------------
# /etc/init.d/nginx-php-fcgi
# Init script for starting PHP FCGI instances with NGINX
# $Id: nginx-php-fcgi 2202 2013-03-18 18:36:44Z kk $
# ----------------------------------------------------------------------------

### BEGIN INIT INFO
# Provides:          nginx-php-fcgi
# Required-Start:    $nginx
# Required-Stop:     $nginx
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start PHP FCGI instances for NGINX
# Description:       This scripts starts all required PHP FCGI instances
#                    when using NGINX with LiveConfig
### END INIT INFO

COMMAND=$1
CONFIG=$2
PHP_CGI=/usr/bin/php-cgi
RETVAL=0

# RedHat/CentOS:
if [ -f /etc/rc.d/init.d/functions ]; then
  . /etc/rc.d/init.d/functions
fi

# check permissions
[ "$EUID" != "0" ] && echo 'You need to have root priviliges' && exit 4

# locate path with nginx vhost config files
for i in "$NGINX_CONFIGPATH" /etc/nginx/sites-enabled /etc/nginx/sites-available /etc/nginx/vhosts.d; do
    [ -n "$i" ] && [ -d "$i" ] && CONFIGPATH=$i && break
done
[ -z "$CONFIGPATH" ] && echo 'Config directory not found' && exit 1

if [ -z "$CONFIG" ]; then
    CONFIG="$CONFIGPATH/*.conf"
else
    [ ! -f "$CONFIGPATH/$CONFIG.conf" ] && echo "Configuration file $CONFIGPATH/$CONFIG.conf not found" && exit 1
    CONFIG="$CONFIGPATH/$CONFIG.conf"
fi

start_process() {
    CFGFILE=$1
    NGINX_FCGI_USER=`awk -F '=' '$1 ~ /^# NGINX_FCGI_USER/ { print $2}' $CFGFILE`
    [ -z "$NGINX_FCGI_USER" ] && return
    echo -n " $NGINX_FCGI_USER"

    # get all other options:
    NGINX_FCGI_SOCKET=`awk -F '=' '$1 ~ /^# NGINX_FCGI_SOCKET/ { print $2}' $CFGFILE`
    NGINX_FCGI_CHILDREN=`awk -F '=' '$1 ~ /^# NGINX_FCGI_CHILDREN/ { print $2}' $CFGFILE`
    NGINX_FCGI_MAX_REQUESTS=`awk -F '=' '$1 ~ /^# NGINX_FCGI_MAX_REQUESTS/ { print $2}' $CFGFILE`
    NGINX_FCGI_INI_PATH=`awk -F '=' '$1 ~ /^# NGINX_FCGI_INI_PATH/ { print $2}' $CFGFILE`
    PIDFILE=`echo -n "$NGINX_FCGI_SOCKET" | sed -e 's/\.sock$/.pid/'`

    # check if PHP-FCGI is already running for this user
    ps -u "$NGINX_FCGI_USER" -o args | grep -q "$PHP_CGI -b $NGINX_FCGI_SOCKET" && echo -n "[ALREADY_RUNNING]" && return

    # start PHP-FCGI instance for this user
    PHP_CGI_ARGS="- USER=$NGINX_FCGI_USER PATH=/usr/bin PHP_FCGI_CHILDREN=$NGINX_FCGI_CHILDREN PHP_FCGI_MAX_REQUESTS=$NGINX_FCGI_MAX_REQUESTS PHPRC=$NGINX_FCGI_INI_PATH $PHP_CGI -b $NGINX_FCGI_SOCKET"
    if [ -x /sbin/start-stop-daemon ]; then
        start-stop-daemon --quiet --start --background --make-pidfile --pidfile $PIDFILE --chuid "$NGINX_FCGI_USER" --exec /usr/bin/env -- $PHP_CGI_ARGS
    elif [ -f /etc/rc.d/init.d/functions ]; then
        # use "daemon" shell function (RedHat/CentOS)
        daemon --user "$NGINX_FCGI_USER" "/bin/env $PHP_CGI_ARGS &"
    else
        $?=1
    fi
    if [ "$?" != "0" ]; then
        echo -n "[FAILED]"
        RETVAL=1
    fi
}

stop_process() {
    CFGFILE=$1
    NGINX_FCGI_USER=`awk -F '=' '$1 ~ /^# NGINX_FCGI_USER/ { print $2}' $CFGFILE`;
    [[ -z "$NGINX_FCGI_USER" ]] && return
    echo -n " $NGINX_FCGI_USER"

    NGINX_FCGI_SOCKET=`awk -F '=' '$1 ~ /^# NGINX_FCGI_SOCKET/ { print $2}' $CFGFILE`
    PIDFILE=`echo -n "$NGINX_FCGI_SOCKET" | sed -e 's/\.sock$/.pid/'`

    if [ -x /sbin/start-stop-daemon ]; then
        /sbin/start-stop-daemon --quiet --stop --retry=TERM/30/KILL/5 --pidfile $PIDFILE > /dev/null
        [ "$?" = "0" ] && rm -f $PIDFILE
    else
        killall -u "$NGINX_FCGI_USER" -w -TERM php-cgi 2>/dev/null
    fi
}

start() {
    echo -n "Starting PHP FastCGI for NGINX:"

    # set explicit umask:
    umask 0022

    for i in $CONFIG; do
        start_process $i
    done
    echo " - done."
}

stop() {
    echo -n "Stopping PHP FastCGI for NGINX:"
    for i in $CONFIG; do
        stop_process $i
    done
    echo " - done."
}

restart() {
    echo -n "Restarting PHP FastCGI for NGINX:"

    # set explicit umask:
    umask 0022

    for i in $CONFIG; do
        stop_process $i
        start_process $i
    done
    echo " - done."
}

case "$COMMAND" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    restart)
        restart
        ;;
    *)
        echo "Usage: nginx-php-fcgi {start|stop|restart} [config]"
        RETVAL=1
        ;;
esac
exit $RETVAL

# <EOF>-----------------------------------------------------------------------
