#!/bin/sh
set -eu
export LC_ALL=C.UTF-8
cat <<SCRIPT >/tmp/checkeatmydata.sh
#!/bin/sh
set -exu
cat << EOF | diff - "\$1"/usr/bin/dpkg
#!/bin/sh
exec /usr/bin/eatmydata /usr/bin/dpkg.distrib "\\\$@"
EOF
[ -e "\$1"/usr/bin/eatmydata ]
SCRIPT
chmod +x /tmp/checkeatmydata.sh
# first four bytes: magic
elfheader='\177ELF'
# fifth byte: bits
case "$(dpkg-architecture -qDEB_HOST_ARCH_BITS)" in
  32) elfheader="$elfheader\\001" ;;
  64) elfheader="$elfheader\\002" ;;
  *)
    echo "bits not supported"
    exit 1
    ;;
esac
# sixth byte: endian
case "$(dpkg-architecture -qDEB_HOST_ARCH_ENDIAN)" in
  little) elfheader="$elfheader\\001" ;;
  big) elfheader="$elfheader\\002" ;;
  *)
    echo "endian not supported"
    exit 1
    ;;
esac
# seventh and eigth byte: elf version (1) and abi (unset)
elfheader="$elfheader\\001\\000"
{{ CMD }} --mode=root --variant=apt \
  --customize-hook=/tmp/checkeatmydata.sh \
  --essential-hook=/tmp/checkeatmydata.sh \
  --extract-hook='printf "'"$elfheader"'" | cmp --bytes=8 - "$1"/usr/bin/dpkg' \
  --hook-dir=./hooks/eatmydata \
  --customize-hook='printf "'"$elfheader"'" | cmp --bytes=8 - "$1"/usr/bin/dpkg' \
  {{ DIST }} /tmp/debian-chroot {{ MIRROR }}
tar -C /tmp/debian-chroot --one-file-system -c . \
  | tar -t \
  | sort \
  | diff -u tar1.txt -
rm /tmp/checkeatmydata.sh
rm -r /tmp/debian-chroot