#!/bin/sh

. /lib/init/initmethod-bbinit-functions.sh
. /lib/init/rc-once.sh

start() {
	mount_root_rw || exit 1
	if run_rc_once; then
		systemctl disable --quiet rc-once.target
	fi
	systemctl daemon-reexec
	sleep 1
	exec "$0" umount
}

umount() {
	if ! mount_root_restore; then
		# remounting rw/ro during the second boot will flush anything
		# left in the filesystem journal
		systemctl enable --quiet rc-once.target
		systemctl reboot
	else
		systemctl default
	fi
}

case "$1" in

	start|"")
		start
		;;
	umount)
		umount
		;;
	*)
		echo "Usage: $0 {start|umount}" >&2
		exit 1
		;;
esac

exit 0


