#!/bin/sh
#181109 sfs

ROOT_MNT="/new_root"
OFS_DIRS="/run/archroot"
OFS_LOWER="${OFS_DIRS}/root_ro"
OFS_COWSPACE="${OFS_DIRS}/cowspace"

run_latehook() {
    if [ "x${ofs}" != "x" ]; then
	[ -d ${OFS_LOWER} ] || mkdir -p ${OFS_LOWER}
	mount --move ${ROOT_MNT} ${OFS_LOWER}
	mkdir -p ${OFS_COWSPACE}

	if [ "${ofs}" != "y" ]; then 
	    #msg2 "::: Mounting device ${src} to ${mnt}"
	    #mkdir -p "${changes}"
	    #mount "${changes}" ${OFS_COWSPACE}
	    OFS_COWSPACE="${OFS_LOWER}/${rootdir}-${ofs}"
	    rmdir "${OFS_LOWER}/$rootdir/mnt/home" 2>/dev/null
	    [ -L "${OFS_LOWER}/$rootdir/mnt/home" ] && rm "${OFS_LOWER}/$rootdir/mnt/home"
	    ln -sf "${OFS_LOWER}" "${OFS_LOWER}/$rootdir/mnt/home"
     if [ $? != 0 ]; then
        err " ofs2a err: ln -sf ${OFS_LOWER} ${OFS_LOWER}/mnt/home"
        launch_interactive_shell
        return
     fi
	else
	    mount -t tmpfs cowspace ${OFS_COWSPACE}
	fi

	OFS_UPPER="${OFS_COWSPACE}/upper"
	[ -d ${OFS_UPPER} ] || mkdir -p ${OFS_UPPER}
	OFS_WORK="${OFS_COWSPACE}/work"
	[ -d ${OFS_WORK} ] || mkdir -p ${OFS_WORK}

	mount -t overlay -o lowerdir=${OFS_LOWER},upperdir=${OFS_UPPER},workdir=${OFS_WORK} rootfs ${ROOT_MNT}
     if [ $? != 0 ]; then
        err " ofs2b err: mount -t overlay -o lowerdir=${OFS_LOWER},upperdir=${OFS_UPPER},workdir=${OFS_WORK} rootfs ${ROOT_MNT}"
        launch_interactive_shell
        return
     fi
    fi
}
