#!/bin/sh
set -eu
export LC_ALL=C.UTF-8

if [ ! -e "tar1.txt" ]; then
  echo "E: tar1.txt doesn't exist -- run create-directory test first" >&2
  exit 1
fi

trap "rm -rf /tmp/debian-chroot /tmp/log /tmp/trimmed" EXIT INT TERM

# we check the full log to also prevent debug printfs to accidentally make it into a commit
{{ CMD }} --mode=root --variant=apt --logfile=/tmp/log {{ DIST }} /tmp/debian-chroot {{ MIRROR }}

have_mknod=
if mknod /tmp/debian-chroot/test-dev-null c 1 3 2>/dev/null; then
  have_mknod="yes"
  rm /tmp/debian-chroot/test-dev-null
fi

# omit the last line which should contain the runtime
head --lines=-1 /tmp/log >/tmp/trimmed
{
  cat <<LOG
I: chroot architecture {{ HOSTARCH }} is equal to the host's architecture
I: finding correct signed-by value...
I: automatically chosen format: directory
LOG
  if [ -z "$have_mknod" ]; then
    echo "I: Not populating /dev because we cannot mknod (using temporary bind-mounts instead)"
  fi
  cat <<LOG
I: running apt-get update...
I: downloading packages with apt...
I: extracting archives...
I: installing essential packages...
I: installing remaining packages inside the chroot...
I: cleaning package lists and apt cache...
LOG
} | diff -u - /tmp/trimmed
tail --lines=1 /tmp/log | grep '^I: success in .* seconds$'
tar -C /tmp/debian-chroot --one-file-system -c . | tar -t | sort | diff -u tar1.txt -
