#!/bin/sh
set -e

. /usr/share/debconf/confmodule

file="$1"

if [ ! -e /cdrom/.disk/base_installable ] && [ -z "$OVERRIDE_BASE_INSTALLABLE" ]; then
	exit 0
fi
if [ ! -s /cdrom/.disk/info ]; then
	exit 0
fi
REMOUNT_CD=true
if [ "$OVERRIDE_LEAVE_CD_MOUNTED" ]; then
	REMOUNT_CD=
fi
cd_type=$(cat /cdrom/.disk/cd_type)
case $cd_type in
    */single)
	REMOUNT_CD=
	;;
esac

cdfstype=$(mount | grep "on /cdrom " | cut -d' ' -f5)
isofs=
if [ "$cdfstype" = "iso9660" ]; then
	isofs=1
fi
mounts=

remount_cd() {
	if [ "$ROOT" ] && [ -n "$isofs" ] && [ ! -d /hd-media ] && [ "$REMOUNT_CD" ]; then
		fs=iso9660
		if db_get cdrom-detect/cdrom_fs && [ "$RET" ]; then
			fs="$RET"
		fi
		db_get cdrom-detect/cdrom_device
		$logoutput mount -t "$fs" -o ro,exec $RET /cdrom || true
	fi
}

bail_out() {
	remount_cd
	rm -f $ROOT$tmp $ROOT$tmp~
	for mount in $mounts; do
		umount $ROOT$mount || true
	done
	rm -f /var/lib/install-cd.id

	db_input critical apt-setup/cdrom/failed || true
	db_go || exit 10
	exit 1
}

# Save identification of installation CD
save_label() {
	local ident

	if ! ident="$($logoutput_pass $chroot $ROOT apt-cdrom ident)"; then
		bail_out
	fi
	echo "$ident" | grep "^Identifying" | head -n1 | cut -d" " -f2 \
		>/var/lib/install-cd.id
	echo "$ident" | grep "^Stored label:" | head -n1 | sed "s/^[^:]*: //" \
		>>/var/lib/install-cd.id
}

logoutput=""
logoutput_pass=""
if [ "$CATCHLOG" ]; then
	logoutput="log-output -t apt-setup"
	logoutput_pass="$logoutput --pass-stdout"
fi

chroot=
if [ "$ROOT" ]; then
	chroot=chroot

	# Allow apt-cdrom to manage mounting/unmounting CDs in /target
	# (except for hd-media installs)
	if [ ! -d /hd-media ] && [ -n "$isofs" ] && [ "$REMOUNT_CD" ]; then
		rm -f $ROOT/etc/apt/apt.conf.d/00NoMountCDROM

		# Also removes the bind-mount in /target
		$logoutput umount /cdrom || true
	fi
fi

tmp=$($chroot $ROOT tempfile)

if [ ! -e $ROOT/proc/cmdline ]; then
	mount -t proc proc $ROOT/proc
	mounts="$mounts /proc"
fi
if [ ! -d $ROOT/sys/devices ]; then
	mount -t sysfs sysfs $ROOT/sys
	mounts="$mounts /sys"
fi
if [ "$ROOT" ]; then
	mount -o bind /dev $ROOT/dev
	mounts="$mounts /dev"
fi
# apt-cdrom can be interactive, avoid that
if $logoutput $chroot $ROOT apt-cdrom add \
   -o Dir::Etc::SourceList=$tmp \
   </dev/null; then
	cat $ROOT$tmp >> $file

	if [ "$ROOT" ] && [ -n "$isofs" ] && [ ! -d /hd-media ] && [ "$REMOUNT_CD" ]; then
		save_label
	fi
else
	bail_out
fi

remount_cd
rm -f $ROOT$tmp $ROOT$tmp~
for mount in $mounts; do
	umount $ROOT$mount || true
done
