# PXVDI filesystem mounting                     -*- shell-script -*-
# shellcheck shell=sh

pxvdi_top()
{
	if [ "${pxvdi_top_used}" != "yes" ]; then
		[ "${quiet?}" != "y" ] && log_begin_msg "Running /scripts/pxvdi-top"
		run_scripts /scripts/pxvdi-top
		[ "$quiet" != "y" ] && log_end_msg
	fi
	pxvdi_top_used=yes
}

pxvdi_premount()
{
	if [ "${pxvdi_premount_used}" != "yes" ]; then
		[ "${quiet?}" != "y" ] && log_begin_msg "Running /scripts/pxvdi-premount"
		run_scripts /scripts/pxvdi-premount
		[ "$quiet" != "y" ] && log_end_msg
	fi
	pxvdi_premount_used=yes
}

pxvdi_bottom()
{
	if [ "${pxvdi_premount_used}" = "yes" ] || [ "${pxvdi_top_used}" = "yes" ]; then
		[ "${quiet?}" != "y" ] && log_begin_msg "Running /scripts/pxvdi-bottom"
		run_scripts /scripts/pxvdi-bottom
		[ "$quiet" != "y" ] && log_end_msg
	fi
	pxvdi_premount_used=no
	pxvdi_top_used=no
}

pxvdi_mount_root()
{
	pxvdi_top

	wait_for_udev 10

	pxvdi_premount

        mount -t tmpfs tmpfs /live
        mkdir /live/base /live/userdata /live/work -p

        # check live boot
        if [ "$pxvdi" = "live" ]; then
                echo "boot into live mode ..."
                if [ -f "/pxvdi.iso" ]; then
                        if ! mount -t iso9660 /pxvdi.iso /live/base; then
                                panic "mount /pxvdi.iso in pxe mode failed"
                        fi
                else
                        # check iso
                        PXVDI_ISO=`blkid -L PXVDI`
                        if [ ! -b "$PXVDI_ISO" ]; then
                                panic "No PXVDI ISO found"
                        fi

                        if ! mount -t auto $PXVDI_ISO  /live/base; then
                                panic "mount $PXVDI_ISO in live mode failed"
                        fi
                fi

        else
                PXVDIBASE=`blkid -L PXVDIBASE-$pxvdi`
                PXVDIUSERDATA=`blkid -L PXVDIUSERDATA`
                if [ -b "$PXVDIBASE" ]; then

                        if ! mount -t ext4 -o ro,loop "$PXVDIBASE" /live/base; then
                                panic "mount '$PXVDIBASE' failed"
                        fi

                        if [ -b "$PXVDIUSERDATA" ];then
                                if ! mount -t ext4 -o rw "$PXVDIUSERDATA" /live/work/; then
                                panic "mount '$PXVDIUSERDATA' failed"
                                fi
                        fi
                fi
        fi

        echo "create overlay filesystem ..."
        mkdir /live/work/userdata /live/work/base /live/work/work -p

        if ! mount -t squashfs -o rw "/live/base/pxvdi.img" /live/work/base; then
                panic "mount '$pxvdi' failed"
        fi

        if ! mount -t overlay -o lowerdir=/live/work/base,upperdir=/live/work/userdata,workdir=/live/work/work  none /root/; then
                panic "mount overlayfs failed"
        fi

        mkdir /root/pxvdi-iso
        if ! mount --bind /live/base /root/pxvdi-iso; then
                panic "bind mount iso failed"
        fi
}


mountroot()
{
	pxvdi_mount_root
}

mount_top()
{
	# Note, also called directly in case it's overridden.
	pxvdi_top
}

mount_premount()
{
	# Note, also called directly in case it's overridden.
	pxvdi_premount
}

mount_bottom()
{
	# Note, also called directly in case it's overridden.
	pxvdi_bottom
}