#!/bin/bash
# uhhm-session-init: per-user session setup for the shared NFS home. Runs
# early via systemd --user. Idempotent.
#   (1) relocate GnuPG lock files onto machine-local tmpfs so keyring/trustdb
#       dotlocking can't hang or race between machines sharing the home;
#   (2) ensure the machine-local XDG cache dir exists and is ours;
#   (3) if Firefox was switched to a machine-local profile (uhhm-firefox-xdg,
#       on ANY machine: the symlink lives in the shared home), make sure the
#       profile dir exists on THIS machine too.
set -eu
. /usr/lib/uhhm/uhhm-lib.sh

# (1) GnuPG locks -> machine-local tmpfs
gnupghome="${GNUPGHOME:-$HOME/.gnupg}"
lockdir="${XDG_RUNTIME_DIR:-/run/user/$(id -u)}/gnupg-db"
install -dm700 "$lockdir" "$gnupghome"
for lf in pubring.kbx.lock trustdb.gpg.lock; do
    ln -sfn "$lockdir/$lf" "$gnupghome/$lf"
done

# (2) machine-local XDG cache dir (XDG_CACHE_HOME points here, see
# environment.d/profile.d); refuse one someone else pre-created.
uhhm_owned_dir "$UHHM_CACHE_ROOT/$(id -un)" 700 || true

# (3) machine-local Firefox profile
if [ -L "${XDG_CONFIG_HOME:-$HOME/.config}/mozilla" ]; then
    uhhm_moz_ensure || true
fi
