-### xdg local dir
-# NOTE: need this for tmp, so confirm it exists.
-# XXX: perms are not specified for XDG dirs except runtime, but I think 760
-# makes the most sense. shouldn't break anything since no one else should
-# be poking around in our dir.
-[[ -e ${_sev_home:-~}/.local ]] || mkdir -m760 ${_sev_home:-~}/.local
-
-### tmp
-# NOTE: specs say that POSIX tmp and XDG runtime directories should exist
-# until the last session is logged out (POSIX can exist for longer).
-# since we can't reliably keep track of sessions in a cross-platform
-# manner, the current implementation should use a separate directory per
-# toplevel session (i.e. SHLVL=1). this should placate most applications,
-# though it is not expressly spec compliant.
-if [[ ! -v _sev_tmp ]] {
- _sev_tmp=${_sev_home:-~}/.local/tmp
- # NOTE: race condition/remove in use files
- [[ -h $_sev_tmp ]] && unlink $_sev_tmp 2>/dev/null
- t=${TMPDIR:-${TEMP:-${TMP:-/tmp}}}/.home-$LOGNAME
- # create personal tmp dir under system tmp
- [[ -e $t ]] || mkdir -m700 $t 2>/dev/null
- if [[ ! -d $t ]] {
- [[ -o interactive ]] &&
- print -P "%F{orange}*** Can't create TMPDIR $t, using $_sev_tmp%f"
- # fallback bare directory
- [[ -e $_sev_tmp ]] || mkdir -m700 $_sev_tmp 2>/dev/null
- if [[ ! -d $_sev_tmp ]] {
- [[ -o interactive ]] &&
- print -P "%F{red}!!! No usable TMPDIR%f"
- unset _sev_tmp
- } else {
- t=$_sev_tmp
- }
- } elif [[ -e $_sev_tmp ]] {
- [[ -o interactive ]] &&
- print -P "%F{orange}*** $_sev_tmp occluded, can't link to TMPDIR $t%f"
- _sev_tmp=$t
- } else {
- ln -s $t $_sev_tmp 2>/dev/null
- }
- if [[ -v _sev_tmp ]] {
- # ensure dir is clean
- _sev_zcleanup tmp
- # finally create our subdir for this session
- t=$_sev_tmp/.session.$$
- if ! mkdir -m700 $t 2>/dev/null; then
- [[ -o interactive ]] &&
- print -P "%F{red}!!! Can't create session subdir $t, using $_sev_tmp%f"
- t=$_sev_tmp
- fi
- export _sev_tmp TMPDIR=$t TEMP=$t TMP=$t
- unset t
- }
-}
-
-### xdg
-if [[ ! -v _sev_setup_xdg ]] {
- ## merge with any existing dirs and remove duplicates using unique arrays
- # NOTE: we are accepting whatever value might be set for CONFIG and DATA;
- # if it wasn't set, we just use default and leave it unset
- # NOTE: include and then remove CONFIG_HOME and DATA_HOME to ensure they
- # are not present in the array if it was added before we got to it
- typeset -UT XDG_DATA_DIRS xdg_data_dirs
- if [[ -v XDG_DATA_HOME ]] {
- export XDG_DATA_HOME
- } elif [[ ! -e ~/.local/share ]] {
- mkdir -m760 ~/.local/share
- }
- xdg_data_dirs=($XDG_DATA_HOME /{opt,usr/local,usr/pkg,usr}/share
- "${XDG_DATA_DIRS:+${xdg_data_dirs[@]}}")
- # XXX: if colons are not escaped, could remove unintended part of string
- export XDG_DATA_DIRS=${XDG_DATA_DIRS#$XDG_DATA_HOME:}
-
- typeset -UT XDG_CONFIG_DIRS xdg_config_dirs
- if [[ -v XDG_CONFIG_HOME ]] {
- export XDG_CONFIG_HOME
- } elif [[ ! -e ~/.config ]] {
- mkdir -m760 ~/.config
- }
- # I am of the belief .local should follow FHS /usr/local...
- [[ -e ~/.local/etc ]] || ln -s ~/.config ~/.local/etc
- xdg_config_dirs=($XDG_CONFIG_HOME ${XDG_CONFIG_DIRS:+"$xdg_config_dirs[@]"}
- {/opt,/usr/local,/usr/pkg,}/etc/xdg)
- # XXX: if colons are not escaped, could remove unintended part of string
- export XDG_CONFIG_DIRS=${XDG_CONFIG_DIRS#$XDG_CONFIG_HOME:}
-
- if [[ -v XDG_STATE_HOME ]] {
- export XDG_STATE_HOME
- } elif [[ ! -e ~/.local/state ]] {
- mkdir -m760 ~/.local/state
- }
-
- if [[ ! -v XDG_CACHE_HOME ]] {
- if [[ -v _sev_tmp ]] {
- export XDG_CACHE_HOME=$_sev_tmp/.xdg.cache
- [[ -e $XDG_CACHE_HOME ]] || mkdir -m700 $XDG_CACHE_HOME
- } elif [[ ! -e ~/.cache ]] {
- mkdir -m700 ~/.cache
- }
- }
-
- if [[ -v XDG_RUNTIME_DIR ]] {
- export XDG_RUNTIME_DIR
- } else {
- # make runtime dir in our session-specific tmpdir
- export XDG_RUNTIME_DIR=$TMPDIR/.xdg.runtime
- # same as in tmpdir creation, ensure dir doesn't exist
- if [[ -h $XDG_RUNTIME_DIR ]] {
- unlink $XDG_RUNTIME_DIR 2>/dev/null
- } elif [[ -e $XDG_RUNTIME_DIR ]] {
- rm -rf $XDG_RUNTIME_DIR 2>/dev/null
- }
- mkdir -m700 $XDG_RUNTIME_DIR 2>/dev/null
- }
-
- # source user dirs after other vars
- [[ -e $XDG_CONFIG_HOME/user-dirs.dirs ]] &&
- emulate sh -c "source $XDG_CONFIG_HOME/user-dirs.dirs"
- export _sev_setup_xdg=
-}
-