#!/bin/sh -e
# Run apt to install any selected packages.

if [ "$1" != retry ]; then
	# Make dpkg not background itself to get a shell.
	export DPKG_NO_TSTP="yes"

	# Clean up packages that debootstrap used to install the system.
	apt-get clean || true

	if /usr/lib/dpkg/methods/apt/install /var/lib/dpkg; then
		CLEAN=1
		clear
	else
		CLEAN=""
		if $0 retry; then
			# Tell base-config to jump back to stage 50,
			# which is tasksel, to let the user change their
			# selections if needed before retrying.
			exit 50
		fi
	fi

	# Frees up even more disk space, but only do it if the install
	# succeeded. If the install is failing and the user didn't opt to
	# retry, leave them around because they may be useful in fixing up
	# the system later.
	if [ -z "$NOCLEAN" ]; then
		apt-get clean || true
	fi
else
	# This branch is reached if there was some problem installing.
	# It uses debconf (which the other branch cannot use) to ask the
	# user if they want to retry the install. Returns true if the install
	# should be retried.
	. /usr/share/debconf/confmodule
	db_title 'Debian System Configuration'
	db_fset base-config/install-problem seen false
	db_input critical base-config/install-problem || true
	db_go || true
	db_get base-config/install-problem
	if [ "$RET" = true ]; then
	        exit 0
	else
	        exit 1
	fi
fi
