#!/bin/sh

set -e

. /usr/share/debconf/confmodule

case "$1" in
    configure)
	db_get mythtv/mysql_host
	hostname="$RET"
	export hostname
	db_get mythtv/mysql_mythtv_dbname
	database="$RET"
	export database
	db_get mythtv/mysql_mythtv_user
	mythtv_username="$RET"
	export mythtv_username
	db_get mythtv/mysql_mythtv_password
	mythtv_password="$RET"
	export mythtv_password

	db_stop

	HTACCESS=/etc/mythtv/mythweb-mysql.conf
	NEW=${HTACCESS}.new
	touch $NEW

	if [ -s $HTACCESS ]; then
	    INPUT=$HTACCESS
	    chown --reference=$INPUT $NEW
	    chmod --reference=$INPUT $NEW
	else
	    INPUT=/usr/share/mythtv/mythweb/mythweb-mysql.conf
	    chown www-data:www-data $NEW
	    chmod 640 $NEW
	fi

cat $INPUT | perl -pe "
s/(^[ \t]*setenv[ \t]+db_server[ \t]+\")[^\"]*\"/\$1\$ENV{'hostname'}\"/g;
s/(^[ \t]*setenv[ \t]+db_name[ \t]+\")[^\"]*\"/\$1\$ENV{'database'}\"/g;
s/(^[ \t]*setenv[ \t]+db_login[ \t]+\")[^\"]*\"/\$1\$ENV{'mythtv_username'}\"/g;
s/(^[ \t]*setenv[ \t]+db_password[ \t]+\")[^\"]*\"/\$1\$ENV{'mythtv_password'}\"/g;
" > $NEW

	mv $NEW $HTACCESS

	cachedir=/var/cache/mythweb/image_cache
	if ! dpkg-statoverride --list $cachedir; then
	    chgrp www-data $cachedir
	    chmod 1775 $cachedir
	fi

# Move Data to new location
	if [ ! -L /usr/share/mythtv/mythweb/data -a -d /usr/share/mythtv/mythweb/data ]; then
		mv /usr/share/mythtv/mythweb/data/* /var/lib/mythtv/mythweb/data
		rmdir /usr/share/mythtv/mythweb/data
		cd /usr/share/mythtv/mythweb && ln -s /var/lib/mythtv/mythweb/data data
		chown -R www-data:www-data /var/lib/mythtv/mythweb
	fi

# Symlink
	if [ ! -L /usr/share/mythtv/mythweb/data ]; then
		cd /usr/share/mythtv/mythweb && ln -s /var/lib/mythtv/mythweb/data data
	fi

	for dir in /var/lib/mythtv; do
	    if ! dpkg-statoverride --list $dir >/dev/null; then
	        chown mythtv:mythtv $dir
		chmod 2775 $dir
	    fi
	done

	chown -R www-data:www-data /var/lib/mythtv/mythweb/data

# Enable rewrite and cgi module.
# Is apache 2.2 installed ?
	COMMON_STATE=$(dpkg-query -f '${Status}' -W 'apache2.2-common' 2>/dev/null | awk '{print $3}' || true)

	if [ -e /usr/share/apache2/apache2-maintscript-helper ] ; then
		. /usr/share/apache2/apache2-maintscript-helper
		apache2_invoke enmod rewrite
		apache2_invoke enmod cgi
	elif [ "$COMMON_STATE" = "installed" ] || [ "$COMMON_STATE" = "unpacked" ] ; then
		[ -d /etc/apache2/conf.d/ ] && [ ! -L /etc/apache2/conf.d/mythweb.conf ] && ln -s ../conf-available/mythweb.conf /etc/apache2/conf.d/mythweb.conf
		[ -d /etc/apache2/conf.d/ ] && [ ! -L /etc/apache2/conf.d/mythweb-mysql.conf ] && ln -s ../conf-available/mythweb-mysql.conf /etc/apache2/conf.d/mythweb-mysql.conf
		if [ -e /usr/share/apache2/apache2-maintscript-helper ] ; then
			. /usr/share/apache2/apache2-maintscript-helper
			apache2_invoke enmod rewrite >/dev/null || true
			apache2_invoke enmod cgi >/dev/null || true
		fi
		if [ -e /var/run/apache2.pid ]; then
			service apache2 reload
		fi
	fi

	if [ -e /usr/share/apache2/apache2-maintscript-helper ] ; then
		. /usr/share/apache2/apache2-maintscript-helper
		apache2_invoke enconf mythweb  || exit $?
	fi

    ;;

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

    ;;

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

#DEBHELPER#

exit 0
