#!/bin/bash

set -e

if [ -f /usr/share/debconf/confmodule ]; then
  . /usr/share/debconf/confmodule
fi

if [ "$DPKG_DEBUG" = "developer" ]; then
	set -x
fi

db_version 2.0
db_capb backup

step=1
finished="false"

while ! $finished; do
	skip="false"
	case $step in
		1)
			db_input high "frontaccounting/webserver" || true
			next=2
			previous="-1"
		;;
		2)
			db_get "frontaccounting/db_initialized" || true
			previous=1
			dbinitialized="$RET"
			if [ "$dbinitialized" = "true" ]; then
				next=12
			else
				next=3
			fi
			;;
		3)
			if which mysql >/dev/null 2>&1 ; then
				db_input high "frontaccounting/skipdb" || true
			else
				# No mysql client available.
				# skip database setup and tell user to do it.
				db_set "frontaccounting/skipdb" true || true
				db_input high "frontaccounting/manualdb" || true
			fi
			next=4
			previous=1
			;;
		4)
			db_get "frontaccounting/skipdb" || true
			skipdb="$RET"
			previous=3
			if [ "$skipdb" = "true" ]; then
				next=12
			else
				next=5
			fi
		;;
		5)
			db_input critical "frontaccounting/db_host" || true
			previous=3
			next=6
		;;
		6)
			db_beginblock
				db_input critical "frontaccounting/db_admin_user" || true
				db_input critical "frontaccounting/db_admin_pass" || true
			db_endblock
			next=7
			previous=5
		;;
		7)
			db_beginblock
				db_input critical "frontaccounting/db_name" || true
				db_input critical "frontaccounting/db_prefix" || true
				db_input critical "frontaccounting/company" || true
				db_input critical "frontaccounting/db_demo" || true
			db_endblock
      next=8
      previous=6
		;;
		8)
			db_input critical "frontaccounting/db_user" || true
			next=9
			previous=7
		;;
		9)
			db_beginblock
				db_input critical "frontaccounting/db_pass" || true
				db_input critical "frontaccounting/db_pass_conf" || true
			db_endblock
			next=10
			previous=8
		;;
		10)
			# Verify passwords match
			db_get "frontaccounting/db_pass"
			password="$RET"
			db_get "frontaccounting/db_pass_conf"
			confirm="$RET"

			if [ "$password" != "$confirm" ] ; then
				db_reset "frontaccounting/db_pass" || true
				db_fset  "frontaccounting/db_pass" "seen" "false" || true
				db_reset "frontaccounting/db_pass_conf" || true
				db_fset  "frontaccounting/db_pass_conf" "seen" "false" || true
				db_input critical "frontaccounting/pass_mismatch" || true
				next=9
				previous=9
			else
				skip="true"
				next=11
			fi
		;;
		11)
			db_beginblock
				db_input critical "frontaccounting/db_fadmin_pass" || true
				db_input critical "frontaccounting/db_fadmin_conf" || true
			db_endblock
			next=12
			previous=9
		;;
		12)
			# Verify passwords match
			db_get "frontaccounting/db_fadmin_pass"
			password="$RET"
			db_get "frontaccounting/db_fadmin_conf"
			confirm="$RET"

			if [ "$password" != "$confirm" ] ; then
				db_reset "frontaccounting/db_fadmin_pass" || true
				db_fset  "frontaccounting/db_fadmin_pass" "seen" "false" || true
				db_reset "frontaccounting/db_fadmin_conf" || true
				db_fset  "frontaccounting/db_fadmin_conf" "seen" "false" || true
				db_input critical "frontaccounting/fapass_mismatch" || true
				next=11
				previous=11
			else
				skip="true"
				next=13
			fi
		;;
		13)
			db_input medium "frontaccounting/postrm" || true
			next=14
			previous=12
		;;
		14)
			db_get "frontaccounting/webserver" || true
			db_subst "frontaccounting/restart-webserver" webserver $RET
			db_input high "frontaccounting/restart-webserver" || true
			db_go
			next=-1
			if [ "$skipdb" = "true" ]; then
				previous=4
			elif [ "$dbinitialized" = "true" ]; then
				previous=1
			else
				previous=9
			fi
		;;
		-1)
			finished="true"
		;;
		*)
			skip="true"
			message="Unknown step #$step"
			if [ $step -le 0 ]; then
				finished="true"
			fi
	esac

	if ! $skip; then
		#db_title "FrontAccounting" || true
		if db_go; then
			step=$next
		else
			step=$previous
		fi
	else
		step=$next
	fi
done

db_stop
exit 0
