#!/bin/sh
msgTag="brightness" # Arbitrary but unique message tag
highlight="#7aa2f7"

round() {
    awk "BEGIN {printf \"%.0f\", $*}"
}

oldbrightness="$(cat /sys/class/backlight/*/brightness)"
oldpercent="$(round "$oldbrightness * (100/255)")"
newpercent="$(round "$oldpercent + $1")"
newbrightness="$(round "$newpercent * (255/100)")"

if [ "$newpercent" -gt "100" ]; then
    icon="gpm-brightness-lcd"
    newpercent=100
    newbrightness=255
elif [ "$newpercent" -gt "50" ]; then
    icon="gpm-brightness-lcd"
elif [ "$newpercent" -gt "0" ]; then
    icon="gpm-brightness-lcd-disabled"
else
    icon="gpm-brightness-lcd-disabled"
    newpercent=0
    newbrightness=0
fi

# shellcheck disable=2086
round $newbrightness | sudo tee /sys/class/backlight/*/brightness

notify-send -a "changeBrightness" -i "$icon" -u low "Brightness: ${newpercent}%" \
    -h "string:x-dunst-stack-tag:$msgTag" \
    -h "int:value:$newpercent" \
    -h "string:hlcolor:$highlight"