#!/bin/sh
# kick-pi -- Raspberry Pi session entry for kick.
#
# A labwc-pi analogue for running kick as the Pi's compositor instead of
# labwc: sets up the Pi OS session context, then execs kick, passing all
# arguments through untouched (screen mode, --panel, tabs, ...).
# Environment (KICK_LOG, ...) passes through equally untouched, except
# KICK_MODE below.
#
# Per-compositor GTK window controls (close-only under kick, full under
# labwc) live in kick itself now: it builds a shadow config home plus a
# dconf overlay for its own children, so this wrapper carries none of that
# machinery.  (A wrapper-level copy would only duplicate it; version mixes
# still behave: new wrapper + old kick simply scopes nothing.)
#
# Deliberately NOT done here (kept minimal, by choice):
#   - logging (see kick-session for per-boot logs),
#   - labwc autostart files (kick-only: panels and apps come from kick's
#     own arguments, e.g. --panel),
#   - XDG_CURRENT_DESKTOP (the display manager sets it from the session
#     .desktop file's DesktopNames entry),
#   - GDK_BACKEND forcing (toolkits self-select; see the kick README).
#
# Note: the D-Bus/systemd activation environment is refreshed by kick
# itself once its socket is listening (standalone mode with a session bus,
# mirroring labwc); it cannot live here because kick generates its socket
# name at startup, so there is nothing correct to export yet.
#
# Usage (see kick-session.desktop for the lightdm entry pattern):
#   env KICK_LOG=1 kick-pi [kick args...]
set -u

# Pi OS session environment (XDG_DATA_DIRS with raspi-ui-overrides, ...),
# the same file labwc-pi sources before execing labwc.
if [ -f /usr/bin/setup_env ]; then
    . /usr/bin/setup_env
fi

# A runtime dir with strict perms; display managers normally provide it.
if [ -z "${XDG_RUNTIME_DIR:-}" ]; then
    XDG_RUNTIME_DIR="/run/user/$(id -u 2>/dev/null || echo 0)"
    export XDG_RUNTIME_DIR
fi
if [ -d "$XDG_RUNTIME_DIR" ]; then
    chmod 700 "$XDG_RUNTIME_DIR" 2>/dev/null || true
else
    mkdir -p "$XDG_RUNTIME_DIR" 2>/dev/null && \
        chmod 700 "$XDG_RUNTIME_DIR" 2>/dev/null || true
fi

# Software rendering on Pi models whose GPU lacks an MMU (same guard as
# labwc-pi; kick's wlroots renderer autocreate honours WLR_RENDERER).
if command -v raspi-config >/dev/null 2>&1; then
    if raspi-config nonint is_pi && ! raspi-config nonint gpu_has_mmu; then
        export WLR_RENDERER=pixman
    fi
fi

# The panel and other clients need the login-provided session bus; starting
# a private one here would split the session, so only warn when absent
# (bus-less D-Bus calls block into timeouts and look like slow startup).
if [ -z "${DBUS_SESSION_BUS_ADDRESS:-}" ]; then
    echo "kick-pi: no D-Bus session bus; applets needing one may be slow" >&2
fi

# kick-pi is a session entry, so standalone is the default: without this,
# a display-manager login (whose stdin is no virtual console, so kick's own
# console autodetection cannot fire) would try to nest into a nonexistent
# host compositor and drop straight back to the login menu.  An explicit
# KICK_MODE (including nested) always wins; WLR_BACKENDS already forces
# standalone inside kick itself.
if [ -z "${KICK_MODE:-}" ] && [ -z "${WLR_BACKENDS:-}" ]; then
    KICK_MODE=standalone
    export KICK_MODE
fi

if command -v kick >/dev/null 2>&1; then
    KICK_BIN=kick
else
    echo "kick-pi: 'kick' not found in PATH=$PATH" >&2
    exit 127
fi
exec "$KICK_BIN" "$@"
