aboutsummaryrefslogtreecommitdiff
path: root/module/xdg/basedir.scm
blob: f4e7b89b94760067cbaa4d913553264a24faa640 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
;;; Commentary:
;;; https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html
;;; Code:

(define-module (xdg basedir)
  :export (runtime-dir
           data-home config-home state-home cache-home
           data-dirs config-dirs))

;;; Check if an environment variable is set to a non-empty value.
(define (set? var)
  (cond ((getenv var)
         => (lambda (s)
              (if (string-null? s)
                  #f s)))
        (else #f)))

;;; XDG_DATA_HOME
;;; $HOME/.local/share
(define (data-home)
  (or (set? "XDG_DATA_HOME")
      (string-append (getenv "HOME") "/.local/share")))

;;; XDG_CONFIG_HOME
;;; $HOME/.config
(define (config-home)
  (or (set? "XDG_CONFIG_HOME")
      (string-append (getenv "HOME") "/.config")))

;;; XDG_STATE_HOME
;;; $HOME/.local/state
(define (state-home)
  (or (set? "XDG_STATE_HOME")
      (string-append (getenv "HOME") "/.local/state")))

;;; XDG_DATA_DIRS
;;; colon (:) sepparated, in addition to XDG_DATA_HOME
;;; /usr/local/share/:/usr/share/
(define (data-dirs)
  (let ((str (set? "XDG_DATA_DIRS")))
    (if str
        (string-split str #\:)
        '("/usr/local/share" "/usr/share"))))

;;; XDG_CONFIG_DIRS
;;; colon (:) separated, in adddition to XDG_CONFIG_HOME
;;; /etc/xdg
(define (config-dirs)
  (let ((str (set? "XDG_CONFIG_DIRS")))
    (if str
        (string-split str #\:)
        '("/etc/xdg"))))

;;; XDG_CACHE_HOME
;;; $HOME/.cache
(define (cache-home)
  (or (set? "XDG_CACHE_HOME")
      (string-append (getenv "HOME") "/.cache")))

;;; XDG_RUNTIME_DIR
;;; Default to /tmp or /tmp/$(uid), and raise a warning
(define (runtime-dir)
  (or (set? "XDG_RUNTIME_DIR")
      (begin
        (display "WARNING: XDG_RUNTIME_DIR unset, defaulting to /tmp\n"
                 (current-error-port))
        "/tmp")))