# shellcheck shell=dash

___x_cmd_hub_file_cat(){
    local X_help_cmd='___x_cmd_hub___help file cat'
    help:arg-null:parse

    local remotefp="$1"
    [ -n "$remotefp" ] || M='Provide resource path' N=hub log:ret:64
    remotefp="$(___x_cmd_hub_file_normalize_remotefp "$remotefp")"

    # TODO: wait curl module to fix
    local res="$remotefp"
    NO_AUTH=1 ___x_cmd_hub_u_curl get /api/v0/file/download/url res== | {

        local url=; local msg=
        ___x_cmd jo env . .url .msg
        [ -n "$url" ] || {
            ___x_cmd_hub_u_handle_resp false "Please check the remotefp $remotefp: "
            return 1
        }

        hub:debug "Downloading $url"
        local ___X_CMD_HUB_CAT_FILEKEY=
        ___X_CMD_HUB_CAT_FILEKEY="$(___x_cmd_hub_u_curl_header encryption-filekey-base64)"

        ___x_cmd curl -sSL "$url" | ___x_cmd_hub_file_parse_stream "$remotefp" || {
            ___x_cmd_ui_tf false "Failed to decrypt file" >&2
            return 1
        }
    }
}
