#!/bin/sh
set -e

. /usr/share/debconf/confmodule

file="$1"

NL="
"

log() {
	logger -t apt-setup "$@"
}
warning() {
	log "warning: $@"
}

ask_no_mirror() {
	db_input critical apt-setup/no_mirror
	db_go || exit 10

	db_get apt-setup/no_mirror
	if [ "$RET" = true ]; then
		exit 1
	fi
}

# Ask if a mirror should be used if the base system can be installed from CD
# Default value and priority vary with the CD type used and number of CDs
# that has been scanned in 41cdset
if [ -e /cdrom/.disk/base_installable ] || [ "$OVERRIDE_BASE_INSTALLABLE" ]; then
	if ! search-path choose-mirror; then
		warning "choose-mirror is not available; cannot offer network mirror"
		exit 1
	fi

	cd_type=unknown
	if [ -e /cdrom/.disk/cd_type ]; then
		cd_type=$(cat /cdrom/.disk/cd_type)
	fi
	cd_count=$(grep "^deb cdrom:" $ROOT/etc/apt/sources.list.new | wc -l)

	no_network=
	if db_get netcfg/dhcp_options && \
	   [ "$RET" = "Do not configure the network at this time" ]; then
		no_network=1
	fi

	case "$cd_type" in
	    not_complete)
		use_mirror=true
		if [ "$no_network" ]; then
			ask_no_mirror
			exit 0
		else
			use_prio=medium
		fi
		db_metaget apt-setup/use/netinst description
		db_subst apt-setup/use_mirror EXPLANATION "$RET"
		;;
	    full_cd*)
		use_mirror=true
		use_prio=high
		if [ $cd_count -le 8 ]; then
			if [ $cd_count -eq 1 ]; then
				db_metaget apt-setup/use/cd description
			elif [ $cd_count -le 3 ]; then
				db_metaget apt-setup/use/cd-set1 description
				RET="$(printf "$RET" "$cd_count")"
			else
				use_mirror=false
				db_metaget apt-setup/use/cd-set2 description
				RET="$(printf "$RET" "$cd_count")"
			fi
			explanation="$RET"
			if [ $cd_count -le 3 ]; then
				db_metaget apt-setup/use/inet1 description
				explanation="$explanation $RET"
				db_metaget apt-setup/use/cd-note description
				explanation="$explanation$NL$NL$RET"
			else
				db_metaget apt-setup/use/inet2 description
				explanation="$explanation $RET"
			fi
			db_subst apt-setup/use_mirror EXPLANATION "$explanation"
		else
			use_mirror=false
			use_prio=low
		fi
		;;
	    dvd*)
		use_mirror=false
		use_prio=high
		if [ $cd_count -eq 1 ]; then
			db_metaget apt-setup/use/dvd description
			explanation="$RET"
			db_metaget apt-setup/use/inet2 description
			explanation="$explanation $RET"
			db_subst apt-setup/use_mirror EXPLANATION "$explanation"
		else
			use_prio=low
		fi
		;;
	    bluray*)
		use_mirror=false
		use_prio=low
		;;
	    *)
		use_mirror=true
		use_prio=high
		;;
	esac

	if [ "$no_network" ]; then
		use_mirror=false
	fi

	# Only set default if not preseeded or already asked
	# Hack alert: for this to work no default is set in template
	db_get apt-setup/use_mirror
	[ "$RET" ] || db_set apt-setup/use_mirror $use_mirror

	db_input $use_prio apt-setup/use_mirror || [ $? -eq 30 ]
	db_go || exit 10

	db_get apt-setup/use_mirror
	if [ "$RET" = false ]; then
		exit 1
	fi

	# Default suite to codename; choose-mirror will determine actual suite
	if db_get cdrom/codename && [ "$RET" ]; then
		db_set mirror/suite $RET
	fi

	while true; do
		CODE=0
		choose-mirror -n || CODE=$?	# no progress bar
		db_capb backup progresscancel
		if [ $CODE -eq 0 ]; then
			break
		elif [ $CODE -eq 10 ]; then
			# User backed out of choose-mirror
			ask_no_mirror
		else
			logger -t apt-setup "choose-mirror failed with error code $CODE"
			exit $CODE
		fi
	done
fi

# Abort if no mirror is configured.
db_get mirror/protocol
protocol="$RET"
db_get mirror/codename
codename="$RET"
db_get mirror/$protocol/hostname
hostname="$RET"
db_get mirror/$protocol/directory
directory="/${RET#/}"
if [ -z "$hostname" ] || [ -z "$directory" ]; then
	exit 1
fi

STATE=1
while true; do
	case "$STATE" in
	    1)
		db_input low apt-setup/non-free || true
		;;
	    2)
		# If they chose not to use non-free, ask about
		# contrib, with a default of no. If they chose to
		# use non-free, they get contrib too..
		db_get apt-setup/non-free
		if [ "$RET" = false ]; then
			db_fget apt-setup/contrib seen
			if [ "$RET" = false ]; then
				db_set apt-setup/contrib false
			fi
			db_input low apt-setup/contrib || true
		else
			db_fget apt-setup/contrib seen
			if [ "$RET" = false ]; then
				db_set apt-setup/contrib true
			fi
		fi
		;;
	    3)
		db_input low apt-setup/enable-source-repositories || true
		;;
	    *)
		break
		;;
	esac

	if db_go; then
		STATE=$(($STATE + 1))
	else
		STATE=$(($STATE - 1))
	fi
done
if [ $STATE -eq 0 ]; then
	exit 10
fi

dists="main"
db_get apt-setup/non-free
if [ "$RET" = true ]; then
	dists="$dists non-free"
fi
db_get apt-setup/contrib
if [ "$RET" = true ]; then
	dists="$dists contrib"
fi

done=""
while [ ! "$done" ]; do
	db_get mirror/codename
	codename="$RET"
	db_get mirror/protocol
	protocol="$RET"
	db_get mirror/$protocol/hostname
	hostname="$RET"
	db_get mirror/$protocol/directory
	directory="/${RET#/}"

	case $protocol in
	    http|https)
		db_get mirror/$protocol/proxy
		proxy="$RET"
		if [ -n "$proxy" ]; then
			if ! grep -iq "Acquire::$protocol::Proxy" $ROOT/etc/apt/apt.conf.new; then
				echo "Acquire::$protocol::Proxy \"$proxy\";" >> $ROOT/etc/apt/apt.conf.new
			fi
		fi
		;;
	esac

	echo "deb $protocol://$hostname$directory $codename $dists" > $file
	
	if apt-setup-verify --from $PROGRESS_FROM --to $PROGRESS_TO $file; then
		done=1
	else
		db_set apt-setup/mirror/error Retry
		db_input critical apt-setup/mirror/error || true
		db_go || exit 10
		db_get apt-setup/mirror/error
		if [ "$RET" = "Change mirror" ]; then
			choose-mirror -n || true
			db_capb backup progresscancel
		elif [ "$RET" = Ignore ]; then
			exit 1
		fi
	fi
done

deb_src="deb-src"
db_get apt-setup/enable-source-repositories
if [ "$RET" = false ]; then
	deb_src="# deb-src"
fi

echo "$deb_src $protocol://$hostname$directory $codename $dists" >> $file
