#!/bin/bash

PERCENTAGE="$(pmset -g batt | grep -Eo "\d+%" | cut -d% -f1)"
CHARGING="$(pmset -g batt | grep 'AC Power')"

if [ "$PERCENTAGE" = "" ]; then
  exit 0
fi

case "${PERCENTAGE}" in
  9[0-9]|100) 
    ICON=""
    COLOR="0xFFA6DA95"
  ;;
  [6-8][0-9]) 
    ICON=""
    COLOR="0xFFEED49F"
  ;;
  [3-5][0-9]) 
    ICON=""
    COLOR="0xFFF5A97F"
  ;;
  [1-2][0-9]) 
    ICON=""
    COLOR="0xFFED8796"
  ;;
  *) 
    ICON=""
    COLOR="0xFFED8796"
esac

if [[ "$CHARGING" != "" ]]; then
  ICON=""
  COLOR="0xFFA6DA95"
fi

# The item invoking this script (name $NAME) will get its icon and label
# updated with the current battery status
sketchybar --set "$NAME" icon.color="$COLOR" icon="$ICON" label.color="$COLOR" label="${PERCENTAGE}%"
