#!/bin/bash

# The Linux-only half of a backup: snapshot the LIVE desktop wiring into the repo
# (reference copy for recovery/diffing; assets/themes/install-theme.sh remains the
# applier on restore) and copy the Claude memory notes in so git commits their
# content. None of it exists on Windows, which is why it isn't in bin/sync.py.
#
# Not meant to be run by hand — it's the `--full` stage of:
#   python bin/sync.py backup --full      (or `ncp backup -f`)

# Set by bin/sync.py to the repo it was invoked from, so a clone in a scratch dir
# operates on itself instead of reaching into the real ~/nexus-core.
NEXUS_ROOT="${NEXUS_ROOT:-$HOME/nexus-core}"
cd "$NEXUS_ROOT" || { echo "No $NEXUS_ROOT"; exit 1; }

snap="$NEXUS_ROOT/assets/themes/restore-snapshot"
mkdir -p "$snap"
{
    # No timestamp here — git records commit time, and a date line makes every
    # `backup full` dirty this file and commit even when nothing changed.
    echo "# NexusOS live desktop wiring — reference snapshot"
    echo "# Reference only. Restore is done by assets/themes/install-theme.sh"
    for p in /Net/ThemeName /Net/IconThemeName /Gtk/CursorThemeName \
             /Gtk/CursorThemeSize /Gtk/FontName; do
        echo "xsettings $p = $(xfconf-query -c xsettings -p "$p" 2>/dev/null)"
    done
    echo "xfwm4 /general/theme = $(xfconf-query -c xfwm4 -p /general/theme 2>/dev/null)"
} > "$snap/xfconf.txt" 2>/dev/null || true
cp -f "$HOME/.config/gtk-3.0/settings.ini" "$snap/gtk-3.0-settings.ini" 2>/dev/null || true
cp -f "$HOME/.config/gtk-4.0/settings.ini" "$snap/gtk-4.0-settings.ini" 2>/dev/null || true
cp -f "$HOME/.config/xfce4/panel/genmon-13.rc" \
      "$NEXUS_ROOT/management/panel/genmon-13.rc" 2>/dev/null || true

# The xfconf channel XMLs ARE the desktop: panel layout/size/colour, the
# wallpaper, compositing + keybindings, terminal profile. Copying the files
# is the whole restore — no per-property xfconf-query scripting needed.
# displays.xml is deliberately excluded (monitor-specific; would break another box).
xml_src="$HOME/.config/xfce4/xfconf/xfce-perchannel-xml"
mkdir -p "$snap/xfconf-xml"
for c in xfce4-panel xfce4-desktop xfwm4 xsettings xfce4-keyboard-shortcuts xfce4-terminal; do
    cp -f "$xml_src/$c.xml" "$snap/xfconf-xml/$c.xml" 2>/dev/null || true
done

# Multi-monitor primary-follow watcher lives in ~/.local/bin, not the repo.
cp -f "$HOME/.local/bin/plank-primary-watch.sh" \
      "$NEXUS_ROOT/bin/panel/plank-primary-watch.sh" 2>/dev/null || true
# Trailing /. copies the CONTENTS — plain `cp -r src dst` nests into dst/src
# once dst exists, burying the dock one level deeper on every backup.
rm -rf "$snap/plank"
if [ -d "$HOME/.config/plank" ]; then
    mkdir -p "$snap/plank"
    cp -rf "$HOME/.config/plank/." "$snap/plank/" 2>/dev/null || true
fi

# Copy the Claude memory notes (machine knowledge under ~/.claude) into the
# repo so they're versioned too. Some aren't Nexus-specific (Fusion 360, etc.).
notes_src="$HOME/.claude/projects/-home-jon-nexus-core/memory"
notes_dst="$NEXUS_ROOT/assets/notes"
mkdir -p "$notes_dst"
cp -f "$notes_src"/*.md "$notes_dst/" 2>/dev/null || true

echo "Snapshotted desktop wiring + Claude notes."
