#!/bin/zsh

screen="HDMI-1"
dir_icons="/usr/local/bin/icons"

current_bright_float=$(xrandr --verbose | grep "$screen" -A10000 | grep 'Brightness:' | sed 's/[^0-9,.]//g')
current_bright_int=$(echo $((current_bright_float*100 + 0.5)) | sed "s/\..*//" ) # cast to int

if [[ "$1" == "up" || "$1" == "+" ]]; then
new_bright_int=$(($current_bright_int + 1))
fi
if [[ "$1" == "down" || "$1" == "-" ]]; then
new_bright_int=$(($current_bright_int - 1))
fi

if [[ "${#new_bright_int}" -eq 2 ]]; then # cast to float, two-digit number 
new_bright_float=$((0.$new_bright_int))
fi
if [[ "${#new_bright_int}" -eq 1 ]]; then # cast to float, one-digit number 
new_bright_float=$((0.0$new_bright_int))
fi

if [[ "$new_bright_float" -lt "0" ]]; then # brightness must be positive
exit
fi

xrandr --output $screen --brightness $new_bright_float

# send notification
dunstify -a "Screen" \
    "Screen ($screen)" \
    "Brightness: $new_bright_int%" \
    -r 100 \
    -i "$dir_icons"/sun.svg