#!/bin/sh -e

# colors:
# 0 = neutral
# 1 = green
# 2 = yellow
# 3 = red

CPU_TEMP="N/A"
CPU_SPEED="N/A"
FAN_SPEED="N/A"
BATTERY="N/A"

cpu_temp_openbsd() {
    _v=$(/sbin/sysctl -n hw.sensors.acpithinkpad0.fan0)
    CPU_TEMP=$(set_value_color 1400 1 1700 2 $_v )
}
cpu_speed_openbsd() {
    _v=$(/sbin/sysctl -n hw.cpuspeed)
    CPU_SPEED=$(set_value_color 1400 1 1700 2 $_v )
}
battery_openbsd() {
    _v=$(/usr/sbin/apm -l)
    BATTERY=$(set_value_color 10 3 98 1 $_v )
}
fan_speed_openbsd() {
    _v=$(/sbin/sysctl -n hw.sensors.ksmn0.temp0 | cut -d"." -f1)
    FAN_SPEED=$(set_value_color 0 1 3600 3 $_v )
}

set_value_color() {
  # $1 low_value
  # $2 low_color
  # $3 high_value
  # $4 high_color
  # $5 current_value
  [ $5 -ge $3 ] && printf '+@fg=%s;%s+@fg=0;' $4 $5 && return
  [ $5 -le $1 ] && printf '+@fg=%s;%s+@fg=0;' $2 $5 && return
  printf "$5"
}

while true;
do
    cpu_temp_openbsd
    cpu_speed_openbsd
    battery_openbsd
    fan_speed_openbsd
    printf 'BAT: %s%% CPU: %s°C %s Mhz %s\n' \
        "$BATTERY" \
        "$FAN_SPEED" \
        "$CPU_SPEED" \
        "$CPU_TEMP";
    sleep 1;
done
