#!/bin/sh
#210810 sfs
run_hook() {
    [ "${aufs}" ] || return
    [ "${ofs}" ]  && return
        [ "${rootdelay}" ] && sleep ${rootdelay} && 
    	    echo -e "[1;33m"":: aufs Wait rootdelay=${rootdelay}...""[0m" 
ROOT_MNT="/new_root"
OFS_DIRS="/run/archroot"
OFS_HOME="${OFS_DIRS}/root_ro"
OFS_COWSPACE="${OFS_DIRS}/cowspace"
OFS_COWSPACE_DEV="${OFS_DIRS}/cowspace_dev"
rdir() {
    if [ ! "${rootdir}" ]; then
        err " rootdir= not defined"
        launch_interactive_shell
    fi
}
    mount_handler="aufs_mount_handler"
    export mount_handler
}

aufs_mount_handler(){
STEP(){
    [ "${step}" = "y" ] || return
        echo -e  "[1;33m"":: ${@}""[0m" 
        echo -e "[1;33m"":: Debugging shell started. Ctrl+D: continue booting. Ctrl+Alt+Del: reboot""[0m" 
        sh 2>/dev/null
}

XXmoveroot(){
	[ -d ${OFS_HOME} ] || mkdir -p ${OFS_HOME}
	mount --move ${ROOT_MNT} ${OFS_HOME}
    	OFS_LOWER="${OFS_HOME}/${rootdir}"
}
#moveroot
    mkdir -p "${OFS_HOME}"
    default_mount_handler "${OFS_HOME}" && 
    	OFS_LOWER="${OFS_HOME}/${rootdir}"

	if [ "${aufs}" != "y" ]; then 
	    case ${aufs} in
	    /dev/* | LABEL=* | UUID=* | PARTLABEL=* | PARTUUID=* )
		#aufs=/dev/sda1:dir
		device="${aufs%%:*}"
		path="${aufs##*:}"
		STEP "$device"
	      if [[ "${device}" = "${path}" ]]; then
		    err "Syntax error while parsing '$aufs' argument..."
		    err "Example: aufs=/dev/sda1:dir"
		    launch_interactive_shell
	      elif [[ "${device}" = "${root}" ]]; then
		    err "Syntax error while parsing '$aufs' argument..."
		    err "root= & aufs= equal. Use: aufs=dir"
		    launch_interactive_shell
	      else
		msg ":: Trying to mount (${device})"
		mkdir -p  "${OFS_COWSPACE_DEV}" 
		mount "${device}" "${OFS_COWSPACE_DEV}" 
		if [[ ! $? -eq 0 ]]; then
		    err "Unable to mount '$aufs' source: ${device}"
		    echo "You are being dropped to a recovery shell"
		    echo " Try to mount $device in ${OFS_COWSPACE_DEV}"
		    launch_interactive_shell
		    msg "Trying to continue..."
		fi
		STEP "mount ${device} ${OFS_COWSPACE_DEV} "
	      fi
		
		OFS_COWSPACE="${OFS_COWSPACE_DEV}/${path}"
		mkdir -p ${OFS_COWSPACE}
		;;
	    * )
		OFS_COWSPACE="${OFS_LOWER}-${aufs}"
		mkdir -p ${OFS_COWSPACE}
		;;
	    esac
	    STEP "$aufs"
	else
	    mkdir -p ${OFS_COWSPACE}
	    mount -t tmpfs cowspace ${OFS_COWSPACE}
	fi

mount -t aufs -o br=${OFS_COWSPACE}=rw:${OFS_LOWER}=ro+wh aufs ${ROOT_MNT} 
#    /bin/mount -t aufs -o nowarn_perm,br:"${src}" rootaufs "${mnt}"
     if [ $? != 0 ]; then
	err "aufs : mount -t aufs -o br=${OFS_COWSPACE}=rw:${OFS_LOWER}=ro+wh aufs ${ROOT_MNT} "
        launch_interactive_shell
#        return
     fi
STEP
simlink1(){
	    HOME="${OFS_LOWER}/mnt/home"
	    rmdir "${HOME}" 2>/dev/null
	    [ -L "${HOME}" ] && rm "${HOME}"
	    ln -sf "${OFS_LOWER}" "${HOME}"
		if [ $? != 0 ]; then
	            err " aufs err: ln -sf ${OFS_LOWER} ${HOME}"
	            launch_interactive_shell
#	            return
	        fi
}
simlink(){
LNK(){
	    rmdir "${2}" 2>/dev/null
	    [ -L "${2}" ] && rm "${2}"
	    mkdir -p "`dirname "$2"`"
	    [ -d "$1" ] && ln -sf "${1}" "${2}"
}
msg ":: Make simlink like porteus-initrd"
newroot="/new_root"
LNK "${OFS_HOME}" "${newroot}/mnt/home"
LNK "${OFS_COWSPACE}" "${newroot}/mnt/live/memory/changes"
}
simlink
STEP
}