#!/usr/bin/zsh

MAX_BATTERY=85;
MIN_BATTERY=20;
SLEEP_TIME=10;

function battery(){ spd-say $1 -r -10 -i -80; }

function notify(){ notify-send "Battery alert" $1; }

function battery_health(){
  while true; do
    BATTERY_LVL=$(acpi -b | cut -d"," -f2 | grep -Eo '[0-9]*');
    CHARGING=$(acpi -b | cut -d" " -f7);
    
    if (( $BATTERY_LVL > $MAX_BATTERY )) && [[ $CHARGING = "charged" ]]; then
        notify "Unplug you AC adapter" && battery "Unplug charger";
    elif (( $BATTERY_LVL < $MIN_BATTERY )) && [[ $CHARGING != "charged" ]]; then
        notify "Plug you AC adapter" && battery "Plug charger";
    fi
    
    sleep $SLEEP_TIME;
  done
}

battery_health
