#! /bin/sh

ADIR=/usr/local/apache
true=0
fail=1

start()
{
	pid_check=`ps -ef | grep httpd | grep -v grep | grep -v root | wc -l`

        if [ $pid_check != 0 ] ; then
                echo "apache already running"
                return $true
        else
                echo "starting apache..."
                $ADIR/bin/apachectl start

		sleep 2

		pid_check=`ps -ef | grep httpd | grep -v grep | grep -v root | wc -l`
                if [ $pid_check != 0 ] ; then
                        echo " >> apache start [OK]"
                        return $true
                else
                        echo " >> apache start [FAIL]"
                        return $fail
                fi
        fi
}

stop()
{
        pid_check=`ps -ef | grep httpd | grep -v grep | grep -v root | wc -l`

        if [ $pid_check == 0 ] ; then
                echo "apache not running"
                return $true
        else
                echo "stopping apache..."
                $ADIR/bin/apachectl stop

                sleep 2

                pid_check=`ps -ef | grep httpd | grep -v grep | grep -v root | wc -l`
                if [ $pid_check == 0 ] ; then
                        echo " >> apache stop [OK]"
                        return $true
                else
                        echo " >> apache stop [FAIL]"
                        return $fail
                fi
        fi
}

status()
{
        pid_check=`ps -ef | grep httpd | grep -v grep | grep -v root | wc -l`

        if [ $pid_check == 0 ] ; then
                echo "apache not running"
                return $fail
#                return $true
        else
                echo "apache already running"
                return $true
        fi
}


case "$1" in
  start)
	start
        ;;
  stop)
	stop
        ;;
  status)
        status
        ;;
  restart)
	$ADIR/bin/apachectl restart
        ;;
  *)
        echo "Usage: $0 {start|stop|restart|status}"
        exit 1
esac


