#!/bin/sh
# postinst script for hoteldruid
#
# see: dh_installdeb(1)

set -e

# summary of how this script can be called:
#        * <postinst> `configure' <most-recently-configured-version>
#        * <old-postinst> `abort-upgrade' <new version>
#        * <conflictor's-postinst> `abort-remove' `in-favour' <package>
#          <new-version>
#        * <postinst> `abort-remove'
#        * <deconfigured's-postinst> `abort-deconfigure' `in-favour'
#          <failed-install-package> <version> `removing'
#          <conflicting-package> <version>
# for details, see http://www.debian.org/doc/debian-policy/ or
# the debian-policy package


# Source debconf library.
. /usr/share/debconf/confmodule


case "$1" in
  configure)
    db_get hoteldruid/configure-apache || true
    if [ "$RET" = "true" ]; then


      # Write Apache configuration file
      apacheconf=`tempfile`
      cat >> $apacheconf <<-EOF
Alias /hoteldruid/pages /var/lib/hoteldruid/pages
Alias /hoteldruid /usr/share/hoteldruid

<Directory /usr/share/hoteldruid/>
	Options +FollowSymLinks
	AllowOverride All
	order allow,deny
EOF
      db_get hoteldruid/restrict-localhost || true
      if [ "$RET" = "true" ]; then # Access from localhost
        echo "	Allow from localhost 127.0.0.1 ::1" >> $apacheconf
      else  # Globally accessible
        echo "	Allow from all" >> $apacheconf
      fi
      cat >> $apacheconf <<-EOF
</Directory>

<Directory /var/lib/hoteldruid/pages>
	Options +FollowSymLinks
	AllowOverride All
	order allow,deny
	Allow from all
</Directory>

<Directory /var/lib/hoteldruid/data>
	Deny from all
</Directory>
EOF

     ucf --debconf-ok $apacheconf /etc/hoteldruid/apache2.conf
     ucfr hoteldruid /etc/hoteldruid/apache2.conf

      # Remove temporary file
      rm $apacheconf;

      if [ -e /etc/hoteldruid/apache2.conf ]; then
        chmod 0644 /etc/hoteldruid/apache2.conf
      fi


      dir="/etc/apache2/conf.d"
      file="$dir/hoteldruid.conf"
      if [ -d "$dir" ] && [ ! -e "$file" ]; then
        # Link the apache configuration file to the server's
        # conf.d directory
        echo "Installing into... [$dir]" >/dev/stderr
        ln -fs /etc/hoteldruid/apache2.conf "$file"
      fi
      db_get hoteldruid/restart-webserver
      if [ "$RET" = "true" ]; then
        if which invoke-rc.d >/dev/null 2>&1; then
          invoke-rc.d apache2 force-reload 3>/dev/null || true
        else
          /etc/init.d/apache2 force-reload 3>/dev/null || true
        fi
      fi
    fi
  ;;

  abort-upgrade|abort-remove|abort-deconfigure)
  ;;

  *)
    echo "postinst called with unknown argument \`$1'" >&2
    exit 1
  ;;
esac

# dh_installdeb will replace this with shell code automatically
# generated by other debhelper scripts.

#DEBHELPER#

exit 0
