#!/usr/bin/env sh
# Simple script to change the system backlight.  Requires a udev rules that gives the user permission
# to the backlight folder.
# $ change-brightness up   # Increases brightness
# $ change-brightness down # Decreases brightness

kernel="$(ls /sys/class/backlight/ | head -n 1)"
dir="/sys/class/backlight/$kernel"

max_brightness="$(cat $dir/max_brightness)"
brightness="$(cat $dir/brightness)"
increment=12000

if [ "$1" = "down" ]; then
    new_brightness=$(($brightness - $increment))
    if [ $new_brightness -lt 0 ]; then
        new_brightness=0
    fi
else
    new_brightness=$(($brightness + $increment))
    if [ $new_brightness -gt $max_brightness ]; then
        new_brightness=$max_brightness
    fi
fi

echo $new_brightness > $dir/brightness
ratio=$(( (new_brightness * 100) / max_brightness ))
notify-send -r 43 "Brightness: ${ratio}%"

