#!/exec/ash
PATH="/exec:/sbin:/bin:/usr/sbin:/usr/bin"
[ -d /UNIONFS ] && OVERLAY="yes"
NOEJECT=""
BASEDEV="$(mount | awk '/\/BASE/ {print $1}')"
HALT="${1}"
[ -f "/molinfo" ] && . /molinfo
PATH="/exec"

if [ ! -n "${NOEJECT}" ]; then
	if [ -n "${OVERLAY}" ]; then
		echo "Unmounting Overlay System..."
		ln -snf /MOL+/etc /etc
		umount /UNIONFS
	fi

	echo "Unmounting MOL System..."
	umount /MOL
	#losetup -d /dev/loop0

	echo "Unmounting BASE System..."
	umount /BASE

	mv /etc/mtab /etc/mtab.backup
	cp /proc/mounts /etc/mtab

	echo "Ejecting Live Disk...(hit return)"
	eject ${BASEDEV}
	read a
	eject -t ${BASEDEV}
fi

echo "Remount read only anything..."
mount | awk '{ print $3 }' | while read line; do
    mount -n -o ro,remount $line 2>/dev/null
done

case "${HALT}" in
	*halt)
	exec /exec/poweroff
	;;
	*reboot)
	exec /exec/reboot
	;;
	*)
	echo "unknown halt option: ${HALT}"
	echo "starting ash"
	exec /exec/ash
	;;
esac

