#! /bin/sh

. /etc/init.d/functions

. /etc/sysconfig/network

if [ -f /etc/sysconfig/php-fpm ]; then
. /etc/sysconfig/php-fpm
fi

if [ "$NETWORKING" = "no" ]
then
exit 0
fi

RETVAL=0
prog="php-fpm"
pidfile=${PIDFILE-/home/application/php/var/run/php-fpm.pid}
lockfile=${LOCKFILE-/var/lock/subsys/php-fpm}

sbin_php_fpm="/home/application/php/sbin/php-fpm"

start () {
echo -n $"Starting $prog: "
dir=$(dirname ${pidfile})
[ -d $dir ] || mkdir $dir
daemon --pidfile ${pidfile} ${sbin_php_fpm} --daemonize
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && touch ${lockfile}
}
stop () {
echo -n $"Stopping $prog: "
killproc -p ${pidfile} php-fpm
RETVAL=$?
echo
if [ $RETVAL -eq 0 ] ; then
rm -f ${lockfile} ${pidfile}
fi
}

restart () {
stop
start
}

reload () {
echo -n $"Reloading $prog: "
if ! ${sbin_php_fpm} --test ; then
RETVAL=6
echo $"not reloading due to configuration syntax error"
failure $"not reloading $prog due to configuration syntax error"
else
killproc -p ${pidfile} php-fpm -USR2
RETVAL=$?
fi
echo
}

case "$1" in
start)
start
;;
stop)
stop
;;
status)
status -p ${pidfile} php-fpm
RETVAL=$?
;;
restart)
restart
;;
reload|force-reload)
reload
;;
configtest)
${sbin_php_fpm} --test
RETVAL=$?
;;
condrestart|try-restart)
[ -f ${lockfile} ] && restart || :
;;
*)
echo $"Usage: $0 {start|stop|status|restart|reload|force-reload|condrestart|try-restart|configtest}"
RETVAL=2
;;
esac

exit $RETVAL