# shellcheck shell=dash

___x_cmd log init ping
xrc:mod:lib ping    vis rec  __run/_index
# single ...
# x ping --csv bing.com x-cmd.com
# x ping --tsv bing.com x-cmd.com

# support multiple hosts ...
# using ping to monitor multiple servers ...

___x_cmd_ping___main(){
    [ $# -gt 0 ]        ||  set -- --run

    local op="$1";      shift
    case "$op" in
        --help|-h)      ___x_cmd help -m ping            ;;
        vis|rec|test)   ___x_cmd_ping_"$op"         "$@" ;;
        --run)          ___x_cmd_ping___run         "$@" ;;
        --|exec)        ___x_cmd_ping___exec        "$@" ;;

        isbusybox)      ___x_cmd_ping___is_busybox       ;;
        *)              ___x_cmd_ping___run "$op"   "$@" ;;
    esac
}


___x_cmd_ping___is_busybox(){
    local helpmsg;      helpmsg="$(___x_cmd_cmds ping -h 2>&1)"
    case "$helpmsg" in
        *BusyBox*)      return 0  ;;
        *)              return 1  ;;
    esac
}

___x_cmd_ping___exec(){
    if ___x_cmd_hascmd ping; then
        LC_ALL="$___X_CMD_LOCALE_DEF_C" command ping "$@"
    else
        ping:info "ping command not found, use the busybox's ping command"
        LC_ALL="$___X_CMD_LOCALE_DEF_C" ___x_cmd busybox ping "$@"
    fi
}

___x_cmd_ping_test(){
    local x_=;
    ___x_cmd os name_
    case "$x_" in
        linux)      set -- -W 1 -c 1 "$1"    ;;
        darwin)     set -- -t 1 -c 1 "$1"    ;;
        win)        set -- -w 1000 -n 1 "$1" ;;
    esac

    ___x_cmd_ping___exec "$@"    1>/dev/null 2>/dev/null
}
