# shellcheck    shell=sh
# Section: loadavg
# loadavg v1 v2 v3
___x_cmd_os_loadavg() {
    if [ "$#" -eq 0 ]; then
        ___x_cmd_os_loadavg___get
        return
    fi

    ___x_cmd readl "$@" <<A
$(___x_cmd_os_loadavg___get | tr ' ' '\n')
A

}

___x_cmd_os_loadavg___get() {
    ___x_cmd_os_name_
    case "$___X_CMD_OS_NAME_" in
        darwin | freebsd)       sysctl -q -n vm.loadavg | cut -d' ' -f2-4       ;;
        linux | win)            cut -d' ' -f1-3 </proc/loadavg                  ;; # win is cygwin
        openbsd)                sysctl -q -n vm.loadavg | cut -d' ' -f1-3       ;;
    esac
}
## EndSection

# Section: uptime

# NOTICE: In Docker, this will be the kernel uptime. Not the container uptime.
___x_cmd_os_uptime() {
    local x_=; ___x_cmd_os_uptime_ || return
    printf "%s\n" "$___X_CMD_OS_UPTIME_"
}

___x_cmd_os_uptime_() {
    x_=""
    if [ -z "$___X_CMD_OS_UPTIME_" ]; then
        ___x_cmd_os_name_
        case "$___X_CMD_OS_NAME_" in
            darwin | freebsd)
                eval "$({
                    command sysctl -q -n kern.boottime
                    command date +%s
                } | ___x_cmd_cmds_awk '
                    NR==1{ boot = $4; }
                    NR==2{ now  = $0; }
                    END{ print "___X_CMD_OS_UPTIME_=" ( now - boot ) }
                ')"
                ;;
            linux | win)
                ___X_CMD_OS_UPTIME_=$(cut -d' ' -f1 </proc/uptime)
                ;;
            openbsd)
                eval "$({
                    command sysctl -q -n kern.boottime
                    command date +%s
                } | ___x_cmd_cmds_awk '
                    NR==1{ boot = $0; }
                    NR==2{ now  = $0; }
                    END{ print "___X_CMD_OS_UPTIME_=" ( now - boot ) }
                ')"
                ;;
        esac
    fi

    x_="$___X_CMD_OS_UPTIME_"
}

# endSection
