#!/bin/sh

######################################################################
# @author      : swytch
# @file        : AC_notify
# @license     : GPLv3
# @created     : Wednesday May 20, 2020 17:51:45 CEST
#
# @description : send a notification when AC status change
#                refresh statusbar
######################################################################


new_state="$(cat /sys/class/power_supply/AC/online)"
prev_state="$new_state"

notify() { \
	# $1 is $new_state
	if [ "$1" = 1 ]; then
		notify-send -u "normal" "AC plugged in - CHARGING"
	else
		notify-send -u "critical" "AC unplugged ! - DISCHARGING"
	fi
	}

update(){ \
	new_state="$(cat /sys/class/power_supply/AC/online)"
	if [ "$prev_state" != "$new_state" ]; then
		notify "$new_state" && refbar
	fi
	prev_state="$new_state"
	}

while :; do
	update
	# Sleep for 5s seconds before checking again
	sleep 5 &
	wait
done