#!/bin/bash -efu

altboot_cleanup()
{
	enter "altboot_cleanup"

	local mp

	if grep -qs " $mntdir/" /proc/mounts; then
		grep -s " $mntdir/" /proc/mounts |
			cut -f2 -d' ' |
			sort -r |
		while read -r mp; do
			run umount -- "$mp" ||:
		done
	fi

	leave "altboot_cleanup"
}

if [ -n "$ALTBOOT_OLDROOT" ]; then
	[ -f "$mntdir"/DIRTY ] ||
		altboot_cleanup
	[ ! -s "$altboot_envfile" ] ||
		cat "$altboot_envfile" >> /.initrd/kernenv
	message "Spawning init..."
fi
