#!/bin/sh -e

log=/var/log/messages
queue=/var/lib/apt-install/queue

mountpoints () {
	cut -d" " -f2 /proc/mounts | sort | uniq
}

# Create a policy-rc.d to stop maintainer scripts using invoke-rc.d from
# running init scripts. In case of maintainer scripts that don't use
# invoke-rc.d, add a dummy start-stop-daemon.
disable_daemons () {
	cat > /target/usr/sbin/policy-rc.d <<EOF
#!/bin/sh
exit 101
EOF
	chmod a+rx /target/usr/sbin/policy-rc.d
	
	if [ -e /target/sbin/start-stop-daemon ]; then
		mv /target/sbin/start-stop-daemon /target/sbin/start-stop-daemon.REAL
	fi
	cat > /target/sbin/start-stop-daemon <<EOF
#!/bin/sh
echo 1>&2
echo 'Warning: Fake start-stop-daemon called, doing nothing.' 1>&2
exit 0
EOF
	chmod a+rx /target/sbin/start-stop-daemon
}

enable_daemons () {
	rm -f /target/usr/sbin/policy-rc.d
	
	rm /target/sbin/start-stop-daemon
	mv /target/sbin/start-stop-daemon.REAL /target/sbin/start-stop-daemon
}

# If we don't have a working mirror yet, only queue the package;
# it will be installed later by the postinst in base-installer.
if [ ! -f /target/etc/apt/sources.list ] ; then
	# Add to list of extra packages to be installed into /target/.
	mkdir -p /var/lib/apt-install
	touch $queue
	for pkg in $@ ; do
		if ! grep -q "^$pkg$" $queue; then
			echo "$pkg" >> $queue
		fi
	done

	exit 1 # Return error as the package is not ready to be used yet.
fi

# The C.UTF-8 locale is not usable inside /target/.  Unset it here to avoid
# warnings like 'perl: warning: Setting locale failed.'.
if [ "$LANG" = "C.UTF-8" ] ; then
	unset LANG
fi

# Try to enable proxy when using HTTP.  What about using ftp_proxy for
# FTP sources?
RET=`debconf-get mirror/protocol || true`
if [ "http" = "$RET" ]; then
	# try to find http proxy
	RET=`debconf-get mirror/http/proxy || true`
	if [ "$RET" ] ; then
		http_proxy="$RET"
		export http_proxy
	fi
fi

# Unset to avoid problems with packages using debconf.  This should
# avoid the following error when installing dash:
# "/var/lib/dpkg/info/ash/config: 1: Bad file descriptor"
# The problem only appear if /usr/share/debconf/confmodule is sourced
# into this script.
unset DEBIAN_HAS_FRONTEND
unset DEBIAN_FRONTEND
unset DEBCONF_FRONTEND
unset DEBCONF_REDIR
# Avoid debconf mailing notes at this point, or asking questions.
DEBCONF_ADMIN_EMAIL=""
export DEBCONF_ADMIN_EMAIL
DEBIAN_FRONTEND=noninteractive
export DEBIAN_FRONTEND

# Record the current mounts
mountpoints > /tmp/mount.pre

# Some packages (eg. the kernel-image package) require a mounted /proc/
# Only mount it if it isn't mounted already
if [ ! -f /target/proc/cmdline ] ; then
	mount -t proc proc /target/proc
fi

APTOPTS='-o Acquire::gpgv::Options::=--ignore-time-conflict'
if [ -f /cdrom/.disk/base_installable ]; then
	APTOPTS="$APTOPTS -o APT::Get::AllowUnauthenticated=true"
fi

disable_daemons
ERRCODE=0
chroot /target apt-get $APTOPTS -y install $@ < /dev/null >> $log 2>&1 || ERRCODE=$?
enable_daemons

# Undo the mounts done by the packages during installation.
# Reverse sorting to umount the deepest mount points first.
# Items with count of 1 are new.
for dir in $( (cat /tmp/mount.pre /tmp/mount.pre; mountpoints ) | \
	     sort -r | uniq -c | grep "[[:space:]]1[[:space:]]" | \
	     sed "s/[[:space:]]*[0-9][[:space:]]//"); do
	if ! umount $dir ; then
		logger -t apt-install "warning: Unable to umount '$dir'"
	fi
done
rm -f /tmp/mount.pre

if [ "$ERRCODE" != 0 ]; then
	exit 1
else
	exit 0
fi
