#!/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"
}

altboot_finalize()
{
	local text

	enter "altboot_finalize"

	# Check for INIT inside the stage2 rootfs
	if [ -z "$NOASKUSER" ] && [ ! -x "${prevdir}${INIT:-/sbin/init}" ]; then
		text="The specified INIT not found inside the stage2 rootfs."
		text="$text Retry with another init=... after the reboot."
		IM_start_output errmsg
		IM_errmsg "$text $IM_RBMSG"
		bc_reboot
	fi

	[ -n "$BC_DEBUG" ] ||
		altboot_cleanup

	if [ -s "$altboot_envfile" ]; then
		cat "$altboot_envfile" >> /.initrd/kernenv
		run mkdir -p /run/stage1
		run cp -Lf -- "$altboot_envfile" /run/stage1/vars
		debug "altboot environment saved"
	fi

	if [ -d /etc/network ]; then
		run mkdir -p /run/stage1
		run cp -aRf /etc/network /run/stage1/
		debug "/etc/network copyed"
	fi

	message "Spawning init..."

	leave "altboot_finalize"
}

# For propagator-compatibility mode only
[ -z "$ALTBOOT_OLDROOT" ] || altboot_finalize
