#!/bin/sh
#
# spawn-fcgi - this script starts and stops the spawn-fcgi daemon
#
# chkconfig:   - 84 16 
# description:  spawn-fcgi is used to spawn fcgi-process directly \
#                without the help of a webserver or the programm itself.
# processname: spawn-fcgi
# config:      /etc/sysconfig/spawn-fcgi
# pidfile:     /var/run/spawn-fcgi.pid

# Source function library.
. /etc/rc.d/init.d/functions

# Source networking configuration.
. /etc/sysconfig/network

# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0

spawn_fcgi="/usr/sbin/spawn-fcgi"
prog="php-cgi"

SPAWN_FCGI_CONF_FILE="/etc/sysconfig/spawn-fcgi"

[ -f /etc/sysconfig/spawn-fcgi ] && . /etc/sysconfig/spawn-fcgi

lockfile=/var/lock/subsys/spawn-fcgi

start() {
    [ -x $spawn_fcgi ] || exit 5
    [ -f $SPAWN_FCGI_CONF_FILE ] || exit 6
    echo -n $"Starting $prog: "
    daemon $spawn_fcgi -p ${FCGIPORT} -f ${FCGIPROGRAM} -u ${USERID} \
	-g ${GROUPID} -C ${PHP_FCGI_CHILDREN} -P ${FCGIPID} && success || failure
    retval=$?
    echo
    [ $retval -eq 0 ] && touch $lockfile
    return $retval
}

stop() {
    echo -n $"Stopping $prog: "
    killproc $prog -QUIT
    retval=$?
    echo
    [ $retval -eq 0 ] && rm -f $lockfile
    return $retval
}

restart() {
    stop
	sleep 2
    start
}

rh_status() {
    status $prog
}

rh_status_q() {
    rh_status >/dev/null 2>&1
}

case "$1" in
    start)
        rh_status_q && exit 0
        $1
        ;;
    stop)
        rh_status_q || exit 0
        $1
        ;;
    status)
        rh_status
        ;;
    *)
        echo $"Usage: $0 {start|stop|status|restart}"
        exit 2
esac
