# shellcheck shell=dash disable=SC2154,SC2034
___x_cmd_shodan_host_ls(){
    local X_help_cmd='x help -m shodan host ls';   help:arg:parse
    local ip="$1";  ___x_cmd_shodan_host_ls___ip || return

    shodan:info "Returns all services that have been found on the given host [IP=$ip]"
    ___x_cmd_shodan_is_interactive_env || {
        ___x_cmd_shodan_host_ls___curl
        return
    }

    ___x_cmd_shodan_host_ls___csv | x csv app --width 20%,20%,20%,20%,20% --return print
}

___x_cmd_shodan_host_ll(){
    local ip="$1";  ___x_cmd_shodan_host_ls___ip || return
    ___x_cmd_shodan_host_ls___csv | x csv static_tab
}

___x_cmd_shodan_host_ls___csv(){
    ___x_cmd_shodan_host_ls___curl | {
        local ip_str=;
        local city=;        local country_name=;    local org=;
        local last_update=; local data=;            local error=;

        x jo  env .  .ip_str .city .country_name .org .last_update  .data .error
        [ -n "$ip_str" ] || [ -n "$ip_str" ] || N=shodan M="$error" log:ret:1

        shodan:info "Pasrsing JSON to CSV -- [ip:$ip_str] [country:$country_name] [city:$city] [org:$org] [last_update:$last_update]"

        # printf "\nIP: %s\nOrg: %s\nCity: %s\nCountry: %s\nLast Update: %s\n\n" \
        #     "${ip_str}" "${org}" "${city}" "${country_name}" "${last_update}"

        printf "\n%s\n" "${data}" | \
            x ja 2c .port .transport .http.title .domains.1 .timestamp          | \
            x csv header --add  Port  Protocol  Description Domains Timestamp
    }
}

___x_cmd_shodan_host_ls___curl(){
    ___x_cmd_shodan_ourl get "/shodan/host/${ip}"
}

___x_cmd_shodan_host_ls___ip(){
    if [ -z "$ip" ]; then
        shodan:info "Fetch your public IP to query"
        ip="$(x ip echo)" || return
    fi

    x is ip "$ip" || {
        shodan:info "Fetching the IP of the [host=$ip]"
        # reverse loopkup the host for IP
    }

    # x is ip "$ip" || shodan:info "Fetching the IP of the [host=$ip]"
    # shodan:info "Returns all services that have been found on the given host [IP=$ip]"
}
