#!/bin/sh -e
# Set up apt sources list.

# On new installs, comment out the shipped sources.list, since it assumes
# they are on the network, which is wrong if this is a CD install (this may
# no longer br true; debootstrap generates a pretty decent sources.list).
# Anyway apt-setup generates a better one. Touch the file in any case, because
# apt-setup requires it exist.

export TMPFILE="$TMPDIR/.apt-src-info.$$"

PROBE=probe

touch /etc/apt/sources.list
if [ "$1" = "new" ]; then
	# Warty change: Attempt to guess the uri type from d-i first stage (See: #296 #353 #355)
	line="`grep -v "^#" /etc/apt/sources.list | grep "^deb " | head -n 1`"
	uritype="`echo "$line" | sed -e 's/deb\ //g' -e 's/:.*//g'`"
	url=
	scanned=
	# if uritype is unknown, we need to tell it to apt-setup that will
	# prompt with higher priority
	case "$uritype" in
		http|ftp)
			url="`echo $line | sed -e 's/.*http\:\/\///' -e 's/\/.*//' -e 's/\ .*//'`"
		;;
		cdrom)
			# d-i has already scanned the CD-ROM
			PROBE=
			scanned=yes
		;;
		file)
			# cdrom install from d-i is a special case
			if [ "`echo "$line" | grep "///cdrom"`" ]; then
				uritype="cdrom"
			fi
		;;
		*)
			uritype="unknown"
		;;
	esac
	echo "Uritype: $uritype" > "$TMPFILE"
	if [ "$url" ]; then echo "Mirror: $url" >> "$TMPFILE"; fi
	if [ "$scanned" ]; then
		echo "Scanned: $scanned" >> "$TMPFILE"
	else
		sed 's/^\([^#]\)/#\1/' /etc/apt/sources.list > /etc/apt/sources.list.new
		mv -f /etc/apt/sources.list.new /etc/apt/sources.list
	fi
fi

# Probe for cd's in the drive prior to setting up apt.
apt-setup $PROBE

# Clean after us (in an ideal world it would be .$$)
rm -f "$TMPDIR/.apt-src-info.*"

clear
