#!/bin/bash

set -e

if [ "$1" != "configure" ]; then
    exit 0
fi

. /usr/share/debconf/confmodule
db_version 2.0 || [ $? -lt 30 ]

cd /usr/share/ldap-account-manager/config-samples/profiles
files=`ls -a default.*`
for file in $files; do
	if [ ! -f /var/lib/ldap-account-manager/config/templates/profiles/$file ]; then
		cp $file /var/lib/ldap-account-manager/config/templates/profiles/$file
		chown www-data /var/lib/ldap-account-manager/config/templates/profiles/$file
	fi
done
cd /usr/share/ldap-account-manager/config-samples/pdf
files=`ls -a default.*`
for file in $files; do
	if [ ! -f /var/lib/ldap-account-manager/config/templates/pdf/$file ]; then
		cp $file /var/lib/ldap-account-manager/config/templates/pdf/$file
		chown www-data /var/lib/ldap-account-manager/config/templates/pdf/$file
	fi
done
cd /usr/share/ldap-account-manager/config-samples/pdf/logos
files=`ls -a *.jpg`
for file in $files; do
	cp $file /var/lib/ldap-account-manager/config/templates/pdf/logos/$file
	chown www-data /var/lib/ldap-account-manager/config/templates/pdf/logos/$file
done

chmod 700 /var/lib/ldap-account-manager/config
chmod 700 /var/lib/ldap-account-manager/tmp
chmod 700 /var/lib/ldap-account-manager/sess
chmod 600 /etc/ldap-account-manager/config.cfg
set +e
ls -l /var/lib/ldap-account-manager/config/*.conf | grep -v ".sample." &> /dev/null
cfgFilesExist=$?
set -e
if [ $cfgFilesExist -ne 0 ]; then
  cp /var/lib/ldap-account-manager/config/unix.sample.conf /var/lib/ldap-account-manager/config/lam.conf
	chown www-data /var/lib/ldap-account-manager/config/lam.conf
fi
chmod 600 /var/lib/ldap-account-manager/config/*.conf
if [ "$1" = "configure" ]; then
    db_get "ldap-account-manager/alias"
    alias="$RET"
	 perl -pi -e "s/Alias \/.* \/usr\/share\/ldap-account-manager/Alias \/$alias \/usr\/share\/ldap-account-manager/g"\
        /etc/ldap-account-manager/apache.conf
    db_get "ldap-account-manager/config-webserver"
    webservers="$RET"
    restart=""
   for server in $webservers; do
       server=${server%,}
       test -x /usr/sbin/$server || continue
       case "$server" in
           apache2)
               if [ -h /etc/$server/conf.d/ldap-account-manager ]; then
                   rm -f /etc/$server/conf.d/ldap-account-manager
               fi
               if [ -d /etc/$server/conf-available ]; then
                   test -x /usr/sbin/a2query && test -x /usr/sbin/a2enconf || continue
                   if [ ! -e /etc/$server/conf-available/ldap-account-manager.conf ]; then
                       if [ -h /etc/$server/conf-available/ldap-account-manager.conf ]; then
                           rm /etc/$server/conf-available/ldap-account-manager.conf
                       fi
                       ln -s /etc/ldap-account-manager/apache.conf /etc/$server/conf-available/ldap-account-manager.conf
                   fi
                   a2query -q -c ldap-account-manager || a2enconf -q ldap-account-manager
                   restart="$restart $server"
               fi
               ;;
       esac
   done
   db_get "ldap-account-manager/restart-webserver"
   if [ "$RET" = "true" ]; then
       for server in $restart; do
           server=${server%,}
           if [ `which invoke-rc.d` ]; then
               set +e
               invoke-rc.d $server reload
               set -e
           fi
       done
   fi
	fi

#DEBHELPER#

db_stop

exit 0
