#!/bin/sh

D=molly-guard_0.8.4+nmu1_all.deb

set -eu

if test -n "${1:-}"; then
	bad=$(dpkg --root="$1" --verify)
	if test -n "$bad"; then
		echo "$bad"
		exit 1
	fi
	ls -l "$1/sbin/"
	for f in halt poweroff reboot shutdown coldreboot pm-hibernate pm-suspend pm-suspend-hybrid; do
		if test -e "$1/sbin/$f.no-molly-guard" -a -e "$1/sbin/$f.no-molly-guard.usr-is-merged"; then
			echo "$f was wrongly renamed"
			ls -l "$1/sbin"
			exit 1
		fi
	done
	exit 0
fi

bootstrap_test() {
	echo "test case $*"
	suite="$1"
	shift
	mmdebstrap --verbose --variant=apt "$suite" /dev/null --customize-hook="upload $D /p.deb" --customize-hook="$0" "$@" --customize-hook="$0" --chrooted-customize-hook="dpkg -r molly-guard" --customize-hook="$0"
}

PREINTS_OPT=--include=molly-guard
INST_OPT="--chrooted-customize-hook=apt-get -y install /p.deb"

# hello is a dummy package that serves as "no init system"
for I in hello systemd-sysv sysvinit-core runit-init finit-sysv; do
	INIT_OPT="--include=$I"

	# install on unstable
	bootstrap_test unstable "$INIT_OPT" "$INST_OPT"

	# install on bookworm
	bootstrap_test bookworm "$INIT_OPT" "$INST_OPT"

	# upgrade unstable to fixed
	bootstrap_test unstable "$PREINTS_OPT" "$INIT_OPT" "$INST_OPT"

	# upgrade bookworm to fixed
	bootstrap_test bookworm "$PREINTS_OPT" "$INIT_OPT" "$INST_OPT"
done



