#!/usr/bin/env bash
# Reports GNOME's light/dark preference for eww.
#   get     print "light" or "dark" once
#   listen  print it now, then again on every change (for deflisten)
set -u
KEY="org.gnome.desktop.interface color-scheme"

emit() {
  case "$(gsettings get $KEY 2>/dev/null)" in
    *dark*) echo dark ;;
    *)      echo light ;;   # 'prefer-light' and 'default' -> light
  esac
}

case "${1:-get}" in
  get) emit ;;
  listen)
    emit
    gsettings monitor $KEY 2>/dev/null | while read -r _; do emit; done
    ;;
esac
