#!/usr/bin/env bash
set -Eeuo pipefail

thisDir="$(dirname "$(readlink -vf "$BASH_SOURCE")")"
source "$thisDir/.constants.sh" \
	'<target-dir>' \
	'rootfs'

eval "$dgetopt"
while true; do
	flag="$1"; shift
	dgetopt-case "$flag"
	case "$flag" in
		--) break ;;
		*) eusage "unknown flag '$flag'" ;;
	esac
done

targetDir="${1:-}"; shift || eusage 'missing target-dir'
[ -n "$targetDir" ]
epoch="$(< "$targetDir/debuerreotype-epoch")"
[ -n "$epoch" ]

if [ -s "$targetDir/etc/machine-id" ]; then
	# https://www.freedesktop.org/software/systemd/man/machine-id.html
	# > For operating system images which are created once and used on multiple machines, for example for containers or in the cloud, /etc/machine-id should be either missing or an empty file in the generic file system image ...
	echo -n > "$targetDir/etc/machine-id"
	chmod 0644 "$targetDir/etc/machine-id"
fi

# https://github.com/lamby/debootstrap/commit/66b15380814aa62ca4b5807270ac57a3c8a0558d#diff-de4eef4ab836e5c6c9c1f820a2f624baR709
rm -f \
	"$targetDir/var/log/dpkg.log" \
	"$targetDir/var/log/bootstrap.log" \
	"$targetDir/var/log/alternatives.log" \
	"$targetDir/var/cache/ldconfig/aux-cache"

# https://github.com/debuerreotype/debuerreotype/pull/32
rm -f "$targetDir/run/mount/utab"
# (also remove the directory, but only if it's empty)
rmdir "$targetDir/run/mount" 2>/dev/null || :

find "$targetDir" \
	-newermt "@$epoch" \
	-exec touch --no-dereference --date="@$epoch" '{}' +
