#!/bin/sh
# brightness up|down [STEP]: written by Wren Linux parts/brightnessctl.sh
# Changes screen brightness by STEP percent (default 5), never below 5%,
# then tells sway-status to redraw the bar.
step=${2:-5}
cur=$(brightnessctl -m | cut -d, -f4 | tr -d %)
case $1 in
    up) new=$((cur + step)); [ "$new" -le 100 ] || new=100 ;;
    down) new=$((cur - step)); [ "$new" -ge 5 ] || new=5 ;;
    *) echo "usage: brightness up|down [STEP]" >&2; exit 2 ;;
esac
brightnessctl -q set "$new%"
# Only signal it if that PID really is sway-status (a stale file could
# point at an unrelated process, which SIGUSR1 would kill)
pid=$(cat "${XDG_RUNTIME_DIR:-/tmp}/sway-status.pid" 2>/dev/null)
if [ -n "$pid" ] && tr '\0' ' ' <"/proc/$pid/cmdline" 2>/dev/null | grep -q sway-status; then
    kill -USR1 "$pid" 2>/dev/null
fi
exit 0
