#!/usr/bin/env bash

brightness_file="/sys/class/backlight/intel_backlight/brightness"
min_brightness=1
max_brightness=96000

# Check if an argument is provided
if [[ -z $1 ]]; then
  # echo "Error: Number argument is missing."
  exit 1
fi

# Retrieve the argument value
number_to_add=$1

# Validate if the argument is a number
if ! [[ $number_to_add =~ ^-?[0-9]+$ ]]; then
  # echo "Error: Invalid number argument."
  exit 1
fi

# Read the current brightness value from the file
current_brightness=$(cat "$brightness_file")

# Add the desired number to the current brightness
new_brightness=$((current_brightness + number_to_add))

# Enforce minimum and maximum brightness values
if (( new_brightness < min_brightness )); then
  new_brightness=$min_brightness
elif (( new_brightness > max_brightness )); then
  new_brightness=$max_brightness
fi

# Write the new brightness value back to the file
echo "$new_brightness" >> "$brightness_file"