#!/bin/sh

# for notify-send
export DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/$(id -u)/bus

fmt='%d%%\n'
treshold=10
bat=/sys/class/power_supply/BAT0

usage() {
	echo "usage: $(basename $0) [-hcnst]" >&2
}

c=$(cat $bat/capacity)
s=$(cat $bat/status)
v=$c

while getopts "hcstn" OPT; do
	case $OPT in
	c) fmt='%d\n'; v=$c ;; # capacity
	s) fmt='%s\n'; v=$s ;; # charging status
	t) test "$s" = "Charging" -o "$c" -lt "$treshold"; exit $? ;;
	n) [ "$s" != "Charging" -a "$c" -lt "$treshold" ] \
		&& notify-send -u critical "🪫 low battery ($c%)"
		exit ;;
	h) usage; exit 0 ;;
	*) usage; exit 1 ;;
	esac
done
shift $((OPTIND-1))

printf "$fmt" "$v"
