#! /bin/sh
#set -e

. /usr/share/debconf/confmodule

language_name=
db_get localechooser/languagelist
if [ "$RET" ]; then
	language_name="$(zgrep "^[^:]*:$RET:" \
		/usr/lib/ubiquity/localechooser/languagelist.data.gz | \
		head -n1 | cut -d: -f4)"
fi
db_subst ubiquity/summary LANGUAGE "$language_name"
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 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
	part="$(expr match "$choice" '.*/\(.*\))$')" # hda1
	db_get "migration-assistant/$part/users"
	[ "$RET" ] || continue

	ret="$ret$choice:$newline"
	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
done
IFS="$OLD_IFS"
db_subst ubiquity/summary MIGRATE \
	"$(printf %s "${ret%$newline}" | debconf-escape -e)"
db_capb

db_capb escape
db_get ubiquity/partman-confirm
db_metaget "partman/$RET" Extended_description
db_subst ubiquity/summary PARTMAN_CHANGES \
	"$(printf %s "$RET" | debconf-escape -e)"
db_capb

db_input high ubiquity/summary || true
db_go

exit 0
