#!/bin/sh
set -eu
export LC_ALL=C.UTF-8
export SOURCE_DATE_EPOCH={{ SOURCE_DATE_EPOCH }}
trap "rm -f /tmp/chrootless.tar" EXIT INT TERM

if [ ! -e /mmdebstrap-testenv ]; then
  echo "this test modifies the system and should only be run inside a container" >&2
  exit 1
fi

cat <<HOSTS >>/etc/hosts
127.0.0.1 ftp.ports.debian.org
HOSTS

[ "$(id -u)" -eq 0 ]
[ "{{ VARIANT }}" = "minbase" ]

# Make sure, that 'unstable' is requested.
# Nevertheless we pass an empty value for the suite to mmdebstrap because we
# need to pick Essential:yes packages like netdde from "unreleased"
[ "{{ DIST }}" = "unstable" ]

# We are already in a disposable VM here, so we don't need to run chrootless
# in sandwich mode for safety reasons. We need this because systemd conflicts
# with insserv and the VM runs systemd.
{{ CMD }} \
  --variant={{ VARIANT }} \
  --include=insserv,perl,debian-ports-archive-keyring \
  --customize-hook='copy-in /etc/hosts /etc' \
  --customize-hook='copy-in {{ CMD }} /' \
  --customize-hook='
    chroot "$1" {{ CMD }} --mode=chrootless --architecture="hurd-i386" \
    --skip=check/chrootless \
    --variant={{ VARIANT }} "" "/tmp/chrootless.tar" \
    "deb http://ftp.ports.debian.org/debian-ports unreleased main" \
    "deb http://ftp.ports.debian.org/debian-ports unstable main"
    ' \
  --customize-hook='copy-out /tmp/chrootless.tar /tmp' \
  {{ DIST }} /dev/null {{ MIRROR }}

# when creating a foreign architecture chroot, the tarballs are not
# bit-by-bit identical but contain a few remaining differences:
#
#  * /etc/ld.so.cache -- hard problem, must be solved in glibc upstream
#  * /var/lib/dpkg/triggers -- #990712
#  * /var/cache/debconf/*.dat-old -- needs investigation
./tarfilter <"/tmp/chrootless.tar" \
  --symlink-mode=777 \
  --path-exclude=/var/cache/debconf/config.dat-old \
  --path-exclude=/var/cache/debconf/templates.dat-old \
  --path-exclude=/etc/ld.so.cache \
  --path-exclude=/var/lib/dpkg/triggers/File \
  --path-exclude=/var/lib/dpkg/triggers/ldconfig \
  --path-exclude=/var/lib/dpkg/diversions-old \
  --path-exclude=/etc/resolv.conf \
  >"cache/hurd.tar"

# This test isn't comparing anything yet. For that we need to run hurd.sh to
# create a tarball in Hurd.
