#!/bin/bash
# Steven Maresca
# lightyear4 @t gmail
#
# Parses Battery information out of /proc/acpi tree
# and returns percentage of battery life available at
# runtime
#
# Requires ACPI of course, and perl
#
# Are there better ways to do this? sure. this simply worked for me.
#
# calculates battery capacity using specs for the design capacity
# (i.e. the capacity it should have when coming new from the factory)
# this a) allows you to keep track of (eventual) battery degradation
# as it ages.  if you wish to use the 'last full capacity' to calculate
# battery life, simply change the assignment of CAPACITY to use "full" 
# intead of "design."
#
# Current version of this script (and others) always available
# at http://www.fugitivethought.com/projects/shell-scripts/
#
# Start battstat script

BATTLEFT=`grep remaining /proc/acpi/battery/BAT0/state`

LEFT=`expr substr "$BATTLEFT" 26 5` 	#value of 9 in place of 5 gets the
					#mWh portion of the string

CAPACITY=`grep design /proc/acpi/battery/BAT0/info`

FULL=`expr substr "$CAPACITY" 26 5`

#echo "FULL: ($FULL), LEFT: ($LEFT)"

RATIOLEFT=`perl -e "print $LEFT / $FULL"`
PCTLEFT=`perl -e "print $RATIOLEFT * 100"`

PCTTRIM=`expr substr "$PCTLEFT" 1 4`

echo $PCTTRIM



# end battstat script
