# shellcheck shell=dash
# data as host, ip, ip_str, coutnry, city, org ..., json
___x_cmd_shodan_host_csv(){
    ___x_cmd_shodan_host_csv_parse "$@"
}

# x shodan host csv -f "a.tar.gz" ip_str country_code country_name org data

___x_cmd_shodan_host_csv_parse(){
    local fp=-
    while [ $# -gt 0 ]; do
        case "$1" in
            -f|--filename)     fp="$2"; shift 2 ;;
            *)                 break            ;;
        esac
    done

    if [ "$fp" = - ]; then
        ___x_cmd_shodan_host_csv_parse_filter
    else
        case "$fp" in
            *.gz|*.xz|*.zst)
                ___x_cmd zuz cat "$fp" | ___x_cmd_shodan_host_csv_parse_filter "$@" ;;
            *)
                ___x_cmd_shodan_host_csv_parse_filter "$@" <"$fp" ;;
        esac
    fi
}

___x_cmd_shodan_host_csv_parse_filter(){
    [ $# -gt 0 ]    ||      set - --all

    case "$1" in
        --all)     x ja jl2c . ; return
    esac

    local cmd="x ja jl2c"
    local header="x csv header --add"
    while [ "$#" -gt 0 ]; do
        cmd="$cmd .$1"
        #TODO: rename
        header="$header $1"
        shift
    done
    eval "$cmd" | eval "$header"
}
