#!/bin/bash

# wminfo plugin: ACPI status of IBM/Lenovo ThinkPad machines

# command: wminfo -p thinkpad.wmi -u 5 -b#000000 -f#00ff00

#
# CONFIGURATION SECTION BEGINS HERE
#

Temperature="C"		# "C" for Celsius
			# "F" for Fahrenheit

Suspend="off"		# "on" or "off" (register the suspend script in /etc/sudoers)

Low_Battery="5"

#
# CONFIGURATION SECTION ENDS HERE
#

if [ "`echo $* | grep '\-C'`" != "" ]
then
    Temperature="C"
elif [ "`echo $* | grep '\-F'`" != "" ]
then
    Temperature="F"
fi

# Determining basic values

if [ ! -e /proc/acpi/ibm ]
then
    echo "thinkpad.wmi: it isn't IBM ThinkPad."
    exit
fi

BATTERY_STATE=`cat /proc/acpi/battery/BAT0/state | grep 'present:' | awk '{print $2}'`

if [ "$BATTERY_STATE" == "yes" ]
then
    BATTERY_FULL=`cat /proc/acpi/battery/BAT0/info | grep 'last full capacity:' | awk '{print $4}'`
    BATTERY_REMAINING=`cat /proc/acpi/battery/BAT0/state | grep 'remaining capacity:' | awk '{print $3}'`
    BATTERY_PERCENTAGE=`expr 100 \* $BATTERY_REMAINING / $BATTERY_FULL`
elif [ "$BATTERY_STATE" == "no" ]
then
    BATTERY_PERCENTAGE="0"
else
    echo "thinkpad.wmi: there is no working ACPI."
    exit
fi

POWER=`cat /proc/acpi/ac_adapter/AC/state | grep 'state:' | awk '{print $2}'`
BATTERY=`cat /proc/acpi/battery/BAT0/state | grep 'present:' | awk '{print $2}'`

if [ "$POWER" == "on-line" ] && [ "$BATTERY" == "no" ]
then
    POWER_SUPPLY="PWR"
elif [ "$POWER" == "off-line" ] && [ "$BATTERY" == "yes" ]
then
    POWER_SUPPLY="BAT"
elif [ "$POWER" == "on-line" ] && [ "$BATTERY" == "yes" ]
then
    POWER_SUPPLY="PWR & BAT"
fi

# Power on-line and absent battery

if [ "$POWER_SUPPLY" == "PWR" ]
then
    TIME=" --:--"
fi

# Power off-line and present battery

if [ "$POWER_SUPPLY" == "BAT" ]
then
    CURRENT_TIME=`date +"%s"`
    if [ "$FIRST_TIME" == "" ]; then FIRST_TIME=0 ; fi
    let "ELAPSED_TIME=($CURRENT_TIME-$FIRST_TIME)/60"
    PRESENT_RATE=`cat /proc/acpi/battery/BAT0/state | grep 'present rate:' | awk '{print $3}'`
    REMAINING_CAPACITY=`cat /proc/acpi/battery/BAT0/state | grep 'remaining capacity:' | awk '{print $3}'`
    if [ $HOURS -lt 10 ]
    then
	HOURS="0$HOURS"
    fi
    if [ $MINUTES -lt 10 ]
    then
	MINUTES="0$MINUTES"
    fi
    TIME="+$HOURS:$MINUTES"
fi

# Power on-line and present battery

if [ "$POWER_SUPPLY" == "PWR & BAT" ]
then
    CURRENT_TIME=`date +"%s"`
    if [ "$FIRST_TIME" == "" ]; then FIRST_TIME=0 ; fi
    if [ "$FIRST_PERCENTAGE" == "" ]; then FIRST_PERCENTAGE=0 ; fi
    let "ELAPSED_TIME=($CURRENT_TIME-$FIRST_TIME)/60"
    let "GAINED_BATTERY=$BATTERY_PERCENTAGE-$FIRST_PERCENTAGE"
    let "REMAINING_BATTERY=100-$BATTERY_PERCENTAGE"
    if [ "$LOADING_TIME" == "" ]; then LOADING_TIME=0 ; fi
    let "HOURS=$LOADING_TIME/60"
    let "MINUTES=$LOADING_TIME-60*$HOURS"
    CHARGING_STATE=`cat /proc/acpi/battery/BAT0/state | grep 'charging state:' | awk '{print $3}'`
    if [ $HOURS -lt 10 ]
    then
	HOURS="0$HOURS"
    fi
    if [ $MINUTES -lt 10 ]
    then
	MINUTES="0$MINUTES"
    fi
    if [ "$ELAPSED_TIME" == "" ] || [ "$GAINED_BATTERY" == "" ] || [ $ELAPSED_TIME -eq 0 ] || [ $GAINED_BATTERY -eq 0 ]
    then
	TIME=""
    elif [ "$CHARGING_STATE" == "charging" ]
    then
	TIME="-$HOURS:$MINUTES"
    elif [ "$CHARGING_STATE" == "charged" ]
    then
	TIME="00:00"
    fi
fi

# Displaying the results

if [ `echo "$BATTERY_PERCENTAGE%$TIME" | wc -m` -lt 10 ]
then
    echo "$BATTERY_PERCENTAGE% $TIME"
elif [ `echo "$BATTERY_PERCENTAGE%$TIME" | wc -m` -gt 10 ]
then
    echo "$BATTERY_PERCENTAGE$TIME"
else
    echo "$BATTERY_PERCENTAGE%$TIME"
fi

let "count=$BATTERY_PERCENTAGE/10"
for n in `seq 1 $count`
do
    GRAPH="$GRAPH!"
done
let "count=9-$count"
for n in `seq 1 $count`
do
    GRAPH="$GRAPH."
done

echo "$GRAPH"
# echo "$POWER_SUPPLY"

if [ -d /proc/acpi/ibm ]
then
    CPU_TEMP=`cat /proc/acpi/ibm/thermal | awk '{print $2}'`
    GPU_TEMP=`cat /proc/acpi/ibm/thermal | awk '{print $5}'`
    if [ "$GPU_TEMP" == "-128" ]
    then
	GPU_TEMP=`cat /proc/acpi/ibm/thermal | awk '{print $10}'`
    fi
    if [ "$Temperature" == "C" ]
    then
	echo "CPU: $CPU_TEMP C"
	echo "GPU: $GPU_TEMP C"
    elif [ "$Temperature" == "F" ]
    then
	let "CPU_TEMP=9*$CPU_TEMP/5+32"
	let "GPU_TEMP=9*$GPU_TEMP/5+32"
	if [ $CPU_TEMP -lt 100 ]
	then
	    CPU_TEMP=" $CPU_TEMP"
	fi
	if [ $GPU_TEMP -lt 100 ]
	then
	    GPU_TEMP=" $GPU_TEMP"
	fi
	echo "CPU: ${CPU_TEMP}F"
	echo "GPU: ${GPU_TEMP}F"
    fi
    FAN_SPEED=`cat /proc/acpi/ibm/fan | grep 'speed:' | awk '{print $2}'`
    if [ "$FAN_SPEED" == "0" ]
    then
        FAN_SPEED="0"
    fi
    echo "FAN: $FAN_SPEED"
fi

# Attention: to use suspend feature register suspend script in /etc/sudoers file

if [ "$Suspend" == "on" ]
then
    if [ $BATTERY_PERCENTAGE -le $Low_Battery ] && [ $BATTERY_PERCENTAGE -gt 0 ] && [ "`echo $POWER_SUPPLY | grep 'PWR'`" == "" ]
    then
	sudo `which suspend`
	sleep 60
    fi
fi

