#!/bin/sh

die() {
	echo "error: $*" 1>&2
	exit 1
}

diverted="halt poweroff reboot shutdown"

properly_diverted_old() {
	test -e "$1/lib/molly-guard/$2" -o -h "$1/lib/molly-guard/$2" -o -e "$1/usr/lib/molly-guard/$2" -o -h "$1/usr/lib/molly-guard/$2"
}

properly_diverted_new() {
	test -e "$1/usr/sbin/$2.no-molly-guard" -o -h "$1/usr/sbin/$2.no-molly-guard" -o -e "$1/sbin/$2.no-molly-guard.usr-is-merged" -o -h "$1/sbin/$2.no-molly-guard.usr-is-merged"
}

if test -n "$MMDEBSTRAP_HOOK"; then
	if dpkg-query --root "$1" -f '${db:Status-Status}\n' -W molly-guard| grep -q '^installed$'; then
		if dpkg --compare-versions "$(dpkg-query --root "$1" -f '${Version}' -W molly-guard)" gt 0.8.1; then
			diverted_check=properly_diverted_new
		else
			diverted_check=properly_diverted_old
		fi
		if dpkg-query --root "$1" -f '${db:Status-Status}\n' -W systemd-sysv sysvinit-core | grep -q '^installed$'; then
			for cmd in $diverted; do
				"$diverted_check" "$1" "$cmd" || {
					ls -la "$1/usr/sbin"
					ls -la "$1/usr/lib/molly-guard"
					PAGER=cat dpkg --root "$1" -l molly-guard systemd-sysv sysvinit-core
					die "init installed, but $cmd missing checking $diverted_check"
				}
			done
		else
			for cmd in $diverted; do
				"$diverted_check" "$1" "$cmd" && {
					ls -la "$1/usr/sbin"
					ls -la "$1/usr/lib/molly-guard"
					PAGER=cat dpkg --root "$1" -l molly-guard systemd-sysv sysvinit-core
					die "no init, but reboot present checking $diverted_check"
				}
			done
		fi
	else
		test -d "$1/usr/lib/molly-guard" && {
			ls -la "$1/usr/lib/molly-guard"
			die "molly-guard removed, but libdir remains"
		}
	fi
	exit 0
fi

fromcase="$1-"
tocase="$2-"

basedist=bookworm
nextdist=sid
mirror=http://deb.debian.org/debian
includepkgs=
preinstallpkgs=

while test "${fromcase#*-}" != "$fromcase"; do
	thing="${fromcase%%-*}"
	fromcase="${fromcase#*-}"
	case "$thing" in
		molly)
			includepkgs="${includepkgs:+"$includepkgs,"}molly-guard"
		;;
		sidmolly)
			preinstallpkgs="${preinstallpkgs:+"$preinstallpkgs "}molly-guard"
		;;
		newmolly)
			preinstallpkgs="${preinstallpkgs:+"$preinstallpkgs "}/molly-guard.deb"
		;;
		systemd)
			includepkgs="${includepkgs:+"$includepkgs,"}systemd-sysv"
		;;
		sidsystemd)
			preinstallpkgs="${preinstallpkgs:+"$preinstallpkgs "}systemd-sysv"
		;;
		newsystemd)
			preinstallpkgs="${preinstallpkgs:+"$preinstallpkgs "}/libsystemd0.deb /libsystemd-shared.deb /libudev1.deb /systemd-dev.deb /systemd.deb /systemd-sysv.deb"
		;;
		sysvinit)
			includepkgs="${includepkgs:+"$includepkgs,"}sysvinit-core"
		;;
		"")
		;;
		*)
			die "unrecognized from thing: $thing"
		;;
	esac
done

while test "${tocase#*-}" != "$tocase"; do
	thing="${tocase%%-*}"
	tocase="${tocase#*-}"
	case "$thing" in
		molly)
			installpkgs="${installpkgs:+"$installpkgs "}/molly-guard.deb"
		;;
		rmmolly)
			installpkgs="${installpkgs:+"$installpkgs "}molly-guard-"
		;;
		systemd)
			#installpkgs="${installpkgs:+"$installpkgs "}systemd-sysv"
			installpkgs="${installpkgs+:"$installpkgs "}/libsystemd0.deb /libsystemd-shared.deb /libudev1.deb /systemd-dev.deb /systemd.deb /systemd-sysv.deb"
		;;
		rmsystemd)
			installpkgs="${installpkgs:+"$installpkgs "}systemd-sysv-"
		;;
		sysvinit)
			installpkgs="${installpkgs:+"$installpkgs "}sysvinit-core"
		;;
		rmsysvinit)
			installpkgs="${installpkgs:+"$installpkgs "}sysvinit-core-"
		;;
		"")
		;;
		*)
			die "unrecognized to thing: $thing"
		;;
	esac
done

set -- --variant=apt "$basedist" /dev/null "$mirror"
test -n "$includepkgs" && set -- "$@" --include="$includepkgs"
set -- "$@" --customize-hook="$0"
set -- "$@" --customize-hook="upload molly-guard_0.8.2_all.deb /molly-guard.deb"
SDVER="255-1.1"
set -- "$@" --customize-hook="upload libsystemd-shared_${SDVER}_amd64.deb /libsystemd-shared.deb"
set -- "$@" --customize-hook="upload libsystemd0_${SDVER}_amd64.deb /libsystemd0.deb"
set -- "$@" --customize-hook="upload libudev1_${SDVER}_amd64.deb /libudev1.deb"
set -- "$@" --customize-hook="upload systemd_${SDVER}_amd64.deb /systemd.deb"
set -- "$@" --customize-hook="upload systemd-dev_${SDVER}_all.deb /systemd-dev.deb"
set -- "$@" --customize-hook="upload systemd-sysv_${SDVER}_amd64.deb /systemd-sysv.deb"
set -- "$@" --customize-hook='sed -i -e "'"s/$basedist/$nextdist/"'" "$1/etc/apt/sources.list"'
set -- "$@" --chrooted-customize-hook="apt-get update"
test -n "$preinstallpkgs" && set -- "$@" --chrooted-customize-hook="apt-get -y install $preinstallpkgs"
set -- "$@" --customize-hook="$0"
test -n "$installpkgs" || die "no packages to install"
set -- "$@" --chrooted-customize-hook="apt-get -y install $installpkgs"
set -- "$@" --customize-hook="$0"
set -- "$@" --chrooted-customize-hook="apt-get -y remove molly-guard"
set -- "$@" --customize-hook="$0"

exec auto-apt-proxy mmdebstrap "$@"
