#!/bin/bash
set -euo pipefail

ac_conn=0
for ac in $(find /sys/class/power_supply -type l -name 'A*'); do
	[ $(cat "${ac}/online") -eq 1 ] && ac_conn=1
done

bat_cap=()
for bat in $(find /sys/class/power_supply -type l -name 'B*'); do
	color='#ffffff'
	cap=$(cat "${bat}/capacity")

	[ ${cap} -lt 50 ] && color='#ffd966'
	[ ${cap} -lt 25 ] && color='#dd0000'

	bat_cap+=("<span color=\"${color}\">${cap}%</span>")
done

if [ ${ac_conn} -eq 0 ]; then
	printf '\uf58d %s' "${bat_cap[*]}"
else
	printf '\uf740 %s' "${bat_cap[*]}"
fi