# shellcheck shell=dash disable=SC2154,2034
# https://geonet.shodan.io/docs#/default/geo_dns_query_api_geodns__hostname__get
___x_cmd_shodan_geodns(){
    local X_help_cmd='x help -m shodan geo geodns';   help:arg-null:parse
    shodan:arg:format:init

    local rtype=A; local hostname=
    while [ "$#" -gt 0 ]; do
        shodan:arg:format:match
        case "$1" in
            --rtype)    rtype="$2";     shift 2 ;;
            *)          hostname="$1";  break ;;
        esac
    done

    shodan:arg:format:fini

    [ -n "$hostname" ] || N=shodan M="Provide a hostname" log:ret:64
    ___x_cmd_shodan_geodns___"$format"
}

___x_cmd_shodan_geodns___app(){
    local data; data="$( ___x_cmd_shodan_geodns___csv )"   || shodan:data:ret
    ___x_cmd_pipevar data  x csv app --return print
}

___x_cmd_shodan_geodns___csv(){
    local data; data="$( ___x_cmd_shodan_geodns___raw )"   || shodan:data:ret
    ___x_cmd_pipevar data   ___x_cmd_shodan_geodns___parse_json2csv
}

___x_cmd_shodan_geodns___json(){
    local data; data="$( ___x_cmd_shodan_geodns___raw )"   || shodan:data:ret
    ___x_cmd_pipevar data
}

___x_cmd_shodan_geodns___raw(){
    x curl --fail-with-body -sS \
        "https://geonet.shodan.io/api/geodns/${hostname}?rtype=${rtype}"
}

# Section: parse
___x_cmd_shodan_geodns___parse_json2csv(){
    ___x_cmd_shodan_geodns___csvappparse  | x csv convert --col
}

___x_cmd_shodan_geodns___csvappparse(){
    x ja -f "$___X_CMD_ROOT_MOD/shodan/lib/geo/awk/geodns.awk"
}
# EndSection
