#!/bin/sh
# Start the Tres session using isolated user configuration.
set -eu
config="${XDG_CONFIG_HOME:-$HOME/.config}/tres/labwc"
legacy="$HOME/.config/tres-labwc"
if [ ! -e "${XDG_CONFIG_HOME:-$HOME/.config}/tres" ] && [ -d "$legacy" ]; then
    /usr/bin/env bash /usr/share/tres/migrate-legacy.sh
fi
mkdir -p "$config"
portal_config="${XDG_CONFIG_HOME:-$HOME/.config}/xdg-desktop-portal/tres-portals.conf"
if [ ! -e "$portal_config" ]; then
    mkdir -p "$(dirname "$portal_config")"
    cp /usr/share/tres/tres-portals.conf "$portal_config"
fi
if [ ! -e "$config/autostart" ]; then
    cp /usr/share/tres/autostart "$config/autostart"
    chmod 0755 "$config/autostart"
fi
# Do not overwrite a customized desktop right-click menu.
if [ ! -e "$config/menu.xml" ]; then
    cp /usr/share/tres/menu.xml "$config/menu.xml"
    chmod 0644 "$config/menu.xml"
fi
# Upgrade only the original default wallpaper invocation, preserving other
# autostart edits (especially an existing HiDPI scale block). Leave custom
# wallpaper commands untouched.
if [ -f "$config/autostart" ] &&
   grep -Fq "swaybg -c '#141414'" "$config/autostart" &&
   ! grep -Fq 'tres-wallpaper' "$config/autostart"; then
    cp -p "$config/autostart" "$config/autostart.before-tres-wallpaper"
    sed -i "s|swaybg -c '#141414'|/usr/bin/tres-wallpaper|g" "$config/autostart"
fi
# Upgrade recognizable Tres defaults, keeping other autostart content intact.
# All changes are scoped to this desktop's private Labwc config.
 /usr/bin/python3 - "$config" <<'PY'
from pathlib import Path
import sys

directory = Path(sys.argv[1])
autostart = directory / "autostart"
if autostart.is_file() and not autostart.is_symlink():
    old = autostart.read_text(encoding="utf-8")
    updated = old.replace(
        '/usr/bin/tres-panel >> "$HOME/.cache/tres/panel.log" 2>&1 &',
        '/usr/bin/tres-panel-supervisor >/dev/null 2>&1 &',
    )
    if "tres-panel" in updated and "dbus-update-activation-environment" not in updated:
        block = (
            'if command -v dbus-update-activation-environment >/dev/null 2>&1; then\n'
            '    dbus-update-activation-environment --systemd WAYLAND_DISPLAY '
            'XDG_CURRENT_DESKTOP XDG_SESSION_DESKTOP >/dev/null 2>&1 || true\n'
            'fi\n'
        )
        if updated.startswith("#!"):
            pos = updated.find("\n") + 1
            updated = updated[:pos] + block + updated[pos:]
    # Add authentication to older package-managed Tres autostarts without
    # replacing user-added commands, wallpaper choices, or output scaling.
    if "tres-polkit-agent" not in updated:
        updated += (
            '\n# Tres graphical administrator authentication\n'
            '/bin/sh /usr/lib/tres/tres-polkit-agent.sh '
            '>> "$HOME/.cache/tres/polkit.log" 2>&1 &\n'
        )
    if updated != old:
        backup = autostart.with_name("autostart.before-tres-integration")
        if not backup.exists():
            backup.write_text(old, encoding="utf-8")
        autostart.write_text(updated, encoding="utf-8")

sys.path.insert(0, "/usr/share/tres")
from tres_menu import add_restart_action
add_restart_action(directory / "menu.xml", "/usr/bin/tres-panel-control restart")
PY
# Set up the optional search binding once for existing packaged installations.
TRES_MODULE_DIR=/usr/share/tres /bin/bash /usr/lib/tres/install-search-shortcut.sh
/bin/bash /usr/lib/tres/restore-panel-mouse.sh
exec /usr/lib/tres/tres-session.sh "$@"
