#!/bin/sh -e

__cpu_temp_detail() {
  case "$(uname)" in
    "OpenBSD")
      sysctl hw.sensors | grep -E 'hw.sensors.*temp'
      ;;
    "FreeBSD")
      sysctl dev.cpu | grep -E 'dev.cpu.*temperature'
      ;;
    "Linux")
      local i
      for i in $MONITORED_TEMP /sys/class/hwmon/hwmon*/device/temp*_input /sys/class/hwmon/hwmon*/temp*_input /proc/acpi/ibm/thermal /proc/acpi/thermal_zone/*/temperature /sys/class/thermal/thermal_zone*/temp; do
        [ -r "$i" ] || continue
        printf "%s\n" "$i:"
        cat "$i"/*
      done
      ;;
  esac
}

__cpu_temp() {
  local i t unit
  case "$(uname)" in
    "NetBSD")
      t=$(envstat | awk '/cpu.*temperature:/ && $3 != "N/A" {printf "%d\n", $3; exit}')
      color b k Y; printf "%s%s" "$t" "℃ "; color --
      ;;
    "OpenBSD")
      t=$(sysctl hw.sensors | grep -E 'hw.sensors.*temp' | awk '{ print $1 }' | head -1 | sed 's/^.*=//' | sed 's/\..*$//')
      color b k Y; printf "%s%s" "$t" "℃ "; color --
      ;;
    "FreeBSD")
      t=$(sysctl dev.cpu | grep -E 'dev.cpu.*temperature' | awk '{ print $2 }' | head -1 | sed 's/\..*$/C/')
      color b k Y; printf "%s%s" "$t" "℃ "; color --
      ;;
    "Linux")
      for i in $MONITORED_TEMP /sys/class/hwmon/hwmon*/device/temp*_input /sys/class/hwmon/hwmon*/temp*_input /proc/acpi/ibm/thermal /proc/acpi/thermal_zone/*/temperature /sys/class/thermal/thermal_zone*/temp; do
        case "$i" in
          *temp*_input|*thermal_zone*/temp)
            [ -s "$i" ] && read t < "$i" && t=$(($t/1000))
          ;;
          *)
            [ -s "$i" ] && t=$($BYOBU_SED -e "s/^[^0-9]\+//" -e "s/\s.*$//" "$i")
          ;;
        esac

        if [ -n "$t" ] && [ "$t" -gt 0 ]; then
          color b k Y; printf "%s%s" "$t" "℃ "; color --
          break
        fi
      done
      ;;
    "Darwin")
      t=$(sysctl -n machdep.xcpm.cpu_thermal_level)
      color b k Y; printf "%s%s" "$t" "℃ "; color --
  esac
}

# vi: syntax=sh ts=4 noexpandtab