#!/bin/sh
#
# $FreeBSD: ports/www/apache13-fp/files/apache.sh.tmpl,v 1.6 2003/02/04 03:08:36 edwin Exp $

# PROVIDE: apache
# REQUIRE: DAEMON
# KEYWORD: FreeBSD shutdown
#
# NOTE for FreeBSD 5.0+:
# If you want this script to start with the base rc scripts
# move apache.sh to /etc/rc.d/apache

# Define the following apache_* variables in one of the following:
#       /etc/rc.conf
#       /etc/rc.conf.d/apache
#       ${prefix}/etc/rc.conf.d/apache
#
#       apache_enable  - Set to YES to enable apache
#
#       apache_program - Path to apache program
#                        Default: ${prefix}/sbin/httpd
#
#	apache_start   - Subcommand sent to apachectl to control how
#			 httpd is started.
#			 Default: start_FP

prefix=/usr/local

apache_doit ()
{
	case $1 in
		 start)	action=${apache_start} ;;
		reload) action=graceful ;;
		     *)	action=$1 ;;
	esac
	${ctl_command} ${action}
}

# Create New FrontPage suidkey

new_key() {

	CUR_UMASK=`umask`
	skdir=${prefix}/frontpage/version5.0/apache-fp
	PERL=/usr/local/bin/perl

	if [ -x ${prefix}/libexec/apache/mod_frontpage.so ]
	then

		#NOTE: We need Perl 5, to generate a new key
		if [ -x ${PERL} ]
		then
			umask 077
			${PERL} -e '@a=(split(//, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*-=_+")); print((map {$a[rand(scalar @a)]} (1..128)), "\n");' > $skdir/suidkey
			umask ${CUR_UMASK}
		fi
	fi

}

if [ -f /etc/rc.subr ]; then

	. /etc/rc.subr

	name="apache"
	rcvar=`set_rcvar`
	command="${prefix}/sbin/httpd"
	ctl_command="${prefix}/sbin/apachectl"
	# pidfile=/var/run/httpd.pid
	required_files="${prefix}/etc/apache/httpd.conf"
	extra_commands="reload"
	start_precmd="new_key"
	start_cmd="apache_doit start"
	stop_cmd="apache_doit stop"
	restart_cmd="apache_doit restart"
	reload_cmd="apache_doit reload"

	# The below may be removed when load_local_rc_config is added to rc.subr

	if [ -f ${prefix}/etc/rc.conf.d/"$name" ]; then
		debug "Sourcing ${prefix}/etc/rc.conf.d/${name}"
		. ${prefix}/etc/rc.conf.d/"$name"
	fi

	load_rc_config $name

	if [ -z "${apache_enable}" ] ; then
		apache_enable=yes
	fi

	# The above may be removed when load_local_rc_config is added to rc.subr
	#
	# load_local_rc_config $name

	if [ -z "${apache_start}" ]; then
		apache_start="start_FP"
	fi

	run_rc_command "$1"
else
	if [ -f ${prefix}/etc/rc.conf.d/apache ]; then
		. ${prefix}/etc/rc.conf.d/apache
	fi

	if [ -z '${apache_start}" ]; then
		apache_start="start_FP"
	fi

	case "$1" in

	start)
        	if [ -x ${prefix}/sbin/apachectl ]; then
			new_key
			${prefix}/sbin/apachectl ${apache_start} && echo -n ' httpd'
		fi
		;;

	stop)
		if [ -r /var/run/httpd.pid ]; then
			${prefix}/sbin/apachectl stop && echo -n ' httpd'
		fi
		;;

	*)
		echo "usage: $0 {start|stop}" 1>&2
		exit 64
		;;

	esac
fi
