#!/bin/sh -e
#
#    battery: print the state of the battery
#
#    Copyright (C) 2009 Raphaël Pinson.
#    Copyright (C) 2011 Dustin Kirkland
#
#    Authors: Raphaël Pinson <raphink@ubuntu.com>
#             Dustin Kirkland <kirkland@ubuntu.com>
#
#    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, version 3 of the License.
#
#    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 this program.  If not, see <http://www.gnu.org/licenses/>.

__battery_detail() {
	local bat
	for bat in /proc/acpi/battery/*; do
		cat "$bat/info"
		cat "$bat/state"
	done
}

__battery() {
	local bat line present sign state percent full rem color bcolor
	for bat in $BATTERY /proc/acpi/battery/*; do
		[ -f "$bat/info" ] || continue
		present=""; full=""; rem=""; state=""
		while read line; do
			set -- ${line}
			case "$line" in
				present:*)
					# make sure that this battery is present
					[ "$2" = "no" ] && continue 2
					present="$2";;
		   	last\ full\ capacity:*) full="$4";;
			esac
			[ -n "$present" -a -n "$full" ] && break
		done < "${bat}/info"
		while read line; do
			set -- ${line}
			case "$line" in
				remaining\ capacity:*) rem="$3";;
				charging\ state:*) state="$3";;
			esac
			[ -n "$rem" -a -n "$state" ] && break
		done < "$bat/state"
		percent=$(((100*$rem)/$full))
		if [ "$percent" -lt 33 ]; then
			color="R k"
			bcolor="b R k"
		elif [ "$percent" -lt 67 ]; then
			color="Y k"
			bcolor="b Y k"
		else
			color="G k"
			bcolor="b G k"
		fi
		percent="$percent$PCT"
		case $state in
			charging) sign="+" ;;
			discharging) sign="-" ;;
			charged) sign="="; percent="" ;;
			*) sign="$state" ;;
		esac
		color $bcolor; printf "%s" "$percent"; color -; color $color; printf "|%s|" "$sign"; color --
		break
	done
}

# vi: syntax=sh ts=4 noexpandtab
