#!/bin/sh -e

. /usr/share/debconf/confmodule

db_version 2.0

if [ -n "$2" ] ; then
	if dpkg --compare-versions "$2" le "0.9.14-0.RC1.1" ; then
		# Reset the passwords.
		db_reset "phpgroupware/header/password" || true
		db_reset "phpgroupware/configuration/password" || true
		db_reset "phpgroupware/db/user/password" || true
		db_reset "phpgroupware/header/password/confirm" || true
		db_reset "phpgroupware/configuration/password/confirm" || true
		db_reset "phpgroupware/db/user/password/confirm" || true
		# Set all of them as seen.
		db_fset "phpgroupware/header/password" "seen" "true" || true
		db_fset "phpgroupware/configuration/password" "seen" "true" || true
		db_fset "phpgroupware/db/user/password" "seen" "true" || true
		db_fset "phpgroupware/header/password/confirm" "seen" "true" || true
		db_fset "phpgroupware/configuration/password/confirm" "seen" "true" || true
		db_fset "phpgroupware/db/user/password/confirm" "seen" "true" || true
	fi
fi

if [ "$1" = "reconfigure" ] ; then
	# Rotate old configuration
	config="/etc/phpgroupware/header.inc.php"
	for i in $(seq 8 -1 0) ; do
		if [ -f ${config}.$i ]
			then mv ${config}.$i ${config}.$(($i +1))
		fi
	done
	mv ${config} ${config}.0
fi
debug="false"
step=0
finished="false"

db_capb backup
while ! $finished ; do
	skip="false"
	forward=1
	backward=1
	password=""
	confirm=""
	case $step in
		0)	# Prompt user for notes.
			db_beginblock
				db_subst "phpgroupware/configuration/note" "site" `hostname -f`
				db_input low "phpgroupware/configuration/note" || true
				db_input high "phpgroupware/installation/note_0_9_13_14RC1" || true
			db_endblock
			backward=0
		;;
		1)	# Ask for web server type.
			db_input medium "phpgroupware/webserver" || true
		;;
		2)	# Prompt user for ``Header Admin'' password.
			db_fset "phpgroupware/header/password/mismatch" "seen" "flase" || true
			db_beginblock
				db_input critical "phpgroupware/header/password" || true
				db_input critical "phpgroupware/header/password/confirm" || true
			db_endblock
		;;
		3)	# Check if passwords match.
			db_get "phpgroupware/header/password"
			password="$RET"
			db_get "phpgroupware/header/password/confirm"
			confirm="$RET"
			if [ "$password" != "$confirm" ] ; then
				# Reset the template used.
				db_reset "phpgroupware/header/password" || true
				db_fset  "phpgroupware/header/password" "seen" "false" || true
				db_reset "phpgroupware/header/password/confirm" || true
				db_fset  "phpgroupware/header/password/confirm" "seen" "false" || true
				# Promt the user.
				db_input critical "phpgroupware/header/password/mismatch" || true
				# Do one step back anyway.
				forward=-1
			else
				# Do not prompt, go to the next step.
				skip="true"
			fi
		;;
		4)	# Prompt for ``Setup/Config Admin'' password.
			db_fset "phpgroupware/configuration/password/mismatch" "seen" "false" || true
			db_beginblock
				db_input critical "phpgroupware/configuration/password" || true
				db_input critical "phpgroupware/configuration/password/confirm" || true
			db_endblock
			# Skip previous step
			backward=2
		;;
		5)	# Check if passwords match.
			db_get "phpgroupware/configuration/password"
			password="$RET"
			db_get "phpgroupware/configuration/password/confirm"
			confirm="$RET"
			if [ "$password" != "$confirm" ] ; then
				# Reset the template used.
				db_reset "phpgroupware/configuration/password" || true
				db_fset  "phpgroupware/configuration/password" "seen" "false" || true
				db_reset "phpgroupware/configuration/password/confirm" || true
				db_fset  "phpgroupware/configuration/password/confirm" "seen" "false" || true
				# Promt the user.
				db_input critical "phpgroupware/configuration/password/mismatch" || true
				# Do one step back anyway.
				forward=-1
			else
				# Do not prompt, go to the next step.
				skip="true"
			fi
		;;
		6)	# Ask wich type of DB phpGroupWare should use.
			db_beginblock
				# The note is not critical, but i want user to understand the
				# installation process.
				db_input critical "phpgroupware/db" || true
				db_input critical "phpgroupware/db/type" || true
			db_endblock
			# Skip previous step.
			backward=2
		;;
		7)	# Check for the DBMS type user wish to use.
			db_get "phpgroupware/db/type"
			dbtype="$RET"
			if [ $dbtype != "PostgresQL" -a $dbtype != "MySql" ] ; then
				# DB is neither PostgresQL nor MySql
				db_input critical "phpgroupware/db/setup/abort" || true
				# If user wish to go ahead anyway, skip the DB setup.
				onsuccess='db_set "phpgroupware/db/setup/skip" "true" || true ; finished="true"'
			fi
		;;
		8)	# Ask on wich host the DBMS is installed.
			db_input critical "phpgroupware/db/host" || true
			# Skip previous step.
			backward=2
		;;
		9)	# Check for the DBMS hostname (most for PostgresQL).
			db_get "phpgroupware/db/host" || true
			dbhost="$RET"
			# If DBMS is PostgresQL...
			if [ ${dbtype} = "PostgresQL" ] ; then
				# ... And is on localhost.
				if [ "${dbhost}" = "localhost" ] ; then
					. /usr/share/wwwconfig-common/pgsql-allowip.sh
					if [ "$status" = "error" ] ; then
						# Some error occurred, so skip configuration.
						db_subst "phpgroupware/postgres/error" "error" "$error" || true
						db_input critical "phpgroupware/postgres/error" || true
						onsuccess='db_set "phpgroupware/db/setup/skip" "true" || true ; finished="true"'
					fi
				else
					# PostgresQL is on remote host, so...
					db_input critical "phpgroupware/postgres/remote" || true
					# If user wish to go ahead anyway
					onsuccess='db_set "phpgroupware/db/setup/skip" "true" || true ; finished="true"'
				fi
			fi
		;;
		10)	# Get the database administrator name and password.
			db_beginblock
				db_input critical "phpgroupware/db/admin/name" || true
				db_input critical "phpgroupware/db/admin/password" || true
			db_endblock
			# Skip previous step.
			backward=2
		;;
		11)	# Ask for DB name.
			db_input critical "phpgroupware/db/name" || true
		;;
		12)	# Get the DBMS account username
			db_input critical "phpgroupware/db/user/name" || true
		;;
		13) # Get the DBMS account password
			db_fset "phpgroupware/db/user/password/mismatch" "seen" "false" || true
			db_beginblock
				db_input critical "phpgroupware/db/user/password" || true
				db_input critical "phpgroupware/db/user/password/confirm" || true
			db_endblock
		;;
		14)	# Check if passwords match.
			db_get "phpgroupware/db/user/password"
			password="$RET"
			db_get "phpgroupware/db/user/password/confirm"
			confirm="$RET"
			if [ "$password" != "$confirm" ] ; then
				# Reset the template used
				db_reset "phpgroupware/db/user/password" || true
				db_fset  "phpgroupware/db/user/password" "seen" "false" || true
				db_reset "phpgroupware/db/user/password/confirm" || true
				db_fset  "phpgroupware/db/user/password/confirm" "seen" "false" || true
				# Promt the user
				db_input critical "phpgroupware/header/password/mismatch" || true
				# Do one step back anyway.
				forward=-1
			else
				# Do not prompt, go to the next step.
				skip="true"
			fi
		;;
		15)	# Ask for deleting all the database on package purge.
			db_input medium "phpgroupware/postrm" || true
			onsuccess='finished="true"'
			# Skip previous step
			backward=2
		;;
		*)
			skip="true"
			message="Unknown step #$tep."
			if [ $step -lt 0 ] ; then
				step=-1
			elif [ $step -gt 11 ] ; then
				finished="true"
			fi
		;;
	esac
	
	if $skip ; then
		next=$(($step + 1))
		eval $onsuccess
		onsuccess=''
	else
		db_title "phpGroupWare" || true
		if db_go ; then
			next=$(($step + $forward))
			eval $onsuccess
			onsuccess=''
		else
			next=$(($step - $backward))
			onsuccess=''
		fi
	fi
	if $debug ; then
		db_subst "phpgroupware/debug" "debug" "$message" || true
		db_title "D: Step #$step"
		db_input critical "phpgroupware/debug" || true
		db_go
		db_fset "phpgroupware/debug" "seen" "false" || true
	fi
	step=$next
done

db_stop
exit 0
