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

prefix=
if [ "$(id -u)" -eq 0 ] && [ "{{ MODE }}" != "root" ] && [ "{{ MODE }}" != "auto" ]; then
  if ! id "${SUDO_USER:-user}" >/dev/null 2>&1; then
    if [ ! -e /mmdebstrap-testenv ]; then
      echo "this test modifies the system and should only be run inside a container" >&2
      exit 1
    fi
    useradd --home-dir "/home/${SUDO_USER:-user}" --create-home "${SUDO_USER:-user}"
  fi
  prefix="runuser -u ${SUDO_USER:-user} --"
fi

[ "{{ MODE }}" = "fakechroot" ] && prefix="$prefix fakechroot fakeroot"
symlinktarget=/real
[ "{{ MODE }}" = "fakechroot" ] && symlinktarget='$1/real'
echo copy-in-setup >/tmp/copy-in-setup
echo copy-in-essential >/tmp/copy-in-essential
echo copy-in-customize >/tmp/copy-in-customize
echo tar-in-setup >/tmp/tar-in-setup
echo tar-in-essential >/tmp/tar-in-essential
echo tar-in-customize >/tmp/tar-in-customize
tar --numeric-owner --format=pax --pax-option=exthdr.name=%d/PaxHeaders/%f,delete=atime,delete=ctime -C /tmp -cf /tmp/tar-in-setup.tar tar-in-setup
tar --numeric-owner --format=pax --pax-option=exthdr.name=%d/PaxHeaders/%f,delete=atime,delete=ctime -C /tmp -cf /tmp/tar-in-essential.tar tar-in-essential
tar --numeric-owner --format=pax --pax-option=exthdr.name=%d/PaxHeaders/%f,delete=atime,delete=ctime -C /tmp -cf /tmp/tar-in-customize.tar tar-in-customize
rm /tmp/tar-in-setup
rm /tmp/tar-in-essential
rm /tmp/tar-in-customize
echo upload-setup >/tmp/upload-setup
echo upload-essential >/tmp/upload-essential
echo upload-customize >/tmp/upload-customize
mkdir /tmp/sync-in-setup
mkdir /tmp/sync-in-essential
mkdir /tmp/sync-in-customize
echo sync-in-setup >/tmp/sync-in-setup/file
echo sync-in-essential >/tmp/sync-in-essential/file
echo sync-in-customize >/tmp/sync-in-customize/file
$prefix {{ CMD }} --mode={{ MODE }} --variant=apt \
  --setup-hook='mkdir "$1/real"' \
  --setup-hook='copy-in /tmp/copy-in-setup /real' \
  --setup-hook='echo copy-in-setup | cmp "$1/real/copy-in-setup" -' \
  --setup-hook='rm "$1/real/copy-in-setup"' \
  --setup-hook='echo copy-out-setup > "$1/real/copy-out-setup"' \
  --setup-hook='copy-out /real/copy-out-setup /tmp' \
  --setup-hook='rm "$1/real/copy-out-setup"' \
  --setup-hook='tar-in /tmp/tar-in-setup.tar /real' \
  --setup-hook='echo tar-in-setup | cmp "$1/real/tar-in-setup" -' \
  --setup-hook='tar-out /real/tar-in-setup /tmp/tar-out-setup.tar' \
  --setup-hook='rm "$1"/real/tar-in-setup' \
  --setup-hook='upload /tmp/upload-setup /real/upload' \
  --setup-hook='echo upload-setup | cmp "$1/real/upload" -' \
  --setup-hook='download /real/upload /tmp/download-setup' \
  --setup-hook='rm "$1/real/upload"' \
  --setup-hook='sync-in /tmp/sync-in-setup /real' \
  --setup-hook='echo sync-in-setup | cmp "$1/real/file" -' \
  --setup-hook='sync-out /real /tmp/sync-out-setup' \
  --setup-hook='rm "$1/real/file"' \
  --essential-hook='ln -s "'"$symlinktarget"'" "$1/symlink"' \
  --essential-hook='copy-in /tmp/copy-in-essential /symlink' \
  --essential-hook='echo copy-in-essential | cmp "$1/real/copy-in-essential" -' \
  --essential-hook='rm "$1/real/copy-in-essential"' \
  --essential-hook='echo copy-out-essential > "$1/real/copy-out-essential"' \
  --essential-hook='copy-out /symlink/copy-out-essential /tmp' \
  --essential-hook='rm "$1/real/copy-out-essential"' \
  --essential-hook='tar-in /tmp/tar-in-essential.tar /symlink' \
  --essential-hook='echo tar-in-essential | cmp "$1/real/tar-in-essential" -' \
  --essential-hook='tar-out /symlink/tar-in-essential /tmp/tar-out-essential.tar' \
  --essential-hook='rm "$1"/real/tar-in-essential' \
  --essential-hook='upload /tmp/upload-essential /symlink/upload' \
  --essential-hook='echo upload-essential | cmp "$1/real/upload" -' \
  --essential-hook='download /symlink/upload /tmp/download-essential' \
  --essential-hook='rm "$1/real/upload"' \
  --essential-hook='sync-in /tmp/sync-in-essential /symlink' \
  --essential-hook='echo sync-in-essential | cmp "$1/real/file" -' \
  --essential-hook='sync-out /real /tmp/sync-out-essential' \
  --essential-hook='rm "$1/real/file"' \
  --customize-hook='copy-in /tmp/copy-in-customize /symlink' \
  --customize-hook='echo copy-in-customize | cmp "$1/real/copy-in-customize" -' \
  --customize-hook='rm "$1/real/copy-in-customize"' \
  --customize-hook='echo copy-out-customize > "$1/real/copy-out-customize"' \
  --customize-hook='copy-out /symlink/copy-out-customize /tmp' \
  --customize-hook='rm "$1/real/copy-out-customize"' \
  --customize-hook='tar-in /tmp/tar-in-customize.tar /symlink' \
  --customize-hook='echo tar-in-customize | cmp "$1/real/tar-in-customize" -' \
  --customize-hook='tar-out /symlink/tar-in-customize /tmp/tar-out-customize.tar' \
  --customize-hook='rm "$1"/real/tar-in-customize' \
  --customize-hook='upload /tmp/upload-customize /symlink/upload' \
  --customize-hook='echo upload-customize | cmp "$1/real/upload" -' \
  --customize-hook='download /symlink/upload /tmp/download-customize' \
  --customize-hook='rm "$1/real/upload"' \
  --customize-hook='sync-in /tmp/sync-in-customize /symlink' \
  --customize-hook='echo sync-in-customize | cmp "$1/real/file" -' \
  --customize-hook='sync-out /real /tmp/sync-out-customize' \
  --customize-hook='rm "$1/real/file"' \
  --customize-hook='rmdir "$1/real"' \
  --customize-hook='rm "$1/symlink"' \
  {{ DIST }} /tmp/debian-chroot.tar {{ MIRROR }}
for n in setup essential customize; do
  ret=0
  cmp /tmp/tar-in-$n.tar /tmp/tar-out-$n.tar || ret=$?
  if [ "$ret" -ne 0 ]; then
    if type diffoscope >/dev/null; then
      diffoscope /tmp/tar-in-$n.tar /tmp/tar-out-$n.tar
      exit 1
    else
      echo "no diffoscope installed" >&2
    fi
    if type base64 >/dev/null; then
      base64 /tmp/tar-in-$n.tar
      base64 /tmp/tar-out-$n.tar
      exit 1
    else
      echo "no base64 installed" >&2
    fi
    if type xxd >/dev/null; then
      xxd /tmp/tar-in-$n.tar
      xxd /tmp/tar-out-$n.tar
      exit 1
    else
      echo "no xxd installed" >&2
    fi
    exit 1
  fi
done
echo copy-out-setup | cmp /tmp/copy-out-setup -
echo copy-out-essential | cmp /tmp/copy-out-essential -
echo copy-out-customize | cmp /tmp/copy-out-customize -
echo upload-setup | cmp /tmp/download-setup -
echo upload-essential | cmp /tmp/download-essential -
echo upload-customize | cmp /tmp/download-customize -
echo sync-in-setup | cmp /tmp/sync-out-setup/file -
echo sync-in-essential | cmp /tmp/sync-out-essential/file -
echo sync-in-customize | cmp /tmp/sync-out-customize/file -
tar -tf /tmp/debian-chroot.tar | sort | diff -u tar1.txt -
rm /tmp/debian-chroot.tar \
  /tmp/copy-in-setup /tmp/copy-in-essential /tmp/copy-in-customize \
  /tmp/copy-out-setup /tmp/copy-out-essential /tmp/copy-out-customize \
  /tmp/tar-in-setup.tar /tmp/tar-in-essential.tar /tmp/tar-in-customize.tar \
  /tmp/tar-out-setup.tar /tmp/tar-out-essential.tar /tmp/tar-out-customize.tar \
  /tmp/upload-setup /tmp/upload-essential /tmp/upload-customize \
  /tmp/download-setup /tmp/download-essential /tmp/download-customize \
  /tmp/sync-in-setup/file /tmp/sync-in-essential/file /tmp/sync-in-customize/file \
  /tmp/sync-out-setup/file /tmp/sync-out-essential/file /tmp/sync-out-customize/file
rmdir /tmp/sync-in-setup /tmp/sync-in-essential /tmp/sync-in-customize \
  /tmp/sync-out-setup /tmp/sync-out-essential /tmp/sync-out-customize