#!/bin/bash
# vim: set ts=2 sw=2 et:

set -e

# These functions ask the user whether to reconfigure apache/apachessl.
do_enable () {
  if [ -s /etc/apache/httpd.conf ] ; then
    if grep '^LoadModule.*mod_fastcgi\.so' /etc/apache/httpd.conf 2>&1 >/dev/null
    then
      exit 0
    fi
  fi
  echo -n "A new Apache module has been installed.  Reconfigure apache [Y/n]? "
  read CONFIG
  case "$CONFIG" in
    [nN]*) # user said no
      echo -n ""  # do nothing
      ;;
    *)     # user said yes
      if [ -x /usr/sbin/apacheconfig ] ; then
        /usr/sbin/apacheconfig --force-modules
      fi
      ;;
  esac
}

do_enablessl () {
  if [ -s /etc/apache-ssl/httpd.conf ] ; then
    if grep '^LoadModule.*mod_fastcgi\.so' /etc/apache-ssl/httpd.conf 2>&1 >/dev/null ; then
      exit 0
    fi
  fi
  echo -n "A new Apache module has been installed.  Reconfigure apache-ssl [Y/n]? "
  read CONFIG
  case "$CONFIG" in
    [nN]*)  # user said no
      echo -n ""  # do nothing
      ;;
    *)      # user said yes
      if [ -x /usr/sbin/apache-sslconfig ] ; then
        /usr/sbin/apache-sslconfig --force-modules
      fi
      ;;
  esac
}

# These functions ask the user whether to restart apache/apachessl.
ask_restart () {
  echo -n "An Apache module has been modified.  Restart apache [Y/n]? "
  read CONFIG
  case "$CONFIG" in
    [nN]*)  # user said no
      echo -n ""  # do nothing
      ;;
    *)      # user said yes
      /usr/sbin/apachectl restart
      ;;
  esac
}
ask_restartssl () {
  echo -n "An Apache module has been modified.  Restart apache-ssl [Y/n]? "
  read CONFIG
  case "$CONFIG" in
    [nN]*)  # user said no
      echo -n ""  # do nothing
      ;;
    *)      # user said yes
      /usr/sbin/apache-sslctl restart
      ;;
  esac
}

case "$1" in
  configure)
    # Configure this package.  If the package must prompt the user for
    # information, do it here.  There are three sub-cases.
    if [ "${2+set}" != "set" ] ; then
      # We're being installed by an ancient dpkg which doesn't remember
      # which version was most recently configured, or even whether
      # there is a most recently configured version.
      if [ -s /etc/apache/httpd.conf ]     ; then do_enable    ; fi
      if [ -s /etc/apache-ssl/httpd.conf ] ; then do_enablessl ; fi
    elif [ -z "$2" -o "$2" = "<unknown>" ] ; then
      # The package has not ever been configured on this system, or was
      # purged since it was last configured.
      # DJ: So let's do_enable the module!
      if [ -s /etc/apache/httpd.conf ]     ; then do_enable    ; fi
      if [ -s /etc/apache-ssl/httpd.conf ] ; then do_enablessl ; fi
    else
      # Version $2 is the most recently configured version of this
      # package.
      if [ -x /usr/sbin/apachectl     ] ; then ask_restart    ; fi
      if [ -x /usr/sbin/apache-sslctl ] ; then ask_restartssl ; fi
    fi
    ;;
  abort-upgrade | abort-remove | abort-deconfigure)
    ;;
  *)
    echo "$0: didn't understand being called with \`$1'" 1>&2
    exit 1
    ;;
esac

exit 0

#DEBHELPER#
