#!/bin/sh
# Prefer Intel for a hybrid compositor, retaining NVIDIA for connected outputs.
# Device paths are discovered each session; PCI by-path names contain colons,
# which conflict with Aquamarine's colon-separated AQ_DRM_DEVICES format.

integrated_devices=
nvidia_devices=
other_devices=
for device_path in /sys/class/drm/card[0-9]*; do
	device_name=${device_path##*/}
	[ -c "/dev/dri/$device_name" ] || continue
	[ -r "$device_path/device/vendor" ] || continue
	IFS= read -r vendor < "$device_path/device/vendor"
	case "$vendor" in
		0x8086) integrated_devices="${integrated_devices:+$integrated_devices:}/dev/dri/$device_name" ;;
		0x10de) nvidia_devices="${nvidia_devices:+$nvidia_devices:}/dev/dri/$device_name" ;;
		*) other_devices="${other_devices:+$other_devices:}/dev/dri/$device_name" ;;
	esac
done
# Ordinary single-GPU and non-Intel systems retain the compositor's own selection.
if [ -n "$integrated_devices" ] && [ -n "$nvidia_devices" ]; then
	printf '%s\n' "$integrated_devices:$nvidia_devices${other_devices:+:$other_devices}"
fi
