#!/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
			case "$mp" in
			"$mntdir"/*)
				run umount -- "$mp" ||:
				;;
			esac
		done
	fi

	leave "altboot_cleanup"
}

if [ -n "$ALTBOOT_OLDROOT" ]; then
	[ -n "$BC_DEBUG" ] ||
		altboot_cleanup
	if [ -s "$altboot_envfile" ]; then
		cat "$altboot_envfile" >> /.initrd/kernenv
		mkdir -p /run/stage1
		cat "$altboot_envfile" >> /run/stage1/vars
	fi
	if [ -d /etc/network ]; then
		mkdir -p /run/stage1
		cp -r /etc/network /run/stage1
	fi
	message "Spawning init..."
fi
