#!/bin/dash
FONT="10x20"
FLAG=/tmp/batt-low

# no warning needed if AC adapter is attached
grep -q on-line /proc/acpi/ac_adapter/*/state && exit # exit if ac adapter is plugged already
grep -q 1 /sys/class/power_supply/*/online && exit # alternative check for ac adapter

# type of message
case $1 in
	critical)
		TYPE=critical
		MG=50 BG=red FG=white TEXT="Battery is critical, shutdown imminent.\nAttach charger now."
		;;
	low|*)
		TYPE=low
		MG=30 BG=yellow FG=black TEXT="Battery is low.\nPlease attach charger."
		;;		
esac

# only show the message once
FLAG=$FLAG-$TYPE
[ -f $FLAG ] && exit	
> $FLAG

# display
if type yaf-splash > /dev/null; then
	# preferred UI
	TEXT=$(printf "$TEXT\n\nClick here to remove this message.")
	yaf-splash -font "$FONT" -outline 0 -margin $MG -bg $BG -fg $FG -text "$TEXT"
else
	# alternative UI
	Xdialog --title "Battery Low" --msgbox "$(printf "$TEXT")" 0 0
fi

# cleanup
rm -f $FLAG
