#!/bin/sh

################################################################################
#      This file is part of OpenELEC - http://www.openelec.tv
#      Copyright (C) 2009-2012 Stephan Raue (stephan@openelec.tv)
#
#  This Program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2, or (at your option)
#  any later version.
#
#  This Program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with OpenELEC.tv; see the file COPYING.  If not, write to
#  the Free Software Foundation, 51 Franklin Street, Suite 500, Boston, MA 02110, USA.
#  http://www.gnu.org/copyleft/gpl.html
################################################################################

# inspired by
# https://github.com/xtranophilist/gnome-shell-extension-cpu-temperature/blob/master/extension.js

TEMP=0

if [ -f /sys/devices/platform/coretemp.0/temp1_input ]; then
  # used with coretemp
  TEMP=`cat /sys/devices/platform/coretemp.0/temp1_input`
elif [ -f /sys/devices/platform/coretemp.0/temp2_input ]; then
  # used with coretemp
  TEMP=`cat /sys/devices/platform/coretemp.0/temp2_input`
elif [ -f /sys/bus/acpi/devices/LNXTHERM\:00/thermal_zone/temp ]; then
  # used on some intel systems
  TEMP=`cat /sys/bus/acpi/devices/LNXTHERM\:00/thermal_zone/temp`
elif [ -f /sys/devices/virtual/thermal/thermal_zone0/temp ]; then
  # used on some intel systems
  TEMP=`cat /sys/devices/virtual/thermal/thermal_zone0/temp`
elif [ -f /sys/class/hwmon/hwmon0/temp1_input ]; then
  # hwmon for new 2.6.39, 3.0 linux kernels
  TEMP=`cat /sys/class/hwmon/hwmon0/temp1_input`
elif [ -f /sys/class/hwmon/hwmon0/device/temp1_input ]; then
  # used on AMD systems
  TEMP=`cat /sys/class/hwmon/hwmon0/device/temp1_input`
elif [ -f /sys/class/hwmon/hwmon0/device/temp2_input ]; then
  # used on ION systems
  TEMP=`cat /sys/class/hwmon/hwmon0/device/temp2_input`
elif [ -f /sys/class/thermal/thermal_zone0/temp ]; then
  # used on RaspberryPi
  TEMP=`cat /sys/class/thermal/thermal_zone0/temp`
fi

echo "$(( $TEMP / 1000 )) C"
