#!/sbin/openrc-run
# Bananarchy cover display daemon. Only active on flip devices; the
# lid-profile file is written by scripts/apply-bananarchy-overlay.sh.

description="Nokia 2720 Flip external cover display"
# The recovered native bananarchy-coverd busy-loops at 100% CPU on the
# spidev backend (gpio110/DC is owned by panel-mipi-dbi-spi) and paints
# nothing. Keep that binary installed as a diagnostic, but run the compiled
# idle IPC server: it drives the external panel through /dev/spidev0.1 only
# while the flip is closed and hands the QUP5 bus back on open. No Python
# runs here.
command="/usr/bin/bananarchy-cover-idle"
command_args="--config /etc/bananarchy/cover-display.conf"
command_background="yes"
pidfile="/run/bananarchy/cover-display.pid"
output_log="/run/bananarchy/cover-display.log"
error_log="/run/bananarchy/cover-display.log"

depend() {
  need udev
  after udev-settle
}

start_pre() {
  mkdir -p /run/bananarchy
  chmod 0755 /run/bananarchy
  # /run is tmpfs: clear stale transition guards after every reboot.
  rm -f /run/bananarchy/cover-backlight \
        /run/bananarchy/cover-bl-force \
        /run/bananarchy/cover-power-state \
        /run/bananarchy/cover-runtime.conf 2>/dev/null || true
}

start() {
  if [ "$(cat /usr/share/bananarchy/lid-profile 2>/dev/null)" != "flip" ]; then
    ebegin "Skipping cover display (not a flip device)"
    eend 0
    return 0
  fi
  default_start
  # Seed the compact network/battery indicators once at boot; later changes
  # arrive from the NetworkManager dispatcher (bananarchy-cover-event.sh).
  /usr/bin/bananarchy-cover-event.sh network >/dev/null 2>&1 &
}
