#!/bin/sh
#181211 sfs

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

run_latehook() {
#    [ "x${aufs}" = "x" ] && return
    [ "${aufs}" ] || return

moveroot(){
	[ -d ${OFS_LOWER} ] || mkdir -p ${OFS_LOWER}
	mount --move ${ROOT_MNT} ${OFS_LOWER}
	OFS_LOWER="${OFS_DIRS}/root_ro/${rootdir}"
}
moveroot

	if [ "${aufs}" != "y" ]; then 
	    #msg2 "::: Mounting device ${src} to ${mnt}"
	    OFS_COWSPACE="${OFS_LOWER}-${aufs}"
	    mkdir -p ${OFS_COWSPACE}
simlink(){
	    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
	else
	    mkdir -p ${OFS_COWSPACE}
	    mount -t tmpfs cowspace ${OFS_COWSPACE}
	fi

XXX(){
	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}
#mount -t aufs -o dirs=/$SYSMNT/changes$N/=rw aufs /$SYSMNT/aufs$N && echo "$SYSMNT/aufs$N" || return 1
#mount -t aufs -o dirs=${OFS_COWSPACE}/=rw aufs ${ROOT_MNT} 
#mount -t aufs -o br=${OFS_UPPER}=rw:${OFS_LOWER}=ro+wh rootaufs ${OFS_WORK} 
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 err: mount -t aufs -o dirs=${OFS_COWSPACE}/=rw aufs ${OFS_WORK} "
	err "mount -t aufs -o br=${OFS_COWSPACE}=rw:${OFS_LOWER}=ro+wh aufs ${ROOT_MNT} "
        launch_interactive_shell
        return
     fi
#        rmdir /new_root && ln -s ${OFS_WORK}  /new_root
#        launch_interactive_shell

x1(){
mount -o remount,add:1:"${OFS_LOWER}"=ro+wh ${ROOT_MNT} 
#mount -o remount,append:1:"${OFS_LOWER}"=ro+wh none ${ROOT_MNT} 
#    /bin/mount -t aufs -o remount,append:"${src}"=rr+wh none "${mnt}"
#&& echo "$SYSMNT/aufs$N" || return 1
     if [ $? != 0 ]; then
        err " aufs err: mount -o remount,add:1:"${OFS_LOWER}"=ro+wh ${ROOT_MNT} "
        err " aufs err: mount -o remount,append:1:"${OFS_LOWER}"=ro+wh none ${ROOT_MNT}  "
        launch_interactive_shell
        return
     fi
}

}

###############################
_mnt_aufs_init() {
    src="${1}"
    mnt="${2}"
    msg2 "::: Creating aufs mount ${src} in ${mnt}"
    mkdir -p "${mnt}"
    #/bin/mount -t aufs -o br:"${src}" rootaufs "${mnt}"
    /bin/mount -t aufs -o nowarn_perm,br:"${src}" rootaufs "${mnt}"
}
# args: source, mountpoint
_mnt_aufs_add() {
    src="${1}"
    mnt="${2}"
    msg2 "::: Adding new aufs branch ${src} in ${mnt}"
    #/bin/mount -t aufs -o remount,append:"${src}" none "${mnt}"
    /bin/mount -t aufs -o remount,append:"${src}"=rr+wh none "${mnt}"
}

X(){
    aufs_base="/mnt/"
#    aufs_root="${aufs_base}root"
    aufs_root="${aufs_base}home"
#    aufs_rw="${aufs_base}rw"
    aufs_rw="${aufs_base}live/memory/changes"
    aufs_rwtype=""
    #overlay: "${aufs_base}${img##*/}"    
    _mnt_aufs_init ${aufs_rw} "${newroot}"
        _mnt_aufs_add ${mnt} "${newroot}"
}


XX(){
####
copyramdir="/tmp/.mountRAM"
prefixmp="/mnt/."
changesDir="/mnt/live/memory/changes/"
aufsroot="$(`which busybox 2>/dev/null` mount |egrep ' on / type aufs ')" 
[ "$aufsroot" ] && SYSMNT=/$(cut -f2 -d/ /sys/fs/aufs/si_$(grep ' / aufs' /proc/mounts | cut -f2 -d= | tr ',' ' ' | cut -f1 -d' ')/br0)

mkaufs() {
#make new aufs
N=1
while [ -f $SYSMNT/aufs${N}.lock ] ; do  N=$(($N +1)) ; done 
echo "$SYSMNT/aufs$N" > $SYSMNT/aufs${N}.lock
mkdir $SYSMNT/changes$N
mount -t tmpfs tmpfs /$SYSMNT/changes$N
mkdir $SYSMNT/aufs$N
mount -t aufs -o dirs=/$SYSMNT/changes$N/=rw aufs /$SYSMNT/aufs$N && echo "$SYSMNT/aufs$N" || return 1
}

addlayer() {
#add new aufs layer, $1 - aufs number, $2 - layer source
NEWLAYER=$(realpath "$2") || return 1
AUFSMNT="/"
case "$1" in
	0) shift;;
	[1-9]) N=$1
		if [ $N -lt $(ls -d /sys/fs/aufs/si_* | wc -w) ]; then
			AUFSMNT=$SYSMNT/aufs$N
			shift
		else
			return 2
		fi
	;;
	*) return 3
esac
[ "$(grep aufs$N /proc/mounts)" ] || return 4
MODNAME="$(basename "$NEWLAYER")"
#if df --output=fstype $NEWLAYER |grep -q ^aufs$ ;then
if [ "$(cat /proc/mounts |egrep '^'`df "$NEWLAYER" |tail -1 |cut -f1 -d " "`' ' |cut -f3 -d " " |uniq)" = aufs ];then
	[ "$(grep $SYSMNT/tmpfs$N /proc/mounts)" ] || ( mkdir $SYSMNT/tmpfs$N && mount -t tmpfs tmpfs $SYSMNT/tmpfs$N )
	cp -auPR "$NEWLAYER" "$SYSMNT/tmpfs$N/"
	NEWLAYER="${SYSMNT}/tmpfs${N}/$(basename $NEWLAYER)"
fi
case $(file -b $NEWLAYER) in
	*directory) 
		mkdir -p "$SYSMNT/bundles$N/$MODNAME"
		mount --bind "$NEWLAYER" "/$SYSMNT/bundles$N/$MODNAME"
		NEWLAYER="$SYSMNT/bundles$N/$MODNAME"
	;;
	Squashfs*4.0*|Linux*ext*)
		mkdir -p "$SYSMNT/bundles$N/$MODNAME"
		mount -o loop "$NEWLAYER" "/$SYSMNT/bundles$N/$MODNAME"
		NEWLAYER="$SYSMNT/bundles$N/$MODNAME"
	;;
	*) return 5 ;;
esac
mount -o remount,add:1:"$NEWLAYER"=ro+wh "$AUFSMNT" || return 5
echo $NEWLAYER
}

root_br="$(mkaufs || exitmsg "mkaufs error" 2)"
nn="$(echo "$root_br" | sed -n 's/^.*\([0-9]\)$/\1/p')"
[ -d "$root_br" ] || exitmsg "error mounting aufs" 3
echo "aufs number: $nn"

#add sources as aufs layers
for  i in $(get_layers) ;do
    eval addlayer "$nn" "$i" "$devnull" || exitmsg "can't insert layer to aufs $nn" 5
done 
}