#!/bin/sh

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

to=$(basename "$url")

# curl exit codes considered transient network failures, worth retrying
# (resuming any partial download). Anything else is permanent -- most
# notably 22 (HTTP error such as 404 when using --fail) and 3 (malformed
# URL) -- and fails fast so the caller can move on to the next download
# location instead of burning minutes retrying a URL that can never
# succeed.
transient=" 6 7 18 28 35 52 55 56 "

is_transient()
{
	case "$transient" in
	*\ $1\ *)	return 0 ;;
	esac
	return 1
}

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

	echo "II: $what $url"

	rm -f "$to.hdr"

	# Download with resume support. Connections are occasionally
	# dropped mid-transfer for large files; keeping the partial file
	# and resuming it on the next attempt (instead of starting over)
	# lets such transfers complete. Never leave a partially-downloaded
	# file behind on final failure -- it would fool the caller's
	# file-presence check and the corrupt archive would then fail at
	# extraction time instead of being re-fetched.
	attempt=0
	while :
	do
		attempt=$(($attempt+1))
		if [ "$attempt" -gt 30 ]; then
			echo "EE: giving up on $url after $attempt attempts" 1>&2
			rm -f "$to" "$to.hdr"
			exit 1
		fi

		rc=0
		curl --silent --fail --show-error --continue-at - \
			"$url" -o "$to" -D "$to.hdr" || rc=$?
		[ "$rc" -eq 0 ] && break

		# 36: nothing left to resume -- treat as complete.
		[ "$rc" -eq 36 ] && break

		# 33: range request not honoured -- restart from scratch.
		if [ "$rc" -eq 33 ]; then
			rm -f "$to"
			continue
		fi

		if ! is_transient "$rc"; then
			# Permanent failure (e.g. 404) -- do not retry.
			rm -f "$to" "$to.hdr"
			exit 1
		fi

		size=$(wc -c <"$to" 2>/dev/null || echo 0)
		echo "II:   transfer interrupted (curl $rc, $size bytes), resuming"
		sleep 5
	done

	redirect=$(awk '/^Location: / {gsub(/^[[:space:]]+|[[:space:]]+$/,"",$2); print $2;}' "$to.hdr")
	[ -z "$redirect" ] && break
	# The body of a redirect response is not part of the file.
	rm -f "$to"
	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
