#!/bin/sh
set -e

. /usr/share/debconf/confmodule
. /usr/share/dbconfig-common/dpkg/postinst.mysql
dbc_first_version="1.0.6-1"
dbc_go mantis $@

get_vars()
{
	db_get mantis/admin || true
	ADMIN="$RET"

	db_get mantis/webmaster || true
	WEBMASTER="$RET"

	db_get mantis/from || true
	FROM="$RET"

	db_get mantis/bounce || true
	BOUNCE="$RET"

	db_get mantis/webserver || true
	WEBSERVER="$RET"
}

if [ "$1" = 'configure' ]
then
	TEMPLATE_INC="/usr/share/mantis/conf-templates/config_inc.php.template"
	CONF_DB="/etc/mantis/config_db.php"
	CONF_INC="/etc/mantis/config_inc.php"
	UCF="ucf --three-way --debconf-ok"

	# Register configuration files with ucf
	ucfr mantis ${CONF_DB:?}
	ucfr mantis ${CONF_INC:?}

	# create mantis configuration
	dbconfig-generate-include /etc/dbconfig-common/mantis.conf -f php > ${CONF_DB}.new

	# Get debconf values
	get_vars

	# Generate configuration from template
	cat ${TEMPLATE_INC} > ${CONF_INC}.new
	sed -i -e "s/@admin@/$ADMIN/g" ${CONF_INC}.new
	sed -i -e "s/@webmaster@/$WEBMASTER/g" ${CONF_INC}.new
	sed -i -e "s/@from@/$FROM/g" ${CONF_INC}.new
	sed -i -e "s/@bounce@/$BOUNCE/g" ${CONF_INC}.new

	# Copy configuration over/Merge configuration
	if [ -f ${CONF_DB} ]; then
	    $UCF ${CONF_DB}.new ${CONF_DB}
	    rm ${CONF_DB}.new
	else
	    mv ${CONF_DB}.new ${CONF_DB}
	fi

	if [ -f ${CONF_INC} ]; then
	    $UCF ${CONF_INC}.new ${CONF_INC}
	    rm ${CONF_INC}.new
	else
	    mv ${CONF_INC}.new ${CONF_INC}
	fi

	# configure webservers
	if [ "$WEBSERVER" = 'true' ]; then
		if [ -d /etc/apache2/conf.d/ ]; then
			ln -sf /etc/mantis/apache.conf /etc/apache2/conf.d/mantis || true
			invoke-rc.d apache2 reload
		fi
	fi

	# Make sure permissions are right
	LIST_FILES_STATOVERRIDE="/etc/mantis/config_inc.php /etc/mantis/config_local.php /etc/mantis/custom_strings_inc.php"
	for i in $LIST_FILES_STATOVERRIDE; do
            if [ dpkg-statoverride --list $i > /dev/null 2>&1 ] ; then
		dpkg-statoverride --update --add root www-data 644 $i
	    fi
	done

	# Special handling for the DB configuration, because it should
	# not be world-readable
	DB_CONFIG="/etc/mantis/config_db.php"
	if ! dpkg-statoverride --list $DB_CONFIG >/dev/null 2>&1 ; then
	    dpkg-statoverride --update --add root www-data 640 $DB_CONFIG
	else
	    # Fix for #425010
	    if [ "`dpkg --compare-versions $1 lt 1.1.8+dfsg-2`" ]; then
		filestat="`dpkg-statoverride --list /etc/mantis/config_db.php`"
		user="`echo $filestat|cut -d' ' -f1`"
		group="`echo $filestat|cut -d' ' -f2`"
		dpkg-statoverride --force --update --add $user $group 640 /etc/mantis/config_db.php
	    fi
	fi
	# Also part of the fix for #425010, because just fixing config_db.php
	# would only move the problem, ending up with a readable backup
	# of the database configuration
	if [ -f /etc/mantis/config_db.php.ucf-old ]; then
	    chmod o-r /etc/mantis/config_db.php.ucf-old
	fi

	if [ ! -f /usr/share/mantis/www/config_inc.php ]; then
		ln -s /etc/mantis/config_inc.php /usr/share/mantis/www
	fi

	if [ ! -f /usr/share/mantis/www/config_db.php ]; then
		ln -s /etc/mantis/config_db.php /usr/share/mantis/www
	fi

	if [ ! -f /usr/share/mantis/www/config_local.php ]; then
		ln -s /etc/mantis/config_local.php /usr/share/mantis/www
	fi

	if [ ! -f /etc/mantis/custom_strings_inc.php ]; then
		touch /etc/mantis/custom_strings_inc.php
		ln -s /etc/mantis/custom_strings_inc.php /usr/share/mantis/www
	fi
fi

#DEBHELPER#
