#!/bin/sh

. /usr/share/debconf/confmodule

log() {
	logger -t net-retriever "$@"
}
error() {
	log "error: $@"
	exit 1
}

db_get mirror/protocol
protocol="$RET"
db_get mirror/$protocol/hostname
hostname="$RET"
db_get mirror/$protocol/directory
directory="$RET"

keyring=/usr/share/keyrings/archive.gpg

fetch() {
	fetch-url -c "${protocol}://${hostname}${directory}/$1" "$2"
}

checkmatch() {
	release="$1"
	packages="$2"
	pkgmd5="$3"
	pkgsize="$4"

	set -e
	sed -n '/^MD5Sum:$/ b LOOP; b; : PRINT; /:$/q; p; : LOOP; n; b PRINT' \
	       "$release" | (
		while read md5 size file; do
			if [ "$file" = "$packages" ]; then
				if [ "$md5" != "$pkgmd5" ]; then
					error "MD5 mismatch for $packages ($md5 != $pkgmd5)."
				fi
				if [ "$size" != "$pkgsize" ]; then
					error "Size mismatch for $packages ($size != $pkgsize)."
				fi
				return 0
			fi
		done
		error "$packages not found in $release."
	)
	set +e
}

read_gpg_status() {
	while read prefix keyword rest; do
		[ "$prefix" = '[GNUPG:]' ] || continue
		if [ "$keyword" = VALIDSIG ]; then
			exit 0
		fi
	done
	exit 1
}

cmd="$1"
shift

case "$cmd" in
    retrieve)
	fetch "$@"
	exit $?
	;;

    packages)
	rm -f "$1"
	touch "$1"

	# Setting codename to a suite is not very nice, but can do no harm
	if ! db_get mirror/udeb/suite || [ -z "$RET" ]; then
		if [ -f /etc/udebs-source ]; then
			RET=$(cat /etc/udebs-source)
		else
			db_get mirror/codename
		fi
	fi
	codename="$RET"

	Release="/tmp/net-retriever-$$-Release"
	fetch "dists/$codename/Release" "$Release" || exit $?
	# If gpgv and a keyring are installed, authentication is
	# mandatory by default.
	if type gpgv >/dev/null && [ -f "$keyring" ]; then
		if db_get debian-installer/allow_unauthenticated && [ "$RET" = true ]; then
			log "Not verifying Release signature: unauthenticated mode enabled"
		else
			if ! fetch "dists/$codename/Release.gpg" "$Release.gpg"; then
				error "dists/$codename/Release is unsigned."
			fi
			if ! log-output -t net-retriever --pass-stdout \
			     gpgv --status-fd 1 --keyring "$keyring" \
			     --ignore-time-conflict \
			     "$Release.gpg" "$Release" | read_gpg_status; then
				error "Bad signature on $Release."
			fi
		fi
	else
		log "Not verifying Release signature: gpgv not available"
	fi

	ARCH=`udpkg --print-architecture`
	components="`grep ^Components: $Release | cut -d' ' -f2-`"
	ret=1
	if [ -z "$components" ]; then
		error "No components listed in $Release."
	fi
	for comp in $components; do
		for ext in '.gz' ''; do
			pkgfile="$comp/debian-installer/binary-$ARCH/Packages$ext"
			line=`grep $pkgfile\$ $Release 2>/dev/null`
			if [ $? != 0 ]; then
				continue
			fi
			Packages="/tmp/net-retriever-$$-Packages"
			rm -f "$Packages"
			fetch "dists/$codename/$pkgfile" "$Packages" || continue
			checkmatch "$Release" "$pkgfile" \
				"$(md5sum "$Packages" | cut -d' ' -f1)" \
				"$(wc -c < "$Packages" | tr -d ' ')"
			if [ "$ext" = '' ]; then
				cat "$Packages" >> "$1"
			elif [ "$ext" = .gz ]; then
				zcat "$Packages" >> "$1"
			fi
			ret=0
			break
		done
	done
	exit $ret
	;;

    error)
	T="retriever/net/error"
	db_set "$T" "Retry"
	db_input critical "$T" || true

	if ! db_go; then
		exit 2
	fi
	db_get "$T"
	if [ "$RET" = "Retry" ]; then
		exit 0
	elif [ "$RET" = "Change mirror" ]; then
		choose-mirror || true
		exit 0
	elif [ "$RET" = Cancel ]; then
		exit 2
	fi
	;;

    *)
	# unknown or missing command
	exit 1
	;;
esac
