#! /bin/sh
#
# httpd          Start/Stop the Apache Web Server
#
# chkconfig: 345 80 15
# description: Apache is a World Wide Web server.  It is used to serve \
#              HTML files and CGI.
# processname: httpd
# pidfile: /var/run/httpd.pid
# config: /etc/httpd/conf/httpd.conf

WITHOUT_RC_COMPAT=1

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

BINARY=/usr/sbin/httpd-gysmeteo
PIDFILE=/var/run/httpd-gysmeteo.pid
PERLPIDFILE=/var/run/httpd-perl-gysmeteo.pid
LOCKFILE=/var/lock/subsys/httpd-gysmeteo
RETVAL=0
#vod
CFGFILE=/home/gysmeteo/httpd/conf/httpd.conf

export TMPDIR=/tmp

### Hack for bad hostname configuration
#vod thehost=`hostname`
#vod hostname -i 1>/dev/null 2>/dev/null|| hostname localhost

### Hack for Status
LYNX="lynx -dump"
STATUSURL="http://www.gysmeteo.ru/server-status"

# Change the major functions into functions.
moduleargs() {
	moduledir=/usr/lib/apache
	moduleargs=
	for module in ${moduledir}/*.so ; do
		if [ -x ${module} ] ; then
			module=`echo ${module} | 
			sed -e 's/.*\///g; s/^mod_//g; s/^lib//g; s/\.so//g;'|
			tr '[:lower:]' '[:upper:]'`
			moduleargs="${moduleargs} -DHAVE_$module"
		fi
	done
	echo ${moduleargs}
}

conftest() {
	# TODO: translatable form?
	action "Checking configuration sanity for `basename $BINARY`: " \
		"$BINARY" -t `moduleargs` $DEFINE -f "$CFGFILE"
	RETVAL=$?
	return $RETVAL
}

start()
{
	if [ "$1" != "again" ]; then
		if status --pidfile "$PERLPIDFILE" --expect-user root \
			  --expect-user root -- httpd-perl >&/dev/null; then
			echo "Warning: httpd-perl is already running, check 'service `basename $BINARY` status' now"
			passed "httpd-perl already running"
		fi
	fi
	start_daemon --pidfile "$PIDFILE" --lockfile "$LOCKFILE" \
		--expect-user root --name libhttpd.ep -- $BINARY -f "$CFGFILE"
	RETVAL=$?
	return $RETVAL
}

stop()
{
	stop_daemon --pidfile "$PIDFILE" --lockfile "$LOCKFILE" \
		--expect-user root --name libhttpd.ep -- $BINARY
	if [ "$1" != "nocheck" -a -f "$PERLPIDFILE" ]; then
		echo "Warning: httpd-perl is running, check 'service httpd-perl extendedstatus'"
		passed "httpd-perl already running"
	fi
	RETVAL=$?
	return $RETVAL
}

reload()
{
	msg_reloading `basename $BINARY`
	stop_daemon --pidfile "$PIDFILE" \
		--expect-user root --name libhttpd.ep -HUP -- $BINARY
	RETVAL=$?
	return $RETVAL
} 

restart()
{
	stop nocheck
	conftest || _exit $?
	start again
}

briefstatus()
{
	status --pidfile "$PIDFILE" --expect-user root \
		--expect-user root --name libhttpd.ep -- $BINARY
	RETVAL=$?
	return $RETVAL
}

extendedstatus()
{
	if briefstatus >/dev/null; then
	    RETVAL=$?
	    $LYNX $STATUSURL | awk ' /process$/ { print; exit } { print } '	
	else
	    RETVAL=$?
	    msg_not_running "`basename $BINARY`"
	    echo
	fi
	return $RETVAL
}

_exit() {
	hostname $thehost
	exit $1
}

# See how we were called.
case "$1" in
	start)
		start
		;;
	stop)
		stop
		;;
	restart)
		restart
		;;
	reload|graceful)
		if [ -e /usr/lib/apache/mod_jserv.so ]; then
			restart
		else
			reload
		fi
		;;
	check|configtest)
		conftest
		_exit $?
		;;
	condstop)
		if [ -e "$LOCKFILE" ]; then
			stop
		fi
		;;
	update|condrestart)
		if [ -e "$LOCKFILE" ]; then
			restart
		fi
		;;
	condreload)
		if [ -e "$LOCKFILE" ]; then
			reload
		fi
		;;
	extendedstatus)
		extendedstatus
		;;
	status)
		briefstatus
		;;
	*)
		msg_usage "${0##*/} {start|stop|reload|restart|check|configtest|condstop|condrestart|condreload|status}"
		RETVAL=1
esac

_exit $RETVAL
