#!/bin/sh

# Bring up/down httpd

. /etc/sysconfig/rc.conf
. /bin/network-functions
. /etc/image_features

SETSID=/usr/bin/setsid
PIDFILE=/var/run/httpd.pid
SERVICE=httpd
PROCESS_NAME=httpd

KILLALL=/usr/bin/killall

do_start ()
{
	if ! ServiceCheckRunning1 ; then
		return $?
	fi
	
	# check if config files are there.. if not.. copy them from default-conf
	if [ ! -d /etc/fiad-conf/apache2/sites-enabled ] ; then
		mkdir -p /etc/fiad-conf/apache2/sites-enabled/
	fi
	if [ ! -f /etc/fiad-conf/apache2/httpd-ports.conf ] ; then
		cp -f /etc/default-conf/apache2/httpd-ports.conf /etc/fiad-conf/apache2/
	fi

	if [ ! -f /etc/fiad-conf/apache2/httpd-advanced.conf ] ; then
		cp -f /etc/default-conf/apache2/httpd-advanced.conf /etc/fiad-conf/apache2/
	fi

	if [ ! -f /etc/fiad-conf/apache2/httpd-ssl-certs.conf ] ; then
		cp -f /etc/default-conf/apache2/httpd-ssl-certs.conf /etc/fiad-conf/apache2/
	fi

	/etc/init.d/softdogd wait
	$SETSID /bin/$PROCESS_NAME -f /etc/apache2/httpd.conf >/dev/null 2>/dev/null

	
	ServiceCheckRunning2
	
	return $?
}

do_stop ()
{
	local RESULT;

	/etc/init.d/softdogd wait
	ServiceStop
	RESULT=$?
	/bin/sleep 2
	$KILLALL -9 cgimains.cgi

	return $RESULT
}

do_status ()
{
	local HTTPDSTATUS;

	ServiceStatus
	HTTPDSTATUS=$?
	if [ "$HTTPDSTATUS" != "1" ] && [ "$HTTPDSTATUS" != "3" ] ; then
		/bin/httpstatustester
		HTTPDSTATUS=$?
	fi

	return $HTTPDSTATUS
}

case "$1" in
	start)
		do_start
		;;
	stop)
		do_stop
		;;
	status)
		do_status
		;;
	slow_restart)
		/bin/sleep 5
		do_stop
		do_start
		;;
	restart)
		do_stop
		do_start
		;;
	*)
		echo "Usage: $0 {start|stop|status|restart|slow_restart}"
		exit 1
esac

exit $?
