#! /bin/sh
# postinst script for phpgroupware
#
# 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>
#        * <deconfigured's-postinst> `abort-deconfigure' `in-favour'
#          <failed-install-package> <version> `removing'
#          <conflicting-package> <version>
# for details, see /usr/share/doc/packaging-manual/

. /usr/share/debconf/confmodule
db_version 2.0

case "$1" in
	configure)
		# Update from old package.
		old="/var/www/phpgroupware/files"
		new="/var/lib/phpgroupware/files"
		if [ -d ${old} ] ; then
			for dir in users groups ; do 
				rm -f ${old}/${dir}/dont_delete_me
				if [ -n "$(ls ${old}/${dir})" ] ; then
					mv -f ${old}/${dir}/* ${new}${dir}
				fi
			done
		fi
		rm -rf ${old}

		config="/etc/phpgroupware/header.inc.php"
		touch $config
		if [ ! -s $config ] ; then
			template="/usr/share/phpgroupware/header.inc.php.template"

			# Get the web server type.
			db_get "phpgroupware/webserver"
			webserver="$RET"
			case $webserver in
				Apache)		webservers="apache" ;;
				Apache-SSL)	webservers="apache-ssl" ;;
				Both)		webservers="apache apache-ssl" ;;
				*)		webservers="" ;;
			esac
			. /usr/share/wwwconfig-common/php.get
			. /usr/share/wwwconfig-common/apache-run.get

			# Set up web server.
			for server in $webservers ; do
				trustuser=$wwwuser
				. /usr/share/wwwconfig-common/exim-trust.sh
				test "$status" = "trust" && restart="exim $restart"
				
				includefile="/etc/phpgroupware/apache.conf"
				. /usr/share/wwwconfig-common/apache-include_all.sh
				test "$status" = "uncomment" -o "$status" = "include" && restart="$server $restart"
				
				for index in index.php index.php3; do
					. /usr/share/wwwconfig-common/apache-index_all.sh
					test "$status" = "added" && restart="$server $restart"
				done
			done

			# Setup the database.
			db_get "phpgroupware/db/setup/skip"
			if [ "$RET" = "false" ] ; then
				db_get "phpgroupware/db/host"
				dbserver="$RET"
				db_get "phpgroupware/db/name"
				dbname="$RET"
				db_get "phpgroupware/db/admin/name"
				dbadmin="$RET"
				db_get "phpgroupware/db/admin/password"
				dbadmpass="$RET"
				db_get "phpgroupware/db/user/name"
				dbuser="$RET"
				db_get "phpgroupware/db/user/password"
				dbpass="$RET"
				db_get "phpgroupware/db/type"
				case "$RET" in
					PostgresQL)	dbtype="pgsql"	;;
					MySql)		dbtype="mysql"	;;
					mSQL)		dbtype="mysql"	;;
					ODBC_compliant)	dbtype="odbc"	;;
					Sybase)		dbtype="sybase"	;;
					MSsql)		dbtype="mssql"	;;
					Oracle)		dbtype="oracle"	;;
				esac

				# User may have choosen to continue anyway
				# with the configuration, so:
				if [ "$dbtype" = "pgsql" -o "$dbtype" = "mysql" ] ; then
					. /usr/share/wwwconfig-common/${dbtype}-createuser.sh
					. /usr/share/wwwconfig-common/${dbtype}-createdb.sh
				fi
			fi

			# Get configuration passwords.
			db_get "phpgroupware/header/password"
			header_password="$RET"
			db_get "phpgroupware/configuration/password"
			config_password="$RET"

			# Build phpgroupware header manager configuration file.
			perl -e "
				\$header_password	= '$header_password' ;
				\$config_password	= '$config_password' ;
				\$dbserver       	= '$dbserver' ;
				\$dbname         	= '$dbname' ;
				\$dbtype         	= '$dbtype' ;
				\$dbuser         	= '$dbuser' ;
				\$dbpass         	= '$dbpass' ;
				while (<>) {
					s#{SERVER_ROOT}#/usr/share/phpgroupware/# ;
					s#{INCLUDE_ROOT}#/usr/share/phpgroupware/# ;
					s/{ENABLE_MCRYPT}/false/ ;
					s/{MCRYPT_VERSION}/none/ ;
					s/{MCRYPT_IV}/none/ ;
					s/{HEADER_ADMIN_PASSWORD}/\$header_password/ ;
					s/{CONFIG_PASS}/\$config_password/ ;
					s/{DB_HOST}/\$dbserver/ ;
					s/{DB_NAME}/\$dbname/ ;
					s/{DB_TYPE}/\$dbtype/ ;
					s/{DB_USER}/\$dbuser/ ;
					s/{DB_PASS}/\$dbpass/ ;
					s/{DOMAIN_SELECTBOX}/false/ ;
					s/{DB_PERSISTENT}/false/ ;
					s/{SESSIONS_TYPE}/db/ ;
					print ;
				}
			" < $template > $config
			# Reset all the passwords
			db_reset "phpgroupware/header/password"
			db_reset "phpgroupware/configuration/password"
			db_reset "phpgroupware/db/user/password"
			chown www-data.www-data /var/lib/phpgroupware/files
			chmod -R ug+rw /var/lib/phpgroupware/files
			chmod -R o+w /var/lib/phpgroupware/files
			servers="exim apache-ssl apache mysql postresql"
			. /usr/share/wwwconfig-common/restart.sh
			chown www-data.www-data $config
			chmod 600 $config
		fi

	;;

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

	;;

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

db_stop

#DEBHELPER#

exit 0
