#!/bin/bash

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

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

#
# CONFIGURATION SECTION BEGINS HERE
#

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

Low_Battery="5"

#
# CONFIGURATION SECTION ENDS HERE
#

datafile="$HOME/.wminfo/conky.tmp"
label="THINKPAD"

if [ "`grep 'temperature_unit fahrenheit' $HOME/.wminfo/conky.conf`" != "" ]
then
    TempUnit="F"
else
    TempUnit=" C"
fi

if [ -z "$(grep "^${label}: " "${datafile}")" ]
then
cat <<EOF
$0: ${label} entries in conky.conf are commented.
         
         
         
         
EOF
exit
fi

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

grep "^${label}: " $datafile | \
    sed -E "s/${label}: //;s/h /:/;s/m .*//;s/(.) (.*) (.*)/\2\1\3/;s/%D/%+/;s/%C/%-/;s/^N $/0% --:--/;s/^F $/100%/;s/^E $/0% --:--/;s/^U $/?/;s/^([0-9])%/\1% /;s/([+-])([0-9]):/\10\2:/;s/:([0-9])$/:0\1/;s/#/!/g;s/_/./g;s/CPU: ([0-9]+)/CPU: \1$TempUnit/;s/GPU: ([0-9]+)/GPU: \1$TempUnit/;s/HDD: ([0-9]+)/HDD: \1$TempUnit/"

value=`grep "^${label}: D .%" $datafile | awk '{print $3}'`
value=${value%\%}
value=${value:-100}

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

if [ "$Suspend" == "on" ]
then
    if [ $value -le $Low_Battery ] && [ $value -ne 0 ]
    then
	sudo `which suspend`
	sleep 60
    fi
fi

echo
