#!/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=`mktemp`
      cat >> $apacheconf <<-EOF
Alias /hoteldruid/pages /var/lib/hoteldruid/pages
Alias /hoteldruid /usr/share/hoteldruid

<Directory /usr/share/hoteldruid/>
	Options +FollowSymLinks
EOF
      db_get hoteldruid/restrict-localhost || true
      if [ "$RET" = "true" ]; then # Access from localhost
        echo "	Require local" >> $apacheconf
      else  # Globally accessible
        echo "	Require all granted" >> $apacheconf
      fi
      cat >> $apacheconf <<-EOF
</Directory>

<Directory /var/lib/hoteldruid/pages>
	Options +FollowSymLinks
	AllowOverride All
	Require all granted
</Directory>

<Directory /var/lib/hoteldruid/data>
	Require all denied
</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


      # Remove old configuration symlink for apache 2.2
      if [ -e /etc/apache2/conf.d/hoteldruid.conf ]
      then
        rm /etc/apache2/conf.d/hoteldruid.conf
      fi
      # Remove possible user compatibility symlink for apache 2.4
      if [ -h /etc/apache2/conf-available/hoteldruid.conf ]
      then
        rm /etc/apache2/conf-available/hoteldruid.conf
      fi
      if [ -d /etc/apache2/conf-available -a ! -e /etc/apache2/conf-available/hoteldruid.conf ]
      then
        ln -s /etc/hoteldruid/apache2.conf /etc/apache2/conf-available/hoteldruid.conf
        if [ -e /usr/share/apache2/apache2-maintscript-helper ]
        then
          . /usr/share/apache2/apache2-maintscript-helper
          apache2_invoke enconf hoteldruid
        fi
      fi

      db_get hoteldruid/restart-webserver
      if [ "$RET" = "true" ]; then
        invoke-rc.d apache2 force-reload 3>/dev/null || true
      fi
    fi


  # Write file with administrator username and password
  PASS_FILE="/var/lib/hoteldruid/data/ini.php"
  db_get hoteldruid/administrator-username
  if [ -n "$RET" ] && [ ! -e "$PASS_FILE" ]; then
    cat >> $PASS_FILE <<-EOF
<?php

define('C_ADMIN_NAME','$RET');
EOF
    db_get hoteldruid/administrator-password
    if [ -n "$RET" ]; then
      SALT=$(</dev/urandom tr -dc A-Za-z0-9 | head -c 19)
      SALT="=$SALT"
      PASS=$(echo -n "$RET$SALT" | md5sum -b | cut -d' ' -f1)
      cat >> $PASS_FILE <<-EOF
define('C_ADMIN_PASS','$PASS');
define('C_ADMIN_SALT','$SALT');
define('C_ADMIN_MD5P','1');
EOF
    fi
    cat >> $PASS_FILE <<-EOF

?>
EOF

    if [ -e "$PASS_FILE" ]; then
        chmod 0600 $PASS_FILE
        chown www-data:www-data $PASS_FILE
    fi

  fi
  db_set hoteldruid/administrator-username ""
  db_reset hoteldruid/administrator-password

  ;;

  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
