#!/bin/sh

remind() {
	notify-send -e -i "$icon_dir/battery-010.svg" -h "string:x-canonical-private-synchronous:${0##*/}" "$1 % Battery $2, Please Plugin Charger!!" -u "$2"
}

print_sleep() {
	[ -n "$4" ] && printf "Status: %s, Charge: %s,Next Check in %s seconds\n" "$3" "$2" "$1"
	sleep "$1"
}

#declaration
battery="BAT0" #change accordingly
dir="/sys/class/power_supply/$battery/"
icon_dir="/usr/share/icons/Papirus/22x22/panel/" #change accordingly
i=0
status="normal"

#infinite loop
while charge=$(cat "$dir"/capacity); do
	if [ "$(cat "$dir"/status)" = "Charging" ]; then
		i=300 #default polling time, it's 5 minutes
	else
		case $charge in
			[2-9][0-9] | 100)
				i=300 # 5 minutes
				status="normal"
				;;
			[1-2][0-9])
				i=150 # 2.5 minutes
				status="low"
				;;
			[5-9])
				i=60 # every minute
				status="critical"
				;;
			[0-4])
				i=10 # every 10 seconds
				status="critical"
				;;
		esac

		# remind appropriately
		[ "$status" != "normal" ] && remind "$charge" "$status"
	fi
	print_sleep "$i" "$charge" "$status" "$1" #pass 'log' as argument to print logs
done
