# shellcheck shell=dash
___x_cmd_shodan_cpe_search(){

    local IFS=' '

    local format=yml

    local product=
    local skip=
    local limit=

    while [ $# -gt 0 ]; do
        case "$1" in
            --product)          product="$2" ;          shift 2 ;;
            --skip)             skip="$2" ;             shift 2 ;;
            --limit)            limit="${2}";           shift 2 ;;

            --json)             format=json;            shift 1 ;;
            --yml)              format=yml;             shift 1 ;;
            # --app)              format=app;          shift 1 ;;
            -h|--help)          ___x_cmd help -m shodan cpe search; return  ;;
            *)                  product="$*" ;          break ;;
        esac
    done

    ___x_cmd_shodan_cpe_search___"$format"
}

# ___x_cmd_shodan_cpe_search___app(){
#     ___x_cmd_shodan_cpe_search___json "$@" | \
#         ___x_cmd jo .cpes |\
#         ___x_cmd jo .cpes | ___x_cmd bat -l txt
# }

___x_cmd_shodan_cpe_search___yml(){
    if ___x_cmd_is_stdout2tty; then
        ___x_cmd_shodan_cpe_search___yml_ | ___x_cmd less
    else
        ___x_cmd_shodan_cpe_search___yml_
    fi
}

___x_cmd_shodan_cpe_search___yml_(){
    ___x_cmd_shodan_cpe_search___json  | ___x_cmd j2y
}

___x_cmd_shodan_cpe_search___json(){
    ___x_cmd_shodan_cpe_search___raw | ___x_cmd jo .cpes
}

___x_cmd_shodan_cpe_search___raw(){
    [ -z "$product" ]   || set -- "$@" --data-urlencode "product=$product"
    [ -z "$skip" ]      || set -- "$@" --data-urlencode "skip=$skip"
    [ -z "$limit" ]     || set -- "$@" --data-urlencode "limit=$limit"

    ___x_cmd ccmd 1h -- \
        ___x_cmd curl --silent --get \
            'https://cvedb.shodan.io/cpes' "$@"
}


