#!/bin/sh

#
# get-sensor  (C) 2001 Malte J. Wetz <mail@malte-wetz.de>
#
# Script for retrieving the value of any hardware sensor.
# Result will be printed to stdout. For use with raccess4vbox.
# Requires fully functional lm_sensors package installed.
# Get lm_sensors from http://www.lm-sensors.nu.
#
# Usage:
#   get-sensor <sensorname>
#
# Use sensor names as configured in /etc/sensors.conf.
# You may use the character "_" as a replacement for a blank
# space.
#
# Example:
#     earth:/usr/share/isdn/vbox/bin # get-sensors 'CPU Temp'
#     31 & point & 5 & degrees & celsius
#     earth:/usr/share/isdn/vbox/bin # _
#


################################################################
# SETTINGS: Change these parameters to meet your requirements.
################################################################

# Full path of sensors-binary. Use "which sensors" if you
# don't know it yourself.
SENSORS=/usr/bin/sensors

# Full path of grep-binary. Use "which grep" if you
# don't know it yourself.
GREP=/usr/bin/grep

# Full path of cut-binary. Use "which cut" if you
# don't know it yourself.
CUT=/usr/bin/cut

# Full path of awk-binary. Use "which awk" if you
# don't know it yourself.
AWK=/usr/bin/awk

# Full path of sed-binary. Use "which sed" if you
# don't know it yourself.
SED=/usr/bin/sed

# These strings will be used to generate output text. Change
# them to adopt output to your language.
#
# Uncomment these line for english:
POINT="point"
PLUS="plus"
DEGREE="degrees"
CELSIUS="Celsius"
FAHRENHEIT="Fahrenheit"
RPM="RPM"
VOLT="Volt"
# Uncomment these line for german:
#POINT="komma"
#PLUS="plus"
#DEGREE="Grad"
#CELSIUS="Celsius"
#FAHRENHEIT="Fahrenheit"
#RPM="Umdrehungen"
#VOLT="Volt"

################################################################
# END SETTINGS: There should be nothing to change below this
# point.
################################################################

SENSORNAME=$1

# Replaces "_" with " " in sensor name.
SENSORNAME=`echo $SENSORNAME | sed -e ':start; s/_/ /; t start'`

# Get the sensor line
LINE=`$SENSORS | $GREP "$SENSORNAME"`

# Get the sensor value from that line
VALUE=`echo $LINE | $CUT -d ':' -f 2`

# Stupid sensors will put units "RPM" and "V" behind value,
# seperated by white space, while "C" or "F" are concatenated
# without space. Further more, it uses space to indent output to
# equal length. Since we need the unit, we have to determine
# them like this:
VALUE=`echo $VALUE |  sed -e ':start; s/^\ //; t start; s// /' | $CUT -d '(' -f 1`
UNIT=`echo $VALUE | $CUT -d ' ' -f 2`
VALUE=`echo $VALUE | $CUT -d ' ' -f 1`

# Now parse value to sound nicely.
case "$UNIT" in
  [FC]*)
    # So it's a temperature value, uh?
    for i in `echo $VALUE | $AWK 'gsub(//," ")'`; do
      VTEXT="$VTEXT & $i"
    done
    VALUE=${VTEXT:3}
    VALUETEXT=`echo $VALUE | sed -e "s/+/$PLUS/; s/\./$POINT/"`
    if [ "$UNIT" = "C" ]; then
      VALUETEXT="$VALUETEXT & $DEGREE & $CELSIUS"
    elif [ "$UNIT" = "F" ]; then
      VALUETEXT="$VALUETEXT & $DEGREE & $FAHRENHEIT"
    else
      # Fail safe; degree's in whatever unit
      VALUETEXT="$VALUETEXT & $DEGREE"
    fi
  ;;
  RPM)
    # RPM's are fan speed units.
    for i in `echo $VALUE | $AWK 'gsub(//," ")'`; do
      VTEXT="$VTEXT & $i"
    done
    VALUE=${VTEXT:3}
    VALUETEXT="$VALUE & $RPM"
  ;;
  V)
    # Some kind of voltage value.
    for i in `echo $VALUE | sed -e 's/+//' | $AWK 'gsub(//," ")'`; do
      VTEXT="$VTEXT & $i"
    done
    VALUE=${VTEXT:3}
    VALUETEXT=`echo $VALUE | sed -e "s/\./$POINT/"`" & $VOLT"
  ;;
  *)
    # Fail safe; don't know what it is, just echo and hope for best.
    VALUETEXT="$VALUE & $UNIT"
  ;;
esac

echo "$VALUETEXT"

exit 0
