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

. /usr/share/debconf/confmodule
. /usr/share/dbconfig-common/dpkg/postinst.mysql

# We set some var specific for igestis
CONFFILE=/etc/igestis/config.php
GLOBALVARS=/etc/igestis/ConfigIgestisGlobalVars.php
CACHE_FOLDER="/var/cache/igestis/"
LOG_FILE="/var/log/igestis/igestis.log"

replace_value_config()
{

sed -i "s#^[[:space:]]*[dD][eE][fF][iI][nN][eE][[:space:]]*([[:space:]]*\"$1\".*#define(\"$1\", $2);#g" $CONFFILE
sed_return_value=$?

if [ ! "$sed_return_value" = 0 ] ; then
	echo "error : the variable $1 was not found in the file $CONFFILE"
fi

}

get_config_value()
{

grep "^[[:space:]]*[dD][eE][fF][iI][nN][eE][[:space:]]*([[:space:]]*\"$1" $CONFFILE | \
  sed "s#^[[:space:]]*[dD][eE][fF][iI][nN][eE][[:space:]]*([[:space:]]*\"$1\",[[:space:]]*##" | \
  sed "s#[[:space:]]*)[[:space:]]*;##g"

}

replace_value_global_vars()
{

sed -i "s#const[[:space:]]*$1.*#const $1 = $2;#g" $GLOBALVARS
sed_return_value=$?

if [ ! "$sed_return_value" = 0 ] ; then
    echo "error : the variable $1 was not found in the file $GLOBALVARS"
fi

}

get_value_global_vars()
{

grep "^[[:space:]]*const[[:space:]]*$1" $GLOBALVARS | \
  sed "s#^[[:space:]]*const[[:space:]]$1[[:space:]]=[[:space:]]##g" | \
  sed 's/[ ;][[:space:]]*$//g'

}

generate_random_key()
{

dd if=/dev/urandom bs=1 count=62 2> /dev/null | tr -c -d '[A-Za-z0-9]'

}




#DEBHELPER#


case "$1" in
	configure)

	mkdir -p /etc/igestis/
	dbc_generate_include=php:/etc/igestis/debian-db.php
	dbc_go igestis $@


	# Config file creation from template.
	if [ -e "$GLOBALVARS" ] ; then

		# Backup of old config file-
		mkdir -p /var/backup/igestis/ && chmod 700 /var/backup/igestis/
		cp $GLOBALVARS /var/backup/igestis/ConfigIgestisGlobalVars-$(date "+%Y%M%d_%H%M").php.backup

		# Probably useless as the script always modify this file.
		# ucf --debconf-ok /usr/share/igestis/config/igestis/ConfigIgestisGlobalVars-template.php $GLOBALVARS

	else

		FIRST_INSTALL=true
		cp /usr/share/igestis/config/igestis/ConfigIgestisGlobalVars-template.php $GLOBALVARS
		
	fi
	
	# To avoid config missing, we need to create a symbolic link
	ln -sf $GLOBALVARS /usr/share/igestis/config/igestis/ConfigIgestisGlobalVars.php

	# We generate a random encrypt key.
	if [ "$(get_value_global_vars ENCRYPT_KEY)" = \"\" ] && [ -e "$GLOBALVARS" ] ; then
		replace_value_global_vars ENCRYPT_KEY \"$(generate_random_key)\"
	fi

	## On place le log au bon endroit.
	mkdir -p $(dirname $LOG_FILE)
	chown www-data -R $(dirname $LOG_FILE)
	chmod 750 -R $(dirname $LOG_FILE)
	replace_value_global_vars LOG_FILE \"$LOG_FILE\"
	
	# Ensure that the root folder is correctly defined
	replace_value_global_vars ROOT_FOLDER \"/usr/share/igestis\"

	## We setup the cache folder to the right location
	mkdir -p $CACHE_FOLDER
	if [ ! -z "$CACHE_FOLDER" ] ; then rm -rf $CACHE_FOLDER/* ; fi

	## We regenerate the language cache
	replace_value_global_vars CACHE_FOLDER \"$CACHE_FOLDER\"
	php /usr/share/doc/igestis/regenLocales.php /usr/share/igestis
	chown www-data -R $CACHE_FOLDER
	chmod 700 -R $CACHE_FOLDER

	# On corrige les droits pour les fichiers de configuration.
	chown root:www-data -R /etc/igestis/
	chmod 750 -R /etc/igestis/

	db_get igestis/password1
	ROOT_PW="${RET}"

	db_get igestis/authentication
	AUTHENTICATION="${RET}"

	db_get igestis/directory/base
	LDAP_DOMAIN="${RET}"
	LDAP_BASE="dc=`echo $LDAP_DOMAIN | sed 's/^\.//; s/\./,dc=/g'`"

	db_get igestis/directory/uris
	LDAP_URIS="${RET}"

	db_get igestis/directory/admin
	LDAP_ADMIN="${RET}"

	db_get igestis/directory/password
	LDAP_PASSWORD="${RET}"

	# Get the new value username for the admin igestis account.
	db_get igestis/username
	IGESTIS_CORE_ADMIN="${RET}"
	if [ ! -z "$IGESTIS_CORE_ADMIN" ] ; then
		 dbc_mysql_exec_command "update CORE_CONTACTS set login=\"$IGESTIS_CORE_ADMIN\" WHERE CORE_CONTACTS.id=1;"
	fi
	replace_value_global_vars USE_LDAP false

	# Get the value for the auto import.
	db_get igestis/ldap/import
	AUTO_IMPORT="${RET}"
	if [ "${AUTO_IMPORT}" = "0" ] ; then
		AUTO_IMPORT="false"
	else
		AUTO_IMPORT="true"
	fi

	# Replace the DEBUG_MODE
	replace_value_global_vars DEBUG_MODE DEBUG_MODE

	# Retrive right values
	LDAP_LOGIN=$(echo ${LDAP_ADMIN} | cut -d'@' -f1)



	case "$AUTHENTICATION" in

		Internal)
			replace_value_global_vars USE_LDAP false

			if [ ! -z "$ROOT_PW" ] ; then
				dbc_mysql_exec_command "update CORE_CONTACTS set password=md5(\"$ROOT_PW\") WHERE "CORE_CONTACTS"."id"=1;"
			fi
			replace_value_global_vars IGESTIS_CORE_ADMIN \"${IGESTIS_CORE_ADMIN}\"
		;;

		Samba4)
			replace_value_global_vars USE_LDAP true
			replace_value_global_vars LDAP_URIS \"${LDAP_URIS}\"
			replace_value_global_vars LDAP_BASE \"${LDAP_BASE}\"
			replace_value_global_vars LDAP_ADMIN \"${LDAP_LOGIN}\"
			[ ! -z "${LDAP_PASSWORD}" ] && replace_value_global_vars LDAP_PASSWORD \"${LDAP_PASSWORD}\"
			replace_value_global_vars LDAP_USERS_OU \"cn=Users,${LDAP_BASE}\"
			replace_value_global_vars LDAP_CUSTOMERS_OU \"ou=Customers,${LDAP_BASE}\"
			replace_value_global_vars LDAP_SUPPLIERS_OU \"ou=Suppliers,${LDAP_BASE}\"
			replace_value_global_vars LDAP_VERSION 2
			replace_value_global_vars LDAP_AUTO_IMPORT_USER ${AUTO_IMPORT}
			replace_value_global_vars LDAP_AD_MODE true
			replace_value_global_vars LDAP_CUSTOM_BIND \"%u@${LDAP_DOMAIN}\"
			replace_value_global_vars IGESTIS_CORE_ADMIN \"${IGESTIS_CORE_ADMIN}\"
		;;

		OpenLDAP)
			for i in igestis-populate.ldif root-pw-ch.ldif root-pw-exp.ldif ; do
				sed -i -e "s/dc=.*,dc=.*/${LDAP_BASE}/g" /usr/share/doc/igestis/ldap/$i
			done

			# Adding the right OU.
			#ldapadd -Y EXTERNAL -H ldapi:/// -f /usr/share/doc/igestis/ldap/igestis-populate.ldif || true
			ldapadd -cxD "${LDAP_ADMIN}" -w ${LDAP_PASSWORD} -f /usr/share/doc/igestis/ldap/igestis-populate.ldif > /dev/null 2>&1 || true

			# On remplace ce mot de passe dans le fichier ldif puis on applique ce changement dans la base LDAP.
			# Enfin on replace ce mot de passe par 123456 pour ne pas garder de trace du mot de passe.
			sed -i -e "s/userPassword:.*/userPassword:\ ${ROOT_PW}/g" \
			  /usr/share/doc/igestis/ldap/root-pw-ch.ldif > /dev/null 2>&1
			ldapmodify -cxD "${LDAP_ADMIN}" -w ${LDAP_PASSWORD} -f /usr/share/doc/igestis/ldap/root-pw-ch.ldif > /dev/null 2>&1 || true
			sed -i -e "s/userPassword:.*/userPassword:\ 123456/g" \
			  /usr/share/doc/igestis/ldap/root-pw-ch.ldif

			# Small patch avoid the password expiring in old igestis version.
			ldapmodify -cxD "${LDAP_ADMIN}" -w ${LDAP_PASSWORD} -f /usr/share/doc/igestis/ldap/root-pw-exp.ldif > /dev/null 2>&1 || true			

			replace_value_global_vars USE_LDAP true
			replace_value_global_vars LDAP_URIS \"${LDAP_URIS}\"
			replace_value_global_vars LDAP_BASE \"${LDAP_BASE}\"
			replace_value_global_vars LDAP_ADMIN \"${LDAP_ADMIN}\"	
			[ ! -z "${LDAP_PASSWORD}" ] && replace_value_global_vars LDAP_PASSWORD \"${LDAP_PASSWORD}\"
			replace_value_global_vars LDAP_USERS_OU \"ou=Users,${LDAP_BASE}\"
			replace_value_global_vars LDAP_CUSTOMERS_OU \"ou=Customers,${LDAP_BASE}\"
			replace_value_global_vars LDAP_SUPPLIERS_OU \"ou=Suppliers,${LDAP_BASE}\"
			replace_value_global_vars LDAP_VERSION 3
			replace_value_global_vars LDAP_AUTO_IMPORT_USER ${AUTO_IMPORT}
			replace_value_global_vars LDAP_AD_MODE false
			replace_value_global_vars IGESTIS_CORE_ADMIN \"${IGESTIS_CORE_ADMIN}\"
		;;

		"Active Directory")
			replace_value_global_vars USE_LDAP true
			replace_value_global_vars LDAP_URIS \"${LDAP_URIS}\"
			replace_value_global_vars LDAP_BASE \"${LDAP_BASE}\"
			replace_value_global_vars LDAP_ADMIN \"${LDAP_LOGIN}\"
			[ ! -z "${LDAP_PASSWORD}" ] && replace_value_global_vars LDAP_PASSWORD \"${LDAP_PASSWORD}\"
			replace_value_global_vars LDAP_USERS_OU \"cn=Users,${LDAP_BASE}\"
			replace_value_global_vars LDAP_CUSTOMERS_OU \"ou=Customers,${LDAP_BASE}\"
			replace_value_global_vars LDAP_SUPPLIERS_OU \"ou=Suppliers,${LDAP_BASE}\"
			replace_value_global_vars LDAP_VERSION 3
			replace_value_global_vars LDAP_AUTO_IMPORT_USER ${AUTO_IMPORT}
			replace_value_global_vars LDAP_AD_MODE true
			replace_value_global_vars LDAP_CUSTOM_BIND \"%u@${LDAP_DOMAIN}\"
			replace_value_global_vars IGESTIS_CORE_ADMIN \"${IGESTIS_CORE_ADMIN}\"
		;;

	esac

	db_input high igestis/reconfigure-webserver || true
	db_go

	# Clean all saved password from the config.
	db_reset igestis/password1 || true
	db_reset igestis/password2 || true
	db_reset igestis/directory/password || true

	# Configure Apache
    db_get igestis/reconfigure-webserver
    webservers="$RET"

	for webserver in $webservers; do
		webserver=${webserver%,}
		
		if [ -d /etc/$webserver/conf.d/ ] ; then
			ln -sf /etc/igestis/apache.conf /etc/$webserver/conf.d/igestis
		fi

		[ -e /etc/$webserver/conf.d/igestis ] && mv /etc/$webserver/conf.d/igestis /etc/$webserver/conf.d/igestis.conf
		
		if [ -e /etc/$webserver/mods-available/ ] ; then
			ln -sf /etc/igestis/apache.conf /etc/$webserver/mods-available/igestis.conf
		fi
		
		invoke-rc.d $webserver reload 3>/dev/null || true
	done


	# We need to get the good version of igestis and override it in config file.
	CONFFILE=/usr/share/igestis/config/igestis/ConfigIgestisGlobalVars-template.php
	VERSION="$(get_config_value IGESTIS_CORE_VERSION)"
	CONFFILE=/etc/igestis/ConfigIgestisGlobalVars.php
	replace_value_config IGESTIS_CORE_VERSION ${VERSION}


	# Probably useless.
	# ucfr igestis "$GLOBALVARS"

	;;

	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


