#!/bin/sh
# linuxlite-refresh-thunar-uca
# -----------------------------------------------------------------------------
# Per-user, runs once at login (XFCE autostart). Refreshes the user's Thunar
# custom-actions file (~/.config/Thunar/uca.xml) from the system template at
# /etc/skel — but ONLY when the user's copy is an unmodified Linux Lite default
# (or missing). Genuine user customisations are NEVER overwritten.
#
# /etc/skel is consulted only when an account is first created, so users who
# already existed before lite-patch shipped the corrected uca.xml would
# otherwise be stuck with the stale one. This brings them up to date safely.
#
# Decision logic (in order):
#   * template unreadable        -> do nothing
#   * user's file missing        -> install template
#   * user's file == template    -> already current, do nothing (idempotent)
#   * we wrote it last and the user hasn't changed it since (stamp matches)
#                                -> refresh to template
#   * user's file hash is a KNOWN unmodified LL default
#                                -> refresh to template
#   * otherwise (unknown / customised)
#                                -> leave it ALONE
#
# Deliberately no `set -e`: a login hook must never be able to disrupt the
# session. Every step degrades to a quiet no-op on error.
# -----------------------------------------------------------------------------

SKEL=/etc/skel/.config/Thunar/uca.xml
CFG="${XDG_CONFIG_HOME:-$HOME/.config}"
DEST="$CFG/Thunar/uca.xml"
STAMP_DIR="$CFG/linuxlite"
STAMP="$STAMP_DIR/uca.managed"

[ -r "$SKEL" ] || exit 0

# sha256 of Linux Lite *default* uca.xml versions that predate this mechanism.
# A user file matching any of these is an untouched LL default and is safe to
# replace. ONLY add hashes you are certain are pristine shipped defaults
# (append-only; never list a hash that might be a user customisation).
KNOWN_OLD_DEFAULTS="
abd1c50f8d968fbb74c1e90525e2b213cc6e08423e41003a5269d51bb34365b1
8fb435775de8a06ee4d82acfdc0f7d26a5a02955bb211416e5f2755e39f91c9a
"

sha() { sha256sum "$1" 2>/dev/null | cut -d' ' -f1; }

install_template() {
    mkdir -p "$CFG/Thunar" "$STAMP_DIR" 2>/dev/null || return 1
    cp -f "$SKEL" "$DEST" 2>/dev/null || return 1
    sha "$DEST" > "$STAMP" 2>/dev/null
    return 0
}

SKEL_SHA="$(sha "$SKEL")"
[ -n "$SKEL_SHA" ] || exit 0

# No file yet -> install it.
if [ ! -f "$DEST" ]; then
    install_template
    exit 0
fi

DEST_SHA="$(sha "$DEST")"

# Already the current version -> nothing to do.
if [ "$DEST_SHA" = "$SKEL_SHA" ]; then
    exit 0
fi

# We installed it previously and the user hasn't edited it since -> refresh.
if [ -f "$STAMP" ]; then
    LAST="$(cat "$STAMP" 2>/dev/null)"
    if [ -n "$LAST" ] && [ "$DEST_SHA" = "$LAST" ]; then
        install_template
        exit 0
    fi
fi

# Unmodified pre-mechanism LL default -> refresh.
for h in $KNOWN_OLD_DEFAULTS; do
    if [ "$DEST_SHA" = "$h" ]; then
        install_template
        exit 0
    fi
done

# Anything else is a customisation (or a version we don't recognise): leave it.
exit 0
