#!/bin/sh
# Run various programs to select packages to install.
set -e

case "$1" in
''|new)
	# Make dpkg not background itself to get a shell.
	export DPKG_NO_TSTP="yes"

	# Set DEBIAN_FRONTEND, since some evil postinst scripts still
	# check it directly.
	if [ -z "$DEBIAN_FRONTEND" ] ; then
		DEBIAN_FRONTEND=$($0 get_frontend 4>&1 || true)
		if [ "$DEBIAN_FRONTEND" ] ; then
			export DEBIAN_FRONTEND
		else
			unset DEBIAN_FRONTEND || true
		fi
	fi

	# scrollkeeper takes forever, so we divert it for the duration.
	dpkg-divert --package base-config --rename --quiet --add \
		/usr/bin/scrollkeeper-update
	dpkg-divert --package base-config --rename --quiet --add \
		/usr/bin/scrollkeeper-rebuilddb
	ln -sf /bin/true /usr/bin/scrollkeeper-update
	ln -sf /bin/true /usr/bin/scrollkeeper-rebuilddb

	# Make popularity-contest be selected for installation by default. It
	# lets the user choose whether or not to enable it. We need more
	# people using this so we can hope to get better data about who is
	# using what packages in debian.
	# (Doesn't work with new tasksel.)
	#echo popularity-contest install | dpkg --set-selections
	
	# X needs some packages installed before its debconf config is run
	# to make it do hardware autodetection. The only way to make sure these
	# are installed properly is to install them now, before packages are
	# selected. This way, even if the user picks xserver-xfree86 in
	# aptitude and installs using aptitude, they will be available.
	for pkg in mdetect xresprobe laptop-detect; do
		if ! dpkg --get-selections | grep "$pkg" | grep -q install; then
			if apt-get -y -f install "$pkg"; then
				extra="$pkg $extra"
			fi
		fi
	done

	if which aptitude >/dev/null 2>&1; then
		if [ "$1" != new ]; then
			DEBIAN_PRIORITY=high aptitude --without-recommends \
				|| true
		elif ! DEBIAN_PRIORITY=high aptitude --without-recommends -y \
				install '~tubuntu-desktop'; then
			$0 failure
			DEBIAN_PRIORITY=high aptitude --without-recommends \
				|| true
			exit 0
		fi
	fi

	if [ "$KEEP_BASE_DEBS" != yes ]; then
		apt-get -f clean || true
	fi
	
	# Move cached packages that aren't installed by default into the apt
	# cache, so that installing them doesn't require the CD.
	if [ -d /var/cache/archive-copier/ship ]; then
		find /var/cache/archive-copier/ship -type f -print0 | \
			xargs -0r mv --target-directory /var/cache/apt/archives
		rmdir --ignore-fail-on-non-empty /var/cache/archive-copier/ship
		rmdir --ignore-fail-on-non-empty /var/cache/archive-copier
	fi

	# If X was not installed, remove the hardware detection
	# programs. Of course, this fails if the user manually choose
	# to install these, or wants them installed for some other reason.
	# But I cannot help that.
	if ! dpkg --get-selections | grep xserver-xfree86 | grep -q install; then
		if [ -n "$extra" ] ; then
			dpkg --purge $extra || true
		fi
	fi

	rm -f /usr/bin/scrollkeeper-update /usr/bin/scrollkeeper-rebuilddb
	dpkg-divert --package base-config --rename --quiet --remove \
		/usr/bin/scrollkeeper-update
	dpkg-divert --package base-config --rename --quiet --remove \
		/usr/bin/scrollkeeper-rebuilddb

	echo "Registering documentation, please wait..."
	scrollkeeper-rebuilddb -q || true
	;;
failure)
	# This branch is reached if there was some problem installing.
	# It uses debconf (which the other branch cannot use) to explain
	# the failure to the user.
	. /usr/share/debconf/confmodule
	db_settitle base-config/title
	db_fset base-config/install-problem seen false
	db_input critical base-config/install-problem || true
	db_go || true
	rm -f /usr/bin/scrollkeeper-update /usr/bin/scrollkeeper-rebuilddb
	dpkg-divert --package base-config --rename --quiet --remove \
		/usr/bin/scrollkeeper-update
	dpkg-divert --package base-config --rename --quiet --remove \
		/usr/bin/scrollkeeper-rebuilddb
	;;
esac
