#!/bin/bash -e
#
# apache2		This init.d script is used to start local apache2 servers

#edit /etc/default/apache2 to change this.
NO_START=0

set -e
if ! [ -x /etc/init.d/apache2 ] ; then
	exit 0
fi

. /lib/lsb/init-functions

test -f /etc/default/rcS && . /etc/default/rcS
test -f /etc/default/apache2 && . /etc/default/apache2
if [ "$NO_START" != "0" -a "$1" != "stop" ]; then 
        [ "$VERBOSE" != "no" ] && log_warning_msg "Not starting apache2 - edit /etc/default/apache2 and change NO_START to be 0.";
        exit 0;
fi

LOCFILE=".apache2-local"

invoke() {
	CMD="$1"
	for acct in $(getent passwd | awk '{print $1}' FS=":"); do
	  HOMEDIR="$(getent passwd $acct | awk '{print $6}' FS=":")" 
	  if [ -e "$HOMEDIR/$LOCFILE" ]; then
	    for file in $(cat $HOMEDIR/$LOCFILE); do
	      su - $acct -c /etc/init.d/apache2 $CMD $file
	    done
	  fi
	done
}

case $1 in
	start)
		log_begin_msg "Starting local apache http daemons..."
		if invoke start; then
                        log_end_msg 0
                else
                        log_end_msg 1
                fi
		;;
	stop)
		log_begin_msg "Stopping local apache http daemons..."
		if invoke stop; then
                        log_end_msg 0
                else
                        log_end_msg 1
                fi
		;;
	reload)
		log_begin_msg "Reloading local apache http daemon configuration..."
		if invoke reload graceful; then
                        log_end_msg 0
                else
                        log_end_msg 1
                fi
		;;
	restart | force-reload)
		log_begin_msg "Forcing reload of local apache http daemon web server..."
		if invoke restart; then
                        log_end_msg 0
                else
                        log_end_msg 1
                fi
		;;
	*)
		echo "Usage: /etc/init.d/apache2-local start|stop|restart|reload|force-reload" >&2
		exit 2
		;;
esac
