#!/bin/bash

# Keys: XF86MonBrightnessUp, XF86MonBrightnessDown

class=intel_backlight
dir=/sys/class/backlight/$class
cur=$(cat $dir/brightness)
max=$(cat $dir/max_brightness)

step=$(("$2" + 0))
cur_pct=$((100 * $cur / $max))

case "$1" in
	"up")
		echo "up $2%"
		target_pct=$(($cur_pct + $2))
		;;
	"down")
		echo "down $2%"
		target_pct=$(($cur_pct - $2))
		;;
	*)
		echo "Backlight at $cur/$max ($cur_pct%)"
		exit 0
esac

if [ "$target_pct" -lt "0" ]; then
	target_pct=0
fi
if [ "$target_pct" -gt "100" ]; then
	target_pct=100
fi

target_val=$(($max * $target_pct / 100))

echo "Backlight at $cur/$max ($cur_pct%)"
echo "Target $target_val ($target_pct%)"
echo "Dest $dir/brightness"
echo $target_val > $dir/brightness