#!/bin/sh
# This program handles ubuntu's second stage install -- after installing
# the base system and rebooting, the inittab runs this program.
set -e

DI_DB=/var/log/installer/cdebconf/questions.dat
LIBDIR=/usr/lib/base-config
MENUDIR=$LIBDIR/menu

TEXTDOMAIN=base-config
export TEXTDOMAIN

# Get the parameter past the script invocation.
if [ -z "$NEW" ] && [ "$1" = new ]; then
	NEW=$1
fi
export NEW

if [ -x /usr/bin/script ] && [ -z "$BASE_CONFIG_IN_SCRIPT" ]; then
	# We want this program to run inside script. So if it's not already
	# running, run script.
	export BASE_CONFIG_IN_SCRIPT=1
	# Timing data is collected, so the whole installation experience
	# can play back with the replay command.
	# TODO: could use script -c now..
	SHELL=$0 exec script -q -a /var/log/base-config.log -t 2>>/var/log/base-config.timings
	exit 1 # never reached
fi
SHELL=/bin/sh
export SHELL

gettext "Configuring the base system..."
echo

cleanup () {
	cd /
	if [ "$BC_TMPDIR" ]; then
		rm -rf $BC_TMPDIR
	fi
	if [ ! -z "$ORIG_PRIORITY" ]; then
		# Restore the original debconf priority.
		set_priority $ORIG_PRIORITY
	fi

	exit
}

get_priority () {
	echo GET debconf/priority | debconf-communicate | cut -d ' ' -f 2
}

set_priority () {
	echo "SET debconf/priority $1" | debconf-communicate >/dev/null
}

get_preseed_priority () {
	echo GET base-config/priority | debconf-communicate | cut -d ' ' -f 2
}

set_menu () {
	echo "SET base-config/main-menu $1" | debconf-communicate >/dev/null
}

if [ "$NEW" ]; then
	# Trap most signals because a ctrl-c killing base-config
	# in the middle of the second stage install would be bad.
	trap "" HUP INT QUIT TERM

	if [ "$KEEP_DEBS" != yes ]; then
		# Free up some disk space used by the debs that debootstrap
		# downloaded to install the base system. Those debs are mostly
		# useless now.
		apt-get clean || true
	fi
else 
	# Running again on an existing install. Just trap ctrl-c, and
	# cleanly exit.
	trap cleanup INT
fi

# HOME will not be set properly on initial base-config run.
if [ -z "$HOME" ] || [ "$HOME" = / ]; then
	HOME=/root
	export HOME
fi

ORIG_PRIORITY=$(get_priority)

set_priority "$(get_preseed_priority)"

# DEBIAN_PRIORITY overrides the system priority, which would cause a
# problem with priority changing, so if it's set, unset it.
if [ ! -z "$DEBIAN_PRIORITY" ]; then
	set_priority $DEBIAN_PRIORITY
	unset DEBIAN_PRIORITY
fi

# Read information from first stage installer and stuff it into the debconf
# db.
if [ -e $DI_DB ] && [ "$NEW" ]; then
	# Copy values from the d-i cdebconf database to the debconf
	# database. These values must have the same name in both
	# databases, and be of the same type, with the same allowed
	# values.
	debconf-copydb d-i configdb -c Name:d-i -c Driver:File \
		-c Filename:$DI_DB \
		--pattern='^(mirror/http/proxy|mirror/suite|debian-installer/keymap|debian-installer/country|debian-installer/language|debian-installer/locale|debian-installer/only-os|localechooser/supported-locales|base-config/.*)$'
	# Make sure that the templates of registered values are set to 
	# something sane. This is needed because sometimes d-i uses dummy
	# templates which are not in the debconf db.
	(for q in mirror/http/proxy mirror/suite \
	          debian-installer/keymap debian-installer/country \
		  debian-installer/language debian-installer/locale \
		  debian-installer/only-os localechooser/supported-locales; do
		echo "REGISTER $q $q"
	done) | debconf-communicate >/dev/null
fi

# Copy the base-config scripts into a temp directory. This is done so if
# base-config removes itself, it will not blow up.
BC_TMPDIR=/tmp/base-config.$$
mkdir $BC_TMPDIR
cp -pRL $LIBDIR $BC_TMPDIR
cd $BC_TMPDIR/base-config
# Various scripts use it too.
export BC_TMPDIR

# Set up to display the menu.
./prep-menu 1 $NEW

# Display the main menu. The show-menu helper program returns when the menu
# is done, or when the selected menu item needs to run outside debconf.
TMP=$(tempfile)
ADVANCE=0
while :; do
	# Gross, gross, gross..
	./show-menu $ADVANCE $NEW 5>$TMP
	menu_item=$(cat $TMP)
	
	if [ -z "$menu_item" ]; then
		# No item, so exit the menu.
		break
	else
		# Run menu item outside of debconf.
		if ./menu/$menu_item $NEW; then
			if [ -s jump-to ]; then
				# Jump to a specified menu item out of
				# order.
				set_menu "$(grep ",$(cat jump-to)" menu-mapping | cut -d , -f 1 | tail -n 1)"
				rm -f jump-to
				ADVANCE=0
			else
				# Advance to the next menu item.
				ADVANCE=1
			fi
			if grep -q 'Exit-Menu: true' menu/$menu_item.mnu; then
				# This menu item finished the install.
				break
			fi
		else
			ADVANCE=0
			# Drop priority to make sure the menu is displayed
			# since a menu item is failing.
			set_priority medium
		fi
	fi
	
	if grep -q '^Changes-Menu: true' menu/$menu_item.mnu; then
	        cp -pRL $LIBDIR $BC_TMPDIR || true
		./prep-menu 0 $NEW
	fi
done

cleanup
# That's all, folks! (On a new install, the user sees a login prompt next)
