# args: source, mountpoint
_mnt_bind() {
    local src="${1}"
    local mnt="${2}"
    msg "::: Binding ${src} to ${mnt}"
    /bin/mount -o bind "${src}" "${mnt}"
}

# args: /path/to/image_file, mountpoint
_mnt_squashfs() {
    local img="${1}"
    local mnt="${2}"
    local img_fullname="${img##*/}";
    local img_name="${img_fullname%.*}"
    local tmp_mnt="/ro_branch/${img_name}"

    if [ "${copytoram}" = "y" ]; then
        msg -n ":: Copying squashfs image to RAM..."
        /bin/cp "${img}" "/copytoram/${img_fullname}"
        if [ $? -ne 0 ]; then
            echo "ERROR: while copy ${img} to /copytoram/${img_fullname}"
            launch_interactive_shell
        fi
        img="/copytoram/${img_fullname}"
        msg "done."
    fi

    mkdir -p "${tmp_mnt}"
    /bin/mount -r -t squashfs "${img}" "${tmp_mnt}"
    if [ $? -ne 0 ]; then
        echo "ERROR: while mounting ${img} to ${tmp_mnt}"
        launch_interactive_shell
    fi

    if [ "/${mnt#/*/}" != "/" ]; then
        _mnt_bind "${tmp_mnt}" "${mnt}"
    fi
}

run_hook() {
    modprobe loop
    if [ "x${arch}" = "x" ]; then
        arch="$(uname -m)"
    fi

    if [ "x${rw_branch_size}" = "x" ]; then
        rw_branch_size="75%"
    fi

    if [ "x${copytoram_size}" = "x" ]; then
        copytoram_size="75%"
    fi

    if [ "x${chakraisobasedir}" = "x" ]; then
        chakraisobasedir="chakra"
    fi

    if [ "x${isomounts}" != "x" ]; then
        isomounts="/bootmnt/${isomounts}"
    else
        isomounts="/bootmnt/${chakraisobasedir}/isomounts"
    fi

    if [ "x${chakraisodevice}" = "x" ]; then
        chakraisodevice="/dev/disk/by-label/${chakraisolabel}"
    fi

    # set mount handler for chakraiso
    mount_handler="chakraiso_mount_handler"
}

chakraiso_mount_handler() {
    local newroot="${1}"

    msg ":: Waiting for boot device..."
    while ! poll_device ${chakraisodevice} 30; do
        echo "ERROR: boot device didn't show up after 30 seconds..."
        echo "   Falling back to interactive prompt"
        echo "   You can try to fix the problem manually, log out when you are finished"
        launch_interactive_shell
    done

    FSTYPE=$(blkid -o value -s TYPE -p ${chakraisodevice} 2> /dev/null)
    #FSTYPE="iso9660"
    if [ -n "${FSTYPE}" ]; then
        echo ":: cid was ${chakraisodevice}"
        if mount -r -t "${FSTYPE}" ${chakraisodevice} /bootmnt > /dev/null 2>&1; then
            if [ -e "${isomounts}" ]; then
                echo ":: Mounted chakraiso volume successfully."
                fserror="0"
            else
                echo "ERROR: Mounting was successful, but the ${isomounts} file does not exist."
                fserror="1"
            fi
        else
            echo "ERROR; Failed to mount ${chakraisodevice} (FS is ${FSTYPE})"
            fserror="1"
        fi
    else
        echo "ERROR: ${chakraisodevice} found, but the filesystem type is unknown."
        fserror="1"
    fi

    if [ "${fserror}" = "1" ]; then
        echo "   Falling back to interactive prompt"
        echo "   You can try to fix the problem manually, log out when you are finished"
        launch_interactive_shell
    fi

    if [ "${copytoram}" = "y" ]; then
        msg -n ":: Mounting /copytoram (tmpfs) filesystem, size=${copytoram_size}..."
        mount -t tmpfs -o "size=${copytoram_size}",mode=0755 copytoram /copytoram
        msg "done."
    fi

    msg -n ":: Mounting rw_branch (tmpfs) filesystem, size=${rw_branch_size}..."
    mount -t tmpfs -o "size=${rw_branch_size}",mode=0755 rw_branch /rw_branch
    mkdir /rw_branch/work
    mkdir /rw_branch/upper
    msg "done."

    msg ":: Mounting images"
    local i
    local lowerdirs
    local img_name
    for i in 0 1; do
        while read img imgarch mountpoint type; do
            # check if this line is a comment (starts with #)
            [ "${img#"#"}" != "${img}" ] && continue

            [ "$imgarch" != "$arch" ] && continue

            [ ! -r "/bootmnt/${chakraisobasedir}/${img}" ] && continue

            if [ "$i" == 0 ]; then
                mkdir -p "${newroot}${mountpoint}"
            else
                if [ "${type}" = "bind" ]; then
                    _mnt_bind "/bootmnt/${chakraisobasedir}/${img}" "${newroot}${mountpoint}"
                elif [ "${type}" = "squashfs" ]; then
                    _mnt_squashfs "/bootmnt/${chakraisobasedir}/${img}" "${newroot}${mountpoint}"

                    if [ "${mountpoint}" == "/" ]; then
                        img_name="${img##*/}"
                        img_name="${img_name%.*}"
                        if [ "${lowerdirs}" == "" ]; then
                            lowerdirs="/ro_branch/${img_name}"
                        else
                            lowerdirs="${lowerdirs}:/ro_branch/${img_name}"
                        fi
                    fi
                fi
            fi
        done < "${isomounts}"
    done

    msg ":: Mounting root (overlayfs) filesystem"
    /bin/mount -t overlay -o rw,lowerdir="${lowerdirs}:${newroot}",upperdir=/rw_branch/upper,workdir=/rw_branch/work overlay "${newroot}"
    if [ $? -ne 0 ]; then
        echo "ERROR: while mounting root (overlayfs) filesystem."
        launch_interactive_shell
    fi

    if [ "${copytoram}" = "y" ]; then
        /bin/umount /bootmnt
    else
        mkdir -p "${newroot}/bootmnt"
        _mnt_bind /bootmnt "${newroot}/bootmnt"
    fi

}

# vim:ft=sh:ts=4:sw=4:et:
