#!/bin/sh
#
# $FreeBSD: ports/www/apache13-fp/files/apache.sh.tmpl,v 1.7 2003/06/08 09:56:50 erwin 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

prefix=/usr/local

# Define the following apache_* variables in one of the following:
#       /etc/rc.conf
#       /etc/rc.conf.d/apache
#       /etc/rc.conf.d/apache
#
# DO NOT CHANGE THESE DEFAULT VALUES HERE
#
apache_enable="YES"			# Enable Apache
#apache_program="${prefix}/sbin/httpd	# Location of httpd
apache_start="start_FP"	# Subcommand sent to apachectl to control how
			# httpd is started.

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/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"

	load_rc_config $name
	run_rc_command "$1"
else
	# Suck in the configuration variables.
	if [ -z "${source_rc_confs_defined}" ]; then
		if [ -r /etc/defaults/rc.conf ]; then
			. /etc/defaults/rc.conf
			source_rc_confs
		elif [ -r /etc/rc.conf ]; then
			. /etc/rc.conf
		fi
	fi

	rc=0

	case "${apache_enable}" in
	    [Yy][Ee][Ss])
		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
			rc=64
			;;
		esac
		;;
            *)
                rc=0
                ;;
	esac

	exit $rc
fi
