#!/bin/sh

D=opensysusers_0.7.3-4.1_all.deb

set -eu

if test -n "${1:-}"; then
	bad=$(dpkg --root="$1" --verify)
	if test -n "$bad"; then
		echo "$bad"
		exit 1
	fi
	exit 0
fi

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

PREINTS_OPT=--include=opensysusers
SYSTEMD_OPT=--include=systemd
INST_OPT="--chrooted-customize-hook=apt-get -y install /o.deb"

# install on unstable
bootstrap_test unstable "$INST_OPT"

# install on unstable with systemd, removes systemd
bootstrap_test unstable "$SYSTEMD_OPT" "$INST_OPT"

# install on bookworm
bootstrap_test bookworm "$INST_OPT"

# install on bookworm with systemd
bootstrap_test bookworm "$SYSTEMD_OPT" "$INST_OPT"

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

# unstable's systemd conflicts with opensysusers

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

# upgrade bookworm to fixed with systemd, removes systemd
bootstrap_test bookworm "$SYSTEMD_OPT" "$PREINTS_OPT" "$INST_OPT"
