#!/bin/sh
# Pre-populate signed Release files for an archive.
set -e

## TODO handle mirrors, i.e. archive.ubuntu.com should be install for
## anything that is the selected ubuntu mirror,
## i.e. gb.archive.ubuntu.com or mirror.mycompany.example.com

archive_id="$1"
sources_list="$2"

chroot=
if [ "$ROOT" ]; then
	chroot=chroot
fi

if [ "$ROOT" ] && [ "${sources_list#$ROOT/}" = "$sources_list" ]; then
	# Generate a new temporary file that can be accessed by APT in the
	# chroot.
	file="$($chroot $ROOT mktemp)"
	cat "$sources_list" > "$ROOT$file"
else
	file="${sources_list#$ROOT}"
fi

# Slightly awkward output format, e.g.:
#   'http://archive.ubuntu.com/ubuntu/dists/hardy/Release' archive.ubuntu.com_ubuntu_dists_hardy_Release 0
lines="$($chroot $ROOT \
	 apt-get -o APT::Get::List-Cleanup=false \
		 -o Dir::Etc::sourcelist="$file" update --print-uris | \
	 grep "^'.*'" | grep -v "^'http://archive.ubuntulinux.jp")"

NEWLINE='
'
OLD_IFS="$IFS"
IFS="$NEWLINE"
for line in $lines; do
	IFS="$OLD_IFS"
	uri="$(echo "$line" | sed "s/^'//; s/'.*//")"
	file="$(echo "$line" | sed "s/^'[^']*' //; s/ .*//")"
	base="${uri##*/}"
	dir="${uri%/*}"
	codename="${dir##*/}"
    echo $base $file
	case $base in
	    InRelease|Release|Release.gpg)
        set -x
		release="/usr/share/apt-setup/release-files/$archive_id/$codename/$base"
		if [ -f "$release" ]; then
			cp -a "$release" "$ROOT/var/lib/apt/lists/$file"
		fi
        set +x
		;;
	esac
	IFS="$NEWLINE"
done
IFS="$OLD_IFS"

exit 0
