#!/bin/ash
value() { grep -e  -o "(^| )$1=[^ ]+" /proc/cmdline | cut -d= -f2; } #sfs
param() { grep -e -qo " $1( |\$)" /proc/cmdline; }

msg3(){
#    if [ "${debug}" = "y" ] || [ "${step}" = "y" ] ;then
    if [ "`grep -E '\bstep2\b'  /proc/cmdline`" ]  ;then
	[ "${quiet}" = "y" ] && echo $@
#        echo -e "[1;33m"":: Debugging shell started.\n: Type your desired commands, hit Ctrl+D to continue booting\n: or press Ctrl+Alt+Del to reboot.""[0m" 
        echo -e "[1;33m"":: Debugging shell started. Ctrl+D: continue booting. Ctrl+Alt+Del: reboot""[0m" 
#        launch_interactive_shell
        sh 2>/dev/null #; echo -e "\n\n"
    fi
}
# /oldroot depends on things inside /oldroot/run/archiso...
mkdir /oldrun
##mount -n --move /oldroot/run /oldrun
mount -n --move /oldroot/mnt /oldrun
#    umount -R /oldroot
umount $(mount | awk '$3 ~/^\/oldroot/ {print $3}' | sort -r)
    msg3 "::: Unmount all mounts now."

DN(){
# Remove all dm-snapshot devices.
dmsetup remove_all

# Remove all loopback devices.
for _lup in $(grep ^/dev/loop /oldrun/archiso/used_block_devices | tac); do
    if ! losetup -d ${_lup} 2> /dev/null; then
        umount -d ${_lup}
    fi
done
}
# Unmount the space used to store *.cow.
##umount /oldrun/archiso/cowspace
umount /oldrun/live/memory/changes
    msg3 "::: Unmount changes."

#sh
# Unmount boot device if needed (no copytoram=y used)
##if [[ ! -d /oldrun/archiso/copytoram ]]; then
if [[ ! -d /oldrun/live/memory/copytoram ]]; then
##    if [[ -d /oldrun/archiso/img_dev ]]; then
    if [[ -d /oldrun/img_dev ]]; then
        umount /oldrun/img_dev
    else
#        umount /oldrun/home
        umount /oldrun/mnt/home
    fi
fi
    umount /oldrun
    msg3 "::: Unmount boot device. End shutdown"

# reboot / poweroff / halt, depending on the argument passed by init
# if something invalid is passed, we halt
case "$1" in
  reboot|poweroff|halt) "$1" -f ;;
  *) halt -f;;
esac
