#!/bin/bash
set -e


# This function may enable our module.
enable () {
		apacheconf=/etc/${apache}/httpd.conf
        if
        test -s $apacheconf
        then
           if grep '^LoadModule.*mod_gzip\.so' $apacheconf 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]*) ;;
          *) 
            if ! grep -q "^[[:space:]]*mod_gzip" $apacheconf; then
              echo >> $apacheconf
              echo >> $apacheconf '<IfModule mod_gzip.c>'
              echo >> $apacheconf '  # The current block will be removed automatically by prerm script,'
              echo >> $apacheconf '  # all other mod_gzip* directives will remain commented out.'
              echo >> $apacheconf '  # mod_gzip is turned off by default, insert "mod_gzip_on Yes"'
              echo >> $apacheconf '  # into every virtualhost block (preferred in "IfModule mod_gzip.c'
              echo >> $apacheconf '  # block") you want to use compression or change No to Yes in the'
              echo >> $apacheconf '  # following line to enable it globally'
              echo >> $apacheconf '  # This is a minimal configuration, for more examples see'
              echo >> $apacheconf '  # samples.txt.gz file'
              echo >> $apacheconf '  mod_gzip_on No'
              echo >> $apacheconf '  mod_gzip_item_include file \.htm$'
              echo >> $apacheconf '  mod_gzip_item_include file \.html$'
              echo >> $apacheconf '  mod_gzip_item_include mime text/.*'
              echo >> $apacheconf '  mod_gzip_dechunk Yes'
              echo >> $apacheconf '  mod_gzip_send_vary Yes'
              echo >> $apacheconf '  mod_gzip_temp_dir /tmp'
              echo >> $apacheconf '  mod_gzip_keep_workfiles No'
              echo >> $apacheconf '  mod_gzip_can_negotiate No'
              echo >> $apacheconf '  mod_gzip_update_static No'
              echo >> $apacheconf '  mod_gzip_static_suffix .gz'
              echo >> $apacheconf '  LogFormat "%h %l %u %t \"%V %r\" %>s %b mod_gzip: %{mod_gzip_result}n In:%{mod_g zip_input_size}n Out:%{mod_gzip_output_size}n:%{mod_gzip_compression_ratio}npct.  " common_with_mod_gzip_info'
              echo >> $apacheconf '</IfModule>'
      		  
			  test ! -x /usr/sbin/${apache} || modules-config ${apache} enable mod_gzip
            fi

	    ;;
        esac
}

#
# Restart apache if user wants.

ask_restart () {
        echo -n "An $apache module has been modified.  Restart $apache [Y/n]? "
        read CONFIG
        case "$CONFIG" in
          [nN]*) ;;
          *) /usr/sbin/${apache}ctl 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 test "${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.
			for apache in apache apache-ssl apache-perl
			do 
			  if test -s /etc/${apache}/httpd.conf
              then
                enable
              fi
	        done
      :
    elif test -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 enable the module!
			for apache in apache apache-ssl apache-perl
			do 
			  if test -s /etc/${apache}/httpd.conf
              then
                enable
              fi
	        done
      :
    else
      # Version $2 is the most recently configured version of this
      # package.
			for apache in apache apache-ssl apache-perl
			do 
              if test -x /usr/sbin/${apache}ctl
               then
               ask_restart
              fi
			done
      :
    fi ;;
  abort-upgrade | abort-remove | abort-deconfigure)
        :
    ;;
  *) echo "$0: didn't understand being called with \`$1'" 1>&2
     exit 1;;
esac

#DEBHELPER#

exit 0
