# shellcheck shell=dash

___x_cmd_rfc_txt(){
    [ "$#" -gt 0 ] ||   set - --help

    local fmt=app

    while [ $# -gt 0 ]; do
        case "$1" in
            -h|--help)      ___x_cmd help -m rfc txt "$@"; return ;;
            --raw|--app)    fmt="${1#--}" ;;
            *)              break ;;
        esac
        shift
    done

    ___x_cmd_rfc_txt___"$fmt" "$@"
}


___x_cmd_rfc_txt___app(){
    if ___x_cmd_is_stdout2tty && ___x_cmd_runmode_allow_manual; then
        ___x_cmd_rfc_txt___raw "$@" | ___x_cmd less
    else
        ___x_cmd_rfc_txt___raw "$@"
    fi
}

___x_cmd_rfc_txt___raw(){
    local id="$1" || M='Provide a id' N=rfc log:ret:1

    local txt_url="https://www.rfc-editor.org/rfc/rfc${id}.txt"
    case "$id" in
        rfc*)   txt_url="https://www.rfc-editor.org/rfc/${id}.txt"      ;;
        ien*)   txt_url="https://www.rfc-editor.org/rfc/ien/${id}.txt"  ;;
        bcp*)   txt_url="https://www.rfc-editor.org/rfc/bcp/${id}.txt"  ;;
        fyi*)   txt_url="https://www.rfc-editor.org/rfc/fyi/${id}.txt"  ;;
        std*)   txt_url="https://www.rfc-editor.org/rfc/std/${id}.txt"  ;;
    esac
    ___x_cmd ccmd 1d -- ___x_cmd curl "$txt_url"
}

