#!/bin/sh

###############################################################################
#
# /etc/init.d/apache2 - System control script for the Apache2 daemon.
# Also accessed via a symbolic link named /usr/local/sbin/rcapache.
#
# Copyright (c) 2006, all rights reserved
# License- GPL
# Author-  Andre G Ancelin
# Company- Adtec Digital, Inc.
# Date-    2006-04-05
#
###############################################################################

DAEMON="/usr/sbin/httpd"
CONF="/etc/httpd/conf/httpd.conf"

#APACHE_DIRS="/var/tmp/log/httpd /var/cache/httpd /var/lib/httpd /var/lock/httpd /var/log/httpd"

RCAPACHE_LOG="/var/tmp/rcapache"

# Indicate whether reloads are supported via signals (usually SIGHUP (1)).
# If so, define RELOAD_SIGNAL to the signal. If not, comment out.
#RELOAD_SIGNAL="1"

source /etc/init.d/rcservices

# Check for IPv6 support in Apache
INET6=""
#$DAEMON -help 2>&1 | fgrep IPv6 2>&1 >/dev/null
#if [ $? -eq 0 ]; then
#  INET6="-4"
#  if [ "$START_INET6" = yes ]; then
#    INET6="-6"
#  fi  
#fi

check_modules ()        # Usage: check_modules
{
  MODULES="-D STATUS";
  echo -n "status "
}

# Determine the base and follow a runlevel link name.
base=${0##*/}
link=${base#*[SK][0-9][0-9]}

# export variables for database drivers in PHP
#DBROOT=/dev/null
#AGA LD_LIBRARY_PATH=/usr/local/lib
#export DBROOT LD_LIBRARY_PATH


# export variables for the Python environment.
# Still not clear on this, but from what I (AGA, 2004-01-31) can gleam-
#  - PYTHONPATH is the root to the Python installation.
#  - PYTHONHOME is the prefix:exec_prefix relative path after PYTHONPATH,
#    where prefix is the platform independent .py & .pyc stuff and
#    exec_prefix is the platform dependent .so stuff.
# I also think that you can argue an absolute PYTHONHOME (???).
#
# Relative approach-
#PYTHONPATH="/usr/local"
#PYTHONHOME="lib/python2.2:lib/python2.2/lib-dynload"
#PYTHONPATH="/usr/lib"


export PYTHONHOME="/usr/local"


#------------------------------------------------------------------------------
# Start the service.
do_start ()
{
  # Make all required Apache directories.
  #mkdir -p $APACHE_DIRS
  
  # Start the daemon.
  echo "Starting ${DAEMON} daemon"
  if $DAEMON $INET6 -t -f $CONF $MODULES > $RCAPACHE_LOG 2>&1 ; then
#    echo -n "Starting httpd ["
#    check_modules
#    echo "]"
    start-stop-daemon --start --exec $DAEMON -- $INET6 -f $CONF $MODULES
    #rc_status -v
  else
    if [ $link = $base ]; then
      echo -e -n "\n\n"
      cat $RCAPACHE_LOG
    else
      echo -e -n "\nsee $RCAPACHE_LOG for details\n";
    fi
    #rc_failed 6
    #rc_status -v1
  fi
}

#------------------------------------------------------------------------------
# Stop the service.
do_stop ()
{
  # Stop the service.
  echo "Stopping ${DAEMON} daemon"
  start-stop-daemon --stop  --exec $DAEMON
}

#------------------------------------------------------------------------------
# Reload & Forcereload the service.
do_forcereload ()
{
  check_modules
  echo -n "Reload httpd"
  if $DAEMON $INET6 -t -f /etc/httpd/httpd.conf $MODULES > $RCAPACHE_LOG 2>&1 ; then
    killproc -USR1 $HTTPD_BIN || return=$rc_failed
    #rc_status -v
  else
    if [ $link = $base ] ; then
      echo -e -n "\n\n"
      cat $RCAPACHE_LOG
    else
      echo -e -n "\nsee $RCAPACHE_LOG for details\n";
    fi
    #rc_failed 6
    #rc_status -v1
  fi
}
do_reload () 
{
  do_forcereload
}

#------------------------------------------------------------------------------
# Probe for the necessity of a reload.
# Tell the user whether or not a reload is needed.
do_probe ()
{
  if [ /etc/httpd/httpd.conf -nt /var/run/httpd.pid ]; then
    echo "/etc/httpd/httpd.conf is newer than /var/run/httpd.pid, need to reload"
  else
    echo "/etc/httpd/httpd.conf is up to date, no need to reload"
  fi
}

#------------------------------------------------------------------------------
# Usage for this service.
do_usage ()
{
  echo "Usage: $1 {start|stop|status|tryrestart|restart|forcereload|reload|probe}"
  exit 1
}

# Include the common script for all control scripts.
dispatch_service $1
