#!/bin/sh

# Prints all batteries, their percentage remaining and an icon corresponding
# to charge level. Uses Nerd Font battery icons.

case $BLOCK_BUTTON in
	3) notify-send " Battery module" ": discharging
: charging
: charged
- Scroll to adjust xbacklight." ;;
	4) xbacklight -inc 10 ;;
	5) xbacklight -dec 10 ;;
	6) setsid -f "$TERMINAL" -e "$EDITOR" "$0" ;;
esac

# Get battery level icon based on percentage
# full= (f240), 3/4= (f241), half= (f242), quarter= (f243)
# 20%=󰁻 (f007b), 10%=󰁺 (f007a), empty= (f244)
get_battery_icon() {
	if [ "$1" -ge 95 ]; then
		echo ""
	elif [ "$1" -ge 65 ]; then
		echo ""
	elif [ "$1" -ge 40 ]; then
		echo ""
	elif [ "$1" -ge 25 ]; then
		echo ""
	elif [ "$1" -ge 10 ]; then
		echo "󰁻"
	elif [ "$1" -ge 5 ]; then
		echo "󰁺"
	else
		echo ""
	fi
}

# Exit silently if no battery exists (desktop)
[ ! -d /sys/class/power_supply/BAT0 ] && [ ! -d /sys/class/power_supply/BAT1 ] && exit 0

# Loop through all attached batteries and format the info
for battery in /sys/class/power_supply/BAT?*; do
	# If non-first battery, print a space separator.
	[ -n "${capacity+x}" ] && printf " "
	# Sets up the status and capacity
	capacity="$(cat "$battery/capacity" 2>&1)"
	status="$(cat "$battery/status" 2>&1)"

	# Get icon based on battery level
	icon=$(get_battery_icon "$capacity")

	# Add charging indicator
	case "$status" in
		"Charging") charge_indicator=" " ;;
		"Full") charge_indicator=" " ;;
		*) charge_indicator="" ;;
	esac

	# Prints the info
	printf "%s %d%%%s" "$icon" "$capacity" "$charge_indicator"
done && printf "\\n"
