#!/bin/bash

# omarchy:summary=List webcam devices that support video capture
# omarchy:hidden=true

capture_capable() {
  local device="$1"

  v4l2-ctl --device "$device" --info 2>/dev/null | awk '
    /^[[:space:]]*Device Caps[[:space:]]*:/ { inspect = 1; next }
    inspect && /^[[:space:]]*Video Capture/ { found = 1 }
    END { exit !found }
  '
}

name=""
emitted=0

while IFS= read -r line; do
  if [[ -n $line && $line != [[:space:]]* ]]; then
    name="$line"
    emitted=0
  elif (( ! emitted )); then
    device="${line#"${line%%[![:space:]]*}"}"

    if [[ $device == /dev/video* ]] && capture_capable "$device"; then
      emitted=1
      printf '%s  %s\n' "$device" "$name"
    fi
  fi
done < <(v4l2-ctl --list-devices 2>/dev/null)

exit 0
