]> arthur.barton.de Git - ax-zsh.git/blob - core/30_env/30_env.ax-io
ls: Add support for "lscolors.sh"
[ax-zsh.git] / core / 30_env / 30_env.ax-io
1 # AX-ZSH: Alex' Modular ZSH Configuration
2 # 30_env.ax-io: Setup environment
3
4 # Setup time zone
5 if [[ -z "$TZ" && -r "/etc/timezone" ]]; then
6         TZ=$(<"/etc/timezone") export TZ
7 fi
8
9 # Validate temporary directory
10 if [[ -z "$TMPDIR" || "$TMPDIR" = "/" ]]; then
11         if [[ -n "$XDG_RUNTIME_DIR" && -w "$XDG_RUNTIME_DIR" ]]; then
12                 # The "runtime directory" is set for this user, good, so use
13                 # it silently as the "temporary directory", too:
14                 TMPDIR="$XDG_RUNTIME_DIR"
15         else
16                 # Try to find a sane "temporary directory", but warn the user
17                 # that this is a best guess only!
18                 TMPDIR="$(dirname $(mktemp -ut tmp.XXXXXXXXXX))/"
19                 [[ -z "$TMPDIR" || "$TMPDIR" = "/" ]] && TMPDIR="/tmp"
20                 user_tmpdir="$TMPDIR$UID"
21                 mkdir -p "$user_tmpdir" >/dev/null 2>&1
22                 if [[ -w "$user_tmpdir" ]]; then
23                         TMPDIR="$user_tmpdir/"
24                         chmod 0700 "$TMPDIR"
25                 fi
26                 echo "Note: \"TMPDIR\" was not set, using \"$TMPDIR\"." >&2
27                 unset user_tmpdir
28         fi
29 fi
30 # Make sure TMPDIR ends in a slash (like on macOS by default): this makes its
31 # usage a bit safer ...
32 case "$TMPDIR" in
33         */) ;;
34         *)  TMPDIR="$TMPDIR/"
35 esac
36 if [[ ! -w "$TMPDIR" ]]; then
37         echo "Warning: Temporary directory \"$TMPDIR\" is not writable!" >&2
38 fi
39 export TMPDIR
40
41 # TMPDIR is the only one required to be set, but make sure that TMP, TEMP
42 # and TEMPDIR are set to the same sane path name when already present in the
43 # environment:
44 [[ -n "$TMP" ]] && export TMP="$TMPDIR"
45 [[ -n "$TEMP" ]] && export TEMP="$TMPDIR"
46 [[ -n "$TEMPDIR" ]] && export TEMPDIR="$TMPDIR"
47
48 # Setup XDG cache directory
49 if [[ -z "$XDG_CACHE_HOME" ]]; then
50         XDG_CACHE_HOME="$LOCAL_HOME/.cache"
51 fi
52 if [[ ! -d "$XDG_CACHE_HOME" ]]; then
53         mkdir -p "$XDG_CACHE_HOME"
54         chmod 0700 "$XDG_CACHE_HOME"
55 fi
56 export XDG_CACHE_HOME
57
58 # Setup XDG runtime directory
59 if [[ -z "$XDG_RUNTIME_DIR" ]]; then
60         XDG_RUNTIME_DIR="$TMPDIR/runtime-dir.$UID"
61 fi
62 if [[ ! -d "$XDG_RUNTIME_DIR" ]]; then
63         mkdir -p "$XDG_RUNTIME_DIR"
64         chmod 0700 "$XDG_RUNTIME_DIR"
65 fi
66 export XDG_RUNTIME_DIR
67
68 # Setup ZSH cache directory
69 if [[ -z "$ZSH_CACHE_DIR" ]]; then
70         ZSH_CACHE_DIR="$XDG_CACHE_HOME/zsh"
71 fi
72 if [[ ! -d "$ZSH_CACHE_DIR" ]]; then
73         mkdir -p "$ZSH_CACHE_DIR"
74         chmod 0700 "$ZSH_CACHE_DIR"
75 fi
76 export ZSH_CACHE_DIR