#!/bin/sh

# script to decrease the display brightness by $1%

[ -z "$1" ] && echo "Error: please give percent as argument" && exit 1

#xbacklight -dec "$1"
#exit

# get brightness device
device=$(brightnessctl -l | grep "class 'backlight'" | tail -n +2 | awk '{print $2}' | tr -d "'")
if brightnessctl -l | grep "class 'backlight'" | grep -q "^Device 'intel_backlight'\|^Device 'acpi_video"; then
    device="$(brightnessctl -l | grep "class 'backlight'" | awk '{print $2}' | tr -d "'")"
fi

# get brightness percentage
brightness=$(brightnessctl -d "${device}" | grep Current | awk -F '[)(]' "{print \$2 - $1}")

# correct negative values to 0
echo $brightness | grep -q '^-' && brightness=0

# set brightness
brightnessctl -d "${device}" set "$brightness"%
