# shellcheck shell=bash disable=SC2154,SC2034
# Restricted to the normal user

# x shodan create 8.8.8.8 1.1.1.1=53/dns-udp,443/https
___x_cmd_shodan_scan_create(){
    local X_help_cmd='x help -m shodan scan create';  help:arg:parse
    shodan:arg:format:raw:json2app:all
    local ips=
    local body

    local x_

    local NL="$___X_CMD_UNSEENCHAR_NEWLINE"

    while [ $# -gt 0 ]; do
        case "$1" in
            *=*)    ___x_cmd_shodan_scan_create___handle "$1"
                    body="${body}${NL}${x_}" ;;
            *)      ips="$ips,$1" ;;
        esac
        shift 1
    done


    [ -z "$ips" ]  || set -- "-d 'ips=${ips#,}'"

    [ -z "$body" ] || {

        body="{${NL}$body${NL}}"
        body="$(___x_cmd_pipevar body   x jo fmt)";
        set -- "$@" "-d '$body'"

    }

    ___x_cmd_shodan_scan_create___"$format" "$@"
}


___x_cmd_shodan_scan_create___handle(){
    local ip="${1%%=*}"
    local data="${1#*=}"

    x_=""

    local item
    local port
    local desc

    while true; do
        item="${data%%,*}"

        port="${item%%/*}"
        desc="${item#"${port}"/}"

        x_="$x_
[ $port, \"$desc\" ], "

        [ "$item" != "$data" ] || break
        data="${data#"$item",}"
    done
    x_="\"$ip\": [ $x_ ]"
}



___x_cmd_shodan_scan_create___app(){
    local data;     data="$( ___x_cmd_shodan_scan_create___raw "$@" )"    || shodan:data:ret
    ___x_cmd_pipevar data       x j2y
}

___x_cmd_shodan_scan_create___json(){
    local data;     data="$( ___x_cmd_shodan_scan_create___raw "$@" )"    || shodan:data:ret
    ___x_cmd_pipevar data
}

___x_cmd_shodan_scan_create___raw(){
    ___x_cmd_shodan_ourl post "/shodan/scan" "$@" # | x j2y
}


