#!/bin/sh
set -e

TESTDIR=$(readlink -f $(dirname $0))
. $TESTDIR/framework
setupenvironment
configarchitecture "i386"

buildaptarchive
touch aptarchive/Packages
setupflataptarchive

testaptgetupdate() {
	rm -rf rootdir/var/lib/apt
	aptget update 2>> testaptgetupdate.diff >> testaptgetupdate.diff || true
	sed -i -e '/Ign / d' -e '/Release/ d' -e 's#Get:[0-9]\+ #Get: #' -e 's#\[[0-9]* [kMGTPY]*B\]#\[\]#' testaptgetupdate.diff
	GIVEN="$1"
	shift
	msgtest "Test for correctness of" "apt-get update with $*"
	if [ -z "$GIVEN" ]; then
		echo -n "" | checkdiff - testaptgetupdate.diff && msgpass || msgfail
	else
		echo "$GIVEN" | checkdiff - testaptgetupdate.diff && msgpass || msgfail
	fi
	rm testaptgetupdate.diff
}

createemptyarchive() {
	find aptarchive/ \( -name "Packages*" -o -name "en*" \) -type f -delete
	if [ "en" = "$1" ]; then
		echo -n "" | $COMPRESSOR_CMD > aptarchive/Packages.$COMPRESS
	fi
	touch aptarchive/Packages
	echo -n "" | $COMPRESSOR_CMD > aptarchive/${1}.$COMPRESS
	generatereleasefiles
	signreleasefiles
	rm -f aptarchive/Packages
}

createemptyfile() {
	find aptarchive/ \( -name "Packages*" -o -name "en*" \) -type f -delete
	if [ "en" = "$1" ]; then
		echo -n "" | $COMPRESSOR_CMD > aptarchive/Packages.$COMPRESS
	fi
	touch aptarchive/Packages aptarchive/${1}.$COMPRESS
	generatereleasefiles
	signreleasefiles
	rm -f aptarchive/Packages
}

setupcompressor() {
	COMPRESSOR="$1"
	COMPRESSOR_CMD="$1"
	case $COMPRESSOR in
	gzip) COMPRESS="gz";;
	bzip2) COMPRESS="bz2";;
	lzma) COMPRESS="lzma";;
	xz) COMPRESS="xz";;
	esac
	echo "Acquire::CompressionTypes::Order { \"${COMPRESS}\"; };
Dir::Bin::uncompressed \"/does/not/exist\";
Dir::Bin::gzip \"/does/not/exist\";
Dir::Bin::bzip2 \"/does/not/exist\";
Dir::Bin::lzma \"/does/not/exist\";
Dir::Bin::xz \"/does/not/exist\";" > rootdir/etc/apt/apt.conf.d/00compressor
	if [ -e "/bin/${COMPRESSOR}" ]; then
		echo "Dir::Bin::${COMPRESSOR} \"/bin/${COMPRESSOR}\";" >> rootdir/etc/apt/apt.conf.d/00compressor
	elif [ -e "/usr/bin/${COMPRESSOR}" ]; then
		echo "Dir::Bin::${COMPRESSOR} \"/usr/bin/${COMPRESSOR}\";" >> rootdir/etc/apt/apt.conf.d/00compressor
	elif [ "${COMPRESSOR}" = 'lzma' ]; then
		echo "Dir::Bin::xz \"/usr/bin/xz\";" >> rootdir/etc/apt/apt.conf.d/00compressor
		COMPRESSOR_CMD='xz --format=lzma'
	else
		msgtest "Test for availability of compressor" "${COMPRESSOR}"
		msgfail
		#exit 1
	fi
}

testoverfile() {
	setupcompressor "$1"

	createemptyfile 'en'
	testaptgetupdate 'Reading package lists...' "empty file en.$COMPRESS over file"

	createemptyarchive 'en'
	testaptgetupdate 'Reading package lists...' "empty archive en.$COMPRESS over file"

	createemptyarchive 'Packages'
	# FIXME: Why omits the file transport the Packages Get line?
	#Get:3 file:  Packages []
	testaptgetupdate 'Reading package lists...' "empty archive Packages.$COMPRESS over file"

	createemptyfile 'Packages'
	testaptgetupdate "Err file:  Packages
  Empty files can't be valid archives
W: Failed to fetch ${COMPRESSOR}:$(readlink -f aptarchive/Packages.$COMPRESS)  Empty files can't be valid archives

E: Some index files failed to download. They have been ignored, or old ones used instead." "empty file Packages.$COMPRESS over file"
}

testoverhttp() {
	setupcompressor "$1"

	createemptyfile 'en'
	testaptgetupdate "Get: http://localhost:8080  Packages []
Get: http://localhost:8080  Translation-en
Reading package lists..." "empty file en.$COMPRESS over http"

	createemptyarchive 'en'
	testaptgetupdate "Get: http://localhost:8080  Packages []
Get: http://localhost:8080  Translation-en []
Reading package lists..." "empty archive en.$COMPRESS over http"

	createemptyarchive 'Packages'
	testaptgetupdate "Get: http://localhost:8080  Packages []
Reading package lists..." "empty archive Packages.$COMPRESS over http"

	createemptyfile 'Packages'
	#FIXME: we should response with a good error message instead
	testaptgetupdate "Get: http://localhost:8080  Packages
Err http://localhost:8080  Packages
  Empty files can't be valid archives
W: Failed to fetch ${COMPRESSOR}:$(readlink -f rootdir/var/lib/apt/lists/partial/localhost:8080_Packages)  Empty files can't be valid archives

E: Some index files failed to download. They have been ignored, or old ones used instead." "empty file Packages.$COMPRESS over http"
}

for COMPRESSOR in 'gzip' 'bzip2' 'lzma' 'xz'; do testoverfile $COMPRESSOR; done

# do the same again with http instead of file
changetowebserver

for COMPRESSOR in 'gzip' 'bzip2' 'lzma' 'xz'; do testoverhttp $COMPRESSOR; done