#!/usr/bin/env bash
# Toggle wlsunset.  It runs as the transient user unit eww-nightlight, so it
# survives eww restarts and can be stopped by name.
unit=eww-nightlight

active() { systemctl --user is-active --quiet "$unit" || pgrep -x wlsunset >/dev/null; }

case ${1:-} in
  status) active && echo true || echo false ;;
  *)
    if active; then
      systemctl --user stop "$unit" 2>/dev/null
      pkill -x wlsunset
      state=false
    else
      detach -u "$unit" wlsunset -T 4500 -t 3200
      state=true
    fi
    eww update night-light="$state" 2>/dev/null
    echo "$state"
    ;;
esac
