#!/bin/sh
#
# Startup script for PHP Web Server
#
# chkconfig: 345 85 15
# description: PHP Web Server
# processname: phpwebserver

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

# See how we were called.
PATH=/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin:/usr/local/cnr/usrbin
PHP=/usr/local/bin/php
LOG=/var/log/phpwebserver.log
PORT=80
DOCUMENT_ROOT=/var/www/html

case "$1" in
  start)
        if netstat -nlt | grep -q :$PORT ; then
            action "Starting phpwebserver: " false
        else
	    action "Starting phpwebserver: " true
            cd $DOCUMENT_ROOT
            $PHP -S 0.0.0.0:$PORT >> $LOG 2>&1 &
        fi 
	;;
  stop)
        if netstat -nlt | grep -q :$PORT ; then
            action "Shutting down phpwebserver: " true
            killall php > /dev/null 2>&1
        else
            action "Shutting down phpwebserver: " false
        fi
	;;
  status)
        netstat -nltp | grep :$PORT
	;;
  restart)
	$0 stop
	while pidof php > /dev/null
	do
	    sleep 1
	done
	$0 start
	;;
  *)
	echo "Usage: $0 {start|stop|restart|status}"
	exit 1
esac

exit 0
