#!/bin/bash
set -eu

TESTDIR=$(readlink -f $(dirname $0))

case "$APT_TEST_METHOD" in
	cdrom*)
		echo 'SKIP (not ready for apt-cdrom, because it fetches the lists earlier)' >&2
		exit 0
		;;
	http*) :
	       ;;
	*)
	    # in our setup...
		echo 'SKIP (other methods do not seem to fetch the compressed list...)' >&2
		exit 0
	       ;;
esac

. $TESTDIR/framework

setupenvironment

buildpackage 'simple-package'
buildpackage 'simple-package-noarch'
buildpackage 'conflicting-package-one'
buildpackage 'conflicting-package-two'

generaterepository_and_switch_sources "$TMPWORKINGDIRECTORY/usr/src/RPM/RPMS"

# Inject (if smaller, apt-get update should resume and not refetch completely):
readonly REPO_NOARCH_PKGLIST="$REPO_STORAGE/$NOARCH_DISTRO/base/pkglist.$DISTRO_COMPONENT$REPO_COMPR_EXT"
{
    head -c-4 "$REPO_NOARCH_PKGLIST"
} >"$APT_FETCHED_PARTIAL_NOARCH_PKGLIST"
# assert the size (in partial/) is smaller:
[ "$(stat -c '%s' "$REPO_NOARCH_PKGLIST")" \
      -gt \
      "$(stat -c '%s' "$APT_FETCHED_PARTIAL_NOARCH_PKGLIST")" \
    ]
# Final touch with the right time, so that no one notices.
touch -r "$REPO_NOARCH_PKGLIST" -- "$APT_FETCHED_PARTIAL_NOARCH_PKGLIST"
# To be sure that apt will not have refetched the beginning, we spoil it:
dd conv=notrunc,nocreat of="$REPO_NOARCH_PKGLIST" <<<B
touch -r "$APT_FETCHED_PARTIAL_NOARCH_PKGLIST" -- "$REPO_NOARCH_PKGLIST"


testsuccess aptget update
testsuccess aptcache show simple-package
testsuccess aptcache show simple-package-noarch
testfailure aptcache show nosuchpkg
