-
Notifications
You must be signed in to change notification settings - Fork 1
/
xdg.bash
55 lines (47 loc) · 1.39 KB
/
xdg.bash
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
#!/usr/bin/env bash
xdg::data_file() {
local filename="$1"
echo -n "${XDG_DATA_HOME:-"$HOME/.local/share"}/$filename"
}
xdg::config_file() {
local filename="$1"
echo -n "${XDG_CONFIG_HOME:-"$HOME/.config"}/$filename"
}
xdg::cache_file() {
local filename="$1"
echo -n "${XDG_CACHE_HOME:-"$HOME/.cache"}/$filename"
}
xdg::runtime_file() {
local filename="$1"
if [ -z "${XDG_RUNTIME_DIR}" ];then
log::panic "\$XDG_RUNTIME_DIR isn't set but is required by this application"
fi
echo -n "${XDG_RUNTIME_DIR}/$filename"
}
xdg::_find_file_in_paths() {
local filename="$1"
local paths="$2"
local directories=()
mapfile -d':' -t directories <<< "$paths"
for dir in "${directories[@]}";do
dir=$(echo -n "$dir"|tr -d "\n")
log::debug "looking for $dir/$filename..."
if [ -e "$dir/$filename" ];then
log::debug "found $dir/$filename"
echo -n "$dir/$filename"
return
fi
done
}
xdg::find_data_file() {
local filename="$1"
local data_dirs
data_dirs="$(xdg::data_file ""):${XDG_DATA_DIRS:-"/usr/local/share/:/usr/share/"}"
xdg::_find_file_in_paths "$filename" "$data_dirs"
}
xdg::find_config_file() {
local filename="$1"
local config_dirs
config_dirs="$(xdg::config_file ""):${XDG_CONFIG_DIRS:-"/etc/xdg"}"
xdg::_find_file_in_paths "$filename" "$config_dirs"
}