#!/bin/sh
. "$HOME/.local/libexec/statusbar/colors"

[ -z "$(ls /sys/class/power_supply)" ] && exit 0

for battery in /sys/class/power_supply/BAT?*; do
	pidof -x sbd-battery >/dev/null 2>&1 || "$HOME/.local/libexec/daemons/sbd-battery" "$battery" >/dev/null 2>&1 &
	[ -n "${capacity+x}" ] && printf " "
	capacity="$(cat "$battery/capacity" 2>&1)"
	case "$(cat "$battery/status" 2>&1)" in
	"Full")
		icon="󰁹"
		color="$fggreen"
		;;
	"Not charging")
		icon="󰁹"
		color="$fgwhite"
		;;
	"Unknown")
		icon="󰂑"
		color="$fgorange"
		;;
	"Charging")
		icon="󰂄"
		color="$fgcyan"
		;;
	"Discharging")
		if [ "$capacity" -le 10 ]; then
			icon="󰂎"
			color="$fgred"
		elif [ "$capacity" -le 20 ]; then
			icon="󰁺"
			color="$fgred"
		elif [ "$capacity" -le 30 ]; then
			icon="󰁻"
			color="$fgwhite"
		elif [ "$capacity" -le 40 ]; then
			icon="󰁼"
			color="$fgwhite"
		elif [ "$capacity" -le 50 ]; then
			icon="󰁽"
			color="$fgwhite"
		elif [ "$capacity" -le 60 ]; then
			icon="󰁾"
			color="$fgwhite"
		elif [ "$capacity" -le 70 ]; then
			icon="󰁿"
			color="$fgwhite"
		elif [ "$capacity" -le 80 ]; then
			icon="󰂀"
			color="$fgwhite"
		elif [ "$capacity" -le 90 ]; then
			icon="󰂁"
			color="$fgwhite"
		elif [ "$capacity" -le 100 ]; then
			icon="󰂂"
			color="$fgwhite"
		else
			icon="󰁹"
			color="$fgwhite"
		fi
		;;
	*) exit 1 ;;
	esac
	printf "%b" "<span $color>$icon</span> $capacity"
done && printf "\\n"