#!/bin/sh
#
# test that the user can drop archives into /var/cache/apt/archives as well as
# into /var/cache/apt/archives/partial

set -eu
export LC_ALL=C.UTF-8
export SOURCE_DATE_EPOCH={{ SOURCE_DATE_EPOCH }}
if [ ! -e /mmdebstrap-testenv ]; then
	echo "this test requires the cache directory to be mounted on /mnt and should only be run inside a container" >&2
	exit 1
fi
include="--include=doc-debian"
if [ "{{ VARIANT }}" = "custom" ]; then
	include="$include,base-files,base-passwd,coreutils,dash,diffutils,dpkg,libc-bin,sed"
fi
mount -o size=4G -t tmpfs tmpfs /tmp # workaround for #1010957
{{ CMD }} $include --mode={{ MODE }} --variant={{ VARIANT }} \
	--setup-hook='mkdir -p "$1"/var/cache/apt/archives/partial' \
	--setup-hook='touch "$1"/var/cache/apt/archives/lock' \
	--setup-hook='chmod 0640 "$1"/var/cache/apt/archives/lock' \
	{{ DIST }} - {{ MIRROR }} > orig.tar
# somehow, when trying to create a tarball from the 9p mount, tar throws the
# following error: tar: ./doc-debian_6.4_all.deb: File shrank by 132942 bytes; padding with zeros
# to reproduce, try: tar --directory /mnt/cache/debian/pool/main/d/doc-debian/ --create --file - . | tar --directory /tmp/ --extract --file -
# this will be different:
# md5sum /mnt/cache/debian/pool/main/d/doc-debian/*.deb /tmp/*.deb
# another reason to copy the files into a new directory is, that we can use shell globs
tmpdir=$(mktemp -d)
cp /mnt/cache/debian/pool/main/b/busybox/busybox_*"_{{ HOSTARCH }}.deb" /mnt/cache/debian/pool/main/a/apt/apt_*"_{{ HOSTARCH }}.deb" "$tmpdir"
{{ CMD }} $include --mode={{ MODE }} --variant={{ VARIANT }} \
	--setup-hook='mkdir -p "$1"/var/cache/apt/archives/partial' \
	--setup-hook='sync-in "'"$tmpdir"'" /var/cache/apt/archives/partial' \
	{{ DIST }} - {{ MIRROR }} > test1.tar
cmp orig.tar test1.tar
{{ CMD }} $include --mode={{ MODE }} --variant={{ VARIANT }} \
	--customize-hook='touch "$1"/var/cache/apt/archives/partial' \
	--setup-hook='mkdir -p "$1"/var/cache/apt/archives/' \
	--setup-hook='sync-in "'"$tmpdir"'" /var/cache/apt/archives/' \
	--setup-hook='chmod 0755 "$1"/var/cache/apt/archives/' \
	--customize-hook='find "'"$tmpdir"'" -type f -exec md5sum "{}" \; | sed "s|"'"$tmpdir"'"|$1/var/cache/apt/archives|" | md5sum --check' \
	{{ DIST }} - {{ MIRROR }} > test2.tar
cmp orig.tar test2.tar
rm "$tmpdir"/*.deb orig.tar test1.tar test2.tar
rmdir "$tmpdir"