# shellcheck shell=dash disable=2016

___x_cmd_uptime___main(){
    [ "$#" -gt 0 ] || set -  --yml

    local op="$1"; shift
    case "$op" in
        --help|-h)      ___x_cmd help -m uptime "$@" ;;
        -y|--yml)       ___x_cmd_uptime___cmd_yml ;;
        --raw)          ___x_cmd_uptime___cmd_raw "$@" ;;

        get|get_|idle|idle_|summary) ___x_cmd_uptime_"$op"   "$@" ;;
        *)              ___x_cmd_uptime___cmd_raw "$op" "$@" ;;
    esac
}

___x_cmd_uptime___cmd_raw(){
    if ___x_cmd hascmd uptime; then
        ___x_cmd_uptime___cmd_raw(){
            ___x_cmd_cmds uptime "$@"
        }
    elif ___x_cmd hascmd busybox && ___x_cmd_cmds busybox uptime 2>/dev/null 1>&2; then
        ___x_cmd_uptime___cmd_raw(){
            ___x_cmd_cmds busybox uptime
        }
    else
        ___x_cmd_uptime___cmd_raw(){
            ___x_cmd cosmo uptime "$@"
        }
    fi

    ___x_cmd_uptime___cmd_raw "$@"
}

___x_cmd_uptime___cmd_yml(){
    local NO_COLOR="$NO_COLOR"
    ___x_cmd_is_stdout2tty || NO_COLOR=1
    ___x_cmd_uptime___cmd_raw 2>/dev/null | \
        ___x_cmd_cmds awk -v NO_COLOR="$NO_COLOR" -f "$___X_CMD_ROOT_MOD/uptime/lib/parse.awk"
}


___x_cmd_uptime_get(){
    local x_
    ___x_cmd_uptime_get_ || return $?
    printf "%s\n" "$x_"
}

___x_cmd_uptime_idle(){
    local x_
    ___x_cmd_uptime_idle_ || return $?
    printf "%s\n" "$x_"
}

if ___x_cmd os is darwin; then
    ___x_cmd_uptime_get_(){
        local btime_; btime_=$(___x_cmd_cmds sysctl -n kern.boottime 2>/dev/null) || return 1
        btime_="${btime_#*sec = }"; btime_="${btime_%%,*}"
        local now_; now_=$(date +%s)
        x_=$(( now_ - btime_ ))
    }

    ___x_cmd_uptime_idle_(){
        local idle_pct
        idle_pct=$(___x_cmd_cmds python3 "$___X_CMD_ROOT_MOD/uptime/lib/idle_darwin.py" 2>/dev/null)
        ___x_cmd_uptime_get_ || return 1; local uptime_="$x_"
        local cpu_count; cpu_count=$(___x_cmd_cmds sysctl -n hw.ncpu 2>/dev/null) || cpu_count=1
        x_=$(___x_cmd_cmds awk -v p="$idle_pct" -v u="$uptime_" -v c="$cpu_count" 'BEGIN { printf "%d", p/100*u*c }')
        [ "$x_" -lt 0 ] && x_=0; :
    }

    ___x_cmd_uptime_summary(){
        local uptime_ idle_ cpu_count
        ___x_cmd_uptime_get_ || return 1; uptime_="$x_"
        ___x_cmd_uptime_idle_; idle_="$x_"
        cpu_count=$(___x_cmd_cmds sysctl -n hw.ncpu 2>/dev/null) || cpu_count=1
        printf "uptime : %s seconds\n" "$uptime_"
        printf "cores  : %s\n" "$cpu_count"
        ___x_cmd_cmds awk -v uptime_="$uptime_" -v idle_="$idle_" -v cpu_count="$cpu_count" 'BEGIN {
            printf "idle%%  : %.2f%%\n", idle_ / (uptime_ * cpu_count) * 100
        }'
    }
else
    ___x_cmd_uptime_get_(){
        read -r x_ </proc/uptime
        x_="${x_% *}"
    }

    ___x_cmd_uptime_idle_(){
        read -r x_ </proc/uptime
        x_="${x_#* }"
    }

    ___x_cmd_uptime_summary(){
        local uptime_ idle_ cpu_count=0
        read -r uptime_ idle_ </proc/uptime
        while IFS=: read -r key _; do
            case "$key" in processor) cpu_count=$((cpu_count + 1)) ;; esac
        done < /proc/cpuinfo
        [ "$cpu_count" -gt 0 ] || cpu_count=1

        printf "uptime : %s seconds\n" "$uptime_"
        printf "cores  : %s\n" "$cpu_count"

        ___x_cmd_cmds awk -v uptime_="$uptime_" -v idle_="$idle_" -v cpu_count="$cpu_count" 'BEGIN {
            printf "idle%%  : %.2f%%\n", idle_ / (uptime_ * cpu_count) * 100
        }'
    }
fi
