#!/bin/sh -e
#
# base-config postinst
#
# This takes the answers to the questions the config script asked, and acts on
# them.

# We don't trap "" so that our children (i.e. dselect etc) don't ignore
# these signals.
trap : 1 2 3 15

# Automatically added by dh_installdocs
if [ "$1" = "configure" ]; then
	if [ -d /usr/doc -a ! -e /usr/doc/base-config -a -d /usr/share/doc/base-config ]; then
		ln -sf ../share/doc/base-config /usr/doc/base-config
	fi
fi
# End automatically added section


# Make dpkg not background itself to get a shell.
export DPKG_NO_TSTP="yes"

# Make sure this isn't the initial install onto boot-floppies
/usr/share/base-config/stage1 || exit 0

# TODO: Why is this done here? I'd like to move it elsewhere.
if [ -f /etc/inittab.real ]; then
	mv -f /etc/inittab.real /etc/inittab
	sync
	kill -HUP 1
fi

# Now a large block of code that deals with installing packages.
# If dpkg is currently running (say this package is being installed,
# vs. reconfigured), don't run this block; dpkg is not re-entrant.
if /usr/lib/base-config/checklock /var/lib/dpkg/lock; then
	# Remove pcmcia if it is not needed.
	/usr/share/base-config/stage2

	# Update available file along with all else; tasksel needs it.
	# The echo is there just in case it prompts for a keypress, which
	# should never happen.
	clear
	echo | dselect update || true

	# This returns 0 if tasksel should be run; 1 if dselect should be run
	# (and if dselect should be run, displays a dselect help message).
	if /usr/share/base-config/stage3; then
		clear
		PERL_BADLANG=0 tasksel -riq || true
		clear
		# Try to auto-detect X server iff the packages they selected
		# resulted in some x server already being selected. (In
		# parctice, this means task-x-window-system-core was
		# selected or pulled in by a dependency.)
		if [ -x /usr/bin/anXious ]; then
		   (apt-get -sy dselect-upgrade 2>&1) | grep ^Inst | grep -q xserver- && \
		   anXious || true
		   # anXious for some reason leaves the console in raw mode.
		   reset
		fi
		# Install selected packages. If necessary, prompt user to
		# retry.
		while ! PERL_BADLANG=0 /usr/lib/dpkg/methods/apt/install &&
		      /usr/share/base-config/stage6; do
			clear
		done
	else
		clear
		# Turn off i18n since perl doesn't like it.  -Joey
		#
		# XXXXX FIXME: actually perl is right, but overly picky:
		# LANG=ll is *not* correct. LANG=ll_CC is. Try:
		# LANG=fr perl -e 'print "test\n";'
		# LANG=fr_FR perl -e 'print "test\n";'
		# PERL_BADLANG=0 LANG=fr perl -e 'print "test\n";'
		# and boot-floppies should probably handle locales as a ll_CC
		# type variable since es_ES is notably different of es_MX
		# (or fr_FR and fr_CA, etc...)
		#
		#       -Vincent
		PERL_BADLANG=0 dselect || true
	fi

	# Ask if ppp should be turned off.
	/usr/share/base-config/stage4
fi

# Display any final messages.
/usr/share/base-config/stage5

# Delete dboostrap_settings file now that we're done with it
rm -f /root/dbootstrap_settings
