#! /bin/sh
#set -e

. /usr/share/debconf/confmodule

db_get languagechooser/language-name
db_subst ubiquity/summary LANGUAGE "$RET"
if db_get console-setup/variant && [ "$RET" ]; then
	db_subst ubiquity/summary KEYMAP "$RET"
else
	db_get console-setup/layout
	db_subst ubiquity/summary KEYMAP "$RET"
fi
db_get mythbuntu/install_type
db_subst ubiquity/summary INSTALLTYPE "$RET"
db_get mythbuntu/mytharchive
db_subst ubiquity/summary MYTHARCHIVE "$RET"
db_get mythbuntu/mythbrowser
db_subst ubiquity/summary MYTHBROWSER "$RET"
db_get mythbuntu/mythcontrols
db_subst ubiquity/summary MYTHCONTROLS "$RET"
db_get mythbuntu/mythdvd
db_subst ubiquity/summary MYTHDVD "$RET"
db_get mythbuntu/mythflix
db_subst ubiquity/summary MYTHFLIX "$RET"
db_get mythbuntu/mythgallery
db_subst ubiquity/summary MYTHGALLERY "$RET"
db_get mythbuntu/mythgame
db_subst ubiquity/summary MYTHGAME "$RET"
db_get mythbuntu/mythmusic
db_subst ubiquity/summary MYTHMUSIC "$RET"
db_get mythbuntu/mythnews
db_subst ubiquity/summary MYTHNEWS "$RET"
db_get mythbuntu/mythphone
db_subst ubiquity/summary MYTHPHONE "$RET"
db_get mythbuntu/mythvideo
db_subst ubiquity/summary MYTHVIDEO "$RET"
db_get mythbuntu/mythweather
db_subst ubiquity/summary MYTHWEATHER "$RET"
db_get mythbuntu/mythweb
db_subst ubiquity/summary MYTHWEB "$RET"
db_get mythbuntu/officialthemes
db_subst ubiquity/summary OFFICIAL "$RET"
db_get mythbuntu/communitythemes
db_subst ubiquity/summary COMMUNITY "$RET"
db_get mythbuntu/ati
db_subst ubiquity/summary ATI "$RET"
db_get mythbuntu/nvidia
db_subst ubiquity/summary NVIDIA "$RET"
db_get mythbuntu/tvout
db_subst ubiquity/summary TVOUT "$RET"
db_get mythbuntu/tvstandard
db_subst ubiquity/summary TVSTANDARD "$RET"
db_get mythtv/mysql_mythtv_user
db_subst ubiquity/summary MYSQLUSER "$RET"
db_get mythtv/mysql_mythtv_password
if [ -z "$RET" ]; then
	set +e
        NEW="$(pwgen -s 8)"
        set -e
	db_set mythtv/mysql_mythtv_password "$NEW"
	db_subst ubiquity/summary MYSQLPASS "$NEW"
else
	db_subst ubiquity/summary MYSQLPASS "$RET"
fi
db_get mythtv/mysql_host
if [ -z "$RET" ]; then
	db_set mythtv/mysql_host "localhost"
	db_subst ubiquity/summary MYSQLSERVER "localhost"
else
	db_subst ubiquity/summary MYSQLSERVER "$RET"
fi
db_get mythtv/mysql_mythtv_dbname
db_subst ubiquity/summary MYSQLDATABASE "$RET"
db_get passwd/user-fullname
db_subst ubiquity/summary FULLNAME "$RET"
db_get passwd/username
db_subst ubiquity/summary USERNAME "$RET"
db_get time/zone # actually continent/city (usually)
db_subst ubiquity/summary LOCATION "$RET"

# Migration Assistant
db_capb escape
ret=""
newline='
'
db_get migration-assistant/partitions
OLD_IFS="$IFS"
IFS='
'
partitions="$(echo "$RET" | sed -e 's/, /\n/g')"
for choice in $partitions; # Microsoft Windows XP Professional (/dev/hda1)
do
	ret="$ret$choice:$newline"
	part="$(expr match "$choice" '.*/\(.*\))$')" # hda1
	
	db_get "migration-assistant/$part/users"
	if [ -z "$RET" ]; then
		ret="${ret}None$newline"
	fi
	users="$(echo "$RET" | sed -e 's/, /\n/g')"
	for user in $users; do
		ret="$ret$user: "
		formatted_user="$(echo "$user" | sed -e 's/ /:/g')"
		db_get "migration-assistant/$part/$formatted_user/items"
		ret="$ret$RET$newline"
	done
	ret="$ret$newline"
done
IFS="$OLD_IFS"
db_subst ubiquity/summary MIGRATE "${ret%$newline}"
db_capb

# We need to run this to get hold of the partitioning summary. Our component
# takes care of ensuring that changes are not actually committed.
if [ "$PARTMAN_UPDATE_BEFORE_COMMIT" ]; then
	partman-commit || true
fi

db_capb escape
db_get ubiquity/partman-made-changes
if [ "$RET" = true ]; then
	db_metaget partman/confirm Extended_description
else
	db_metaget partman/confirm_nochanges Extended_description
fi
db_subst ubiquity/summary PARTMAN_CHANGES \
	"$(printf %s "$RET" | debconf-escape -e)"
db_capb

db_input high ubiquity/summary || true
db_go

exit 0
