#!/bin/sh

if [ "$#" -ne 1 ]; then
	echo "Usage: $0 <url>" 1>&2
	exit 1
fi
url="$1"

to=$(basename "$url")

count=0
what='fetching'
while :
do
	if [ "$count" -eq 20 ]; then
		echo "EE: excessive redirects" 1>&2
		rm -f "$to" "$to.hdr"
		exit 1
	fi
	count=$(($count+1))

	echo "II: $what $url"

	# Download this URL, retrying the *whole* transfer on transient errors.
	#
	# launchpadlibrarian.net serves these files with "Accept-Ranges: none",
	# so a transfer cannot be resumed (-C - is useless): every attempt must
	# deliver the entire file in one uninterrupted stream.  Large files (the
	# nvidia kernel source debs are ~80 MB) regularly die mid-transfer with
	# a connection reset (curl 56) or a truncated response (curl 18), yet a
	# fresh attempt can succeed outright.  So keep re-running the full
	# download, fresh each time, until one attempt makes it all the way
	# through.  Never leave a partially-downloaded file behind -- it would
	# fool the caller's file-presence check and the corrupt archive would
	# then fail at extraction time instead of being re-fetched.
	#
	# HTTP errors (curl 22, e.g. a 404 from a pool that does not carry the
	# package) are permanent, so fail fast on those instead of retrying.
	rm -f "$to" "$to.hdr"
	attempt=0
	max_attempt=30
	dl_rc=1
	while [ "$attempt" -lt "$max_attempt" ]
	do
		attempt=$(($attempt+1))
		curl --silent --fail --show-error \
			--connect-timeout 30 --speed-limit 1 --speed-time 60 \
			"$url" -o "$to" -D "$to.hdr"
		dl_rc=$?
		if [ "$dl_rc" -eq 0 ]; then
			break
		fi
		if [ "$dl_rc" -eq 22 ]; then
			# Permanent HTTP error (404 and friends): retrying will not help.
			break
		fi
		echo "II:   download attempt $attempt/$max_attempt failed (curl $dl_rc), retrying full transfer" 1>&2
		rm -f "$to" "$to.hdr"
		sleep 8
	done
	if [ "$dl_rc" -ne 0 ]; then
		echo "EE: failed to download $url (curl $dl_rc after $attempt attempt(s))" 1>&2
		rm -f "$to" "$to.hdr"
		exit 1
	fi

	redirect=$(awk '/^Location: / {gsub(/^[[:space:]]+|[[:space:]]+$/,"",$2); print $2;}' "$to.hdr")
	[ -z "$redirect" ] && break
	# A redirect hop: the body just fetched is not the target file, drop it.
	rm -f "$to" "$to.hdr"
	what='  following'

	url=$(echo "$redirect" | sed -e 's@https://launchpadlibrarian.net/@http://launchpadlibrarian.net/@')
	if [ "$redirect" != "$url" ]; then
		echo "II:   fixing $redirect"
	fi
done

exit 0
