# Redirect XDG data/cache/state to local disk under /var/user/$USER.
# /var/user must allow users to create their own subdirectory.

# This fixes Opencode on users with home directories on NFS.


if [ -n "$USER" ] && [ "$USER" != "root" ]; then
  LOCAL_BASE="/var/user/$USER"

  test -d $LOCAL_BASE || mkdir $LOCAL_BASE

  if [ ! -d "$LOCAL_BASE" ]; then
  if mkdir -p \
      "$LOCAL_BASE/.local/share" \
      "$LOCAL_BASE/.cache" \
      "$LOCAL_BASE/.local/state" 2>/dev/null
  then
    chmod 700 "$LOCAL_BASE" \
              "$LOCAL_BASE/.local" \
              "$LOCAL_BASE/.local/share" \
              "$LOCAL_BASE/.local/state" \
              "$LOCAL_BASE/.cache" 2>/dev/null || true
  fi
  fi

  if [ -d "$LOCAL_BASE" ]; then
    export XDG_DATA_HOME="$LOCAL_BASE/.local/share"
    export XDG_CACHE_HOME="$LOCAL_BASE/.cache"
    export XDG_STATE_HOME="$LOCAL_BASE/.local/state"
  fi
fi

