#!/bin/bash

# omarchy:summary=Print the most likely display backlight device.
# omarchy:examples=omarchy-hw-display

backlight_path="${OMARCHY_BACKLIGHT_PATH:-/sys/class/backlight}"

# Start with the first possible output, then refine to the most likely given an order heuristic.
# Glob the candidates in the loop list: [[ ]] does not do pathname expansion.
# The Touch Bar on T2 Macs registers a backlight that never drives the display panel.
device="$(ls -1 "$backlight_path" 2>/dev/null | grep -vx appletb_backlight | head -n1)"
# gmux comes first: apple-gmux only registers when the kernel has already picked it, and on
# dual-GPU Macs the GPU's own PWM stops driving the panel once that GPU suspends.
for candidate in "$backlight_path"/gmux_backlight "$backlight_path"/amdgpu_bl* "$backlight_path"/intel_backlight "$backlight_path"/acpi_video*; do
  if [[ -e $candidate ]]; then
    device="${candidate##*/}"
    break
  fi
done

if [[ -n $device ]]; then
  printf '%s\n' "$device"
else
  exit 1
fi
