# shellcheck shell=dash disable=SC2089,2034

# generate for create rm triggers edit ed like ___x_cmd_shodan_alert_info

# This function is used to create a new alert
___x_cmd_shodan_alert_create(){
    local X_help_cmd='x help -m shodan alert create';   help:arg-null:parse
    shodan:arg:format:raw:json2app:all

    local expire=; local x_=
    [ "$#" -ge 2 ] || N=shodan M="x shodan alert create <alert name> <IPs>" log:ret:1
    local name="$1" ; shift

    while [ "$#" -gt 0 ]; do
        case "$1" in
            [0-9]*)
                ___x_cmd_shodan_alert_create_append___handle "$1"; shift
                ;;
            --expires|--expire)
                expire="$2"; shift 2
                ;;
            *)
                shodan:error Invalid IP:"$1"; return 1
                ;;
        esac
    done
    local ip="$x_"
    set --  "-d '{\"name\": \"$name\", \"filters\": {\"ip\": [$ip]}, \"expires\": ${expire:-0}}'"

    ___x_cmd_shodan_alert_create___"$format" "$@"
}

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

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

___x_cmd_shodan_alert_create_append___handle(){
    if [ -n "$x_" ]; then x_="$x_,\"$1\""
    else                  x_="\"$1\""
    fi
}

___x_cmd_shodan_alert_create___raw(){
    ___x_cmd_shodan_curl post "/shodan/alert" "$@"
}