#!/bin/sh

if [ $# -lt 2 ]; then
	message="beep beep - low battery"
else
	message=$2
fi

notify() {
	notify-send "Low battery" "Please plug in computer" -u critical -t 15000
	echo $message | espeak
}

while [ 1 ]; do
	acstat=$(acpi | awk '{print $3}' | rev | cut -c 2- | rev)
	batlvl=$(acpi | awk '{print $4}' | rev | cut -c 3- | rev)
	if [ $batlvl -lt 15 ]; then
		while [ "$acstat" = "Discharging" ]; do
			$(notify)
			sleep 20
			acstat=$(acpi | awk '{print $3}' | rev | cut -c 2- | rev)
			batlvl=$(acpi | awk '{print $4}' | rev | cut -c 3- | rev)
			if [ "$acstat" = "Charging" ]; then
				break
			fi
		done
	fi
	sleep 240
done