#!/bin/sh
set -e

TESTDIR=$(readlink -f $(dirname $0))
. $TESTDIR/framework

setupenvironment
configcompression '.' 'gz' # only gz is supported for this, so ensure it is used
configarchitecture "i386"

buildsimplenativepackage "testpkg" "i386" "1.0"
setupaptarchive

GOODSHOW="$(aptcache show testpkg)
"
GOODPOLICY="$(aptcache policy testpkg)"
GOODSHOWSRC="$(aptcache showsrc testpkg)
"

test $(echo "$GOODSHOW" | grep -e '^Package: testpkg' -e '^Version: 1.0' -e '^Architecture: i386' | wc -l) -eq 3 || msgdie 'show is broken'
testequal "$GOODSHOW" aptcache show testpkg
test $(echo "$GOODPOLICY" | grep -e '^testpkg:' -e '^  Candidate:' -e '^  Installed: (none)' -e '500 file:/' | wc -l) -eq 4 || msgdie 'policy is broken'
testequal "$GOODPOLICY" aptcache policy testpkg
test $(echo "$GOODSHOWSRC" | grep -e '^Package: testpkg' -e '^Format: 3.0 (native)' -e '^Files:' -e '^Checksums-Sha256:' | wc -l) -eq 4 || msgdie 'showsrc is broken'
testequal "$GOODSHOWSRC" aptcache showsrc testpkg


testrun() {
	local F
	if [ -e rootdir/var/lib/apt/lists/*localhost*Release ]; then
		msgtest "Check if all index files are" "${1:-uncompressed}"
		if [ "$1" = "compressed" ]; then
			! test -e rootdir/var/lib/apt/lists/*_Packages || F=1
			! test -e rootdir/var/lib/apt/lists/*_Sources || F=1
			test -e rootdir/var/lib/apt/lists/*_Packages.gz || F=1
			test -e rootdir/var/lib/apt/lists/*_Sources.gz || F=1
		else
			test -e rootdir/var/lib/apt/lists/*_Packages || F=1
			test -e rootdir/var/lib/apt/lists/*_Sources || F=1
			! test -e rootdir/var/lib/apt/lists/*_Packages.gz || F=1
			! test -e rootdir/var/lib/apt/lists/*_Sources.gz || F=1
		fi
		if [ -n "$F" ]; then
			ls -laR rootdir/var/lib/apt/lists/
			msgfail
		else
			msgpass
		fi
		msgtest "Check if package is downloadable"
		testsuccess --nomsg aptget install -d testpkg
		msgtest "\tdeb file is present"; testsuccess --nomsg test -f rootdir/var/cache/apt/archives/testpkg_1.0_i386.deb
		aptget clean
		msgtest "\tdeb file is gone"; testfailure --nomsg test -f rootdir/var/cache/apt/archives/testpkg_1.0_i386.deb
	fi
	rm -f rootdir/var/cache/apt/pkgcache.bin rootdir/var/cache/apt/srcpkgcache.bin
	testequal "$GOODSHOW" aptcache show testpkg
	testequal "$GOODSHOW" aptcache show testpkg
	rm -f rootdir/var/cache/apt/pkgcache.bin rootdir/var/cache/apt/srcpkgcache.bin
	testequal "$GOODPOLICY" aptcache policy testpkg
	testequal "$GOODPOLICY" aptcache policy testpkg
	rm -f rootdir/var/cache/apt/pkgcache.bin rootdir/var/cache/apt/srcpkgcache.bin
	testequal "$GOODSHOWSRC" aptcache showsrc testpkg
	testequal "$GOODSHOWSRC" aptcache showsrc testpkg
	aptget clean
	msgtest "Check if the source is aptgetable"
	testsuccess --nomsg aptget source testpkg
	msgtest "\tdsc file is present"; testsuccess --nomsg test -f testpkg_1.0.dsc
	msgtest "\tdirectory is present"; testsuccess --nomsg test -d testpkg-1.0
	rm -rf testpkg-1.0
}

echo 'Acquire::GzipIndexes "false";' > rootdir/etc/apt/apt.conf.d/02compressindex
msgmsg "File: Test with uncompressed indexes"
testrun

testsuccess aptget update -o Acquire::Pdiffs=1
msgmsg "File: Test with uncompressed indexes (update unchanged with pdiffs)"
testrun

testsuccess aptget update -o Acquire::Pdiffs=0
msgmsg "File: Test with uncompressed indexes (update unchanged without pdiffs)"
testrun

rm -rf rootdir/var/lib/apt/lists
echo 'Acquire::CompressionTypes::Order:: "gz";
Acquire::GzipIndexes "true";' > rootdir/etc/apt/apt.conf.d/02compressindex

testsuccess aptget update
msgmsg "File: Test with compressed indexes"
testrun "compressed"

testsuccess aptget update -o Acquire::Pdiffs=1
msgmsg "File: Test with compressed indexes (update unchanged with pdiffs)"
testrun "compressed"

testsuccess aptget update -o Acquire::Pdiffs=0
msgmsg "File: Test with compressed indexes (update unchanged without pdiffs)"
testrun "compressed"

rm rootdir/etc/apt/apt.conf.d/02compressindex
changetowebserver
testsuccess aptget update
GOODPOLICY="$(aptcache policy testpkg)"
test $(echo "$GOODPOLICY" | grep -e '^testpkg:' -e '^  Candidate:' -e '^  Installed: (none)' -e '500 http://' | wc -l) -eq 4
testequal "$GOODPOLICY" aptcache policy testpkg

msgmsg "HTTP: Test with uncompressed indexes"
testrun

testsuccess aptget update -o Acquire::Pdiffs=1
msgmsg "HTTP: Test with uncompressed indexes (update unchanged with pdiffs)"
testrun

testsuccess aptget update -o Acquire::Pdiffs=0
msgmsg "HTTP: Test with uncompressed indexes (update unchanged without pdiffs)"
testrun

rm -rf rootdir/var/lib/apt/lists
echo 'Acquire::CompressionTypes::Order:: "gz";
Acquire::GzipIndexes "true";' > rootdir/etc/apt/apt.conf.d/02compressindex

testsuccess aptget update
msgmsg "HTTP: Test with compressed indexes"
testrun "compressed"

testsuccess aptget update -o Acquire::Pdiffs=1
msgmsg "HTTP: Test with compressed indexes (update unchanged with pdiffs)"
testrun "compressed"

testsuccess aptget update -o Acquire::Pdiffs=0
msgmsg "HTTP: Test with compressed indexes (update unchanged without pdiffs)"
testrun "compressed"