+(defun packages-find-user-topdir ()
+ "Return the top of the user's local package hierachy.
+
+This should be the equivalent of `$XDG_DATA_HOME/sxemacs'. But will
+fall back to the old legacy directory, `~/.sxemacs', if that is the
+value of `user-init-directory'. It may also be in the location set
+via the `-user-pkgs-directory' command line arg."
+ (let* ((legacy (getenv "SXE_USE_LEGACY"))
+ (xdg (getenv "XDG_DATA_HOME"))
+ (xdgdir (or (and xdg
+ (paths-construct-path `(,xdg "sxemacs")))
+ (paths-construct-path
+ `(,(user-home-directory)
+ ".local" "share" "sxemacs"))))
+ warndirs)
+ ;; Set it, if not already.
+ (when (null user-packages-topdir)
+ (if (or legacy
+ (string= (expand-file-name "~/.sxemacs")
+ (expand-file-name user-init-directory)))
+ (setq user-packages-topdir user-init-directory)
+ (setq user-packages-topdir (file-name-as-directory xdgdir))))
+ ;; Create if needed.
+ (unless (paths-file-readable-directory-p user-packages-topdir)
+ (make-directory-path user-packages-topdir))
+ ;; Warn if a user has pkg dirs in 2 or more of ~/.sxemacs,
+ ;; XDG_CONFIG_HOME, XDG_DATA_HOME, but not for symlinked dirs.
+ (let* ((regexp #r"\(s?xemacs\|site\|mule\)-packages")
+ (cpkgd (or (and (getenv "XDG_CONFIG_HOME")
+ (paths-construct-path
+ `(,(getenv "XDG_CONFIG_HOME") "sxemacs")))
+ (paths-construct-path
+ `(,(user-home-directory) ".config" "sxemacs")))))
+ ;; ~/.sxemacs
+ (when (and (not (file-symlink-p "~/.sxemacs"))
+ (file-directory-p "~/.sxemacs")
+ (directory-files "~/.sxemacs" nil regexp nil 'subdir))
+ (push "~/.sxemacs" warndirs))
+ ;; XDG_DATA_HOME
+ (when (and (not (file-symlink-p xdgdir))
+ (file-directory-p xdgdir)
+ (directory-files xdgdir nil regexp nil 'subdir))
+ (push xdgdir warndirs))
+ ;; XDG_CONFIG_HOME
+ (when (and (not (file-symlink-p cpkgd))
+ (file-directory-p cpkgd)
+ (directory-files cpkgd nil regexp nil 'subdir))
+ (push cpkgd warndirs))
+ (when (> (length warndirs) 1)
+ (lwarn 'multi-pkgd nil
+ "Multiple user package hierarchies detected!!
+
+The following directories contain what appears to be package subdirs:
+%S.
+
+Currently in use: `%s'
+
+This, in itself, is not necessarily a problem, but it may mean that
+some of your packages won't be visible to this SXEmacs instance. You
+should keep all of your local packages in a single location.
+
+See `display-warning-suppressed-classes' to suppress this warning"
+ warndirs user-packages-topdir)))
+ ;; Return the dir
+ user-packages-topdir))
+
+(defun packages-compute-package-locations (user-packages-topdir)