-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbash_common.sh
More file actions
73 lines (66 loc) · 1.63 KB
/
bash_common.sh
File metadata and controls
73 lines (66 loc) · 1.63 KB
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
69
70
71
72
73
#!/usr/bin/env bash
# Common environment detection functions
# Sourced by bashrc, git-config.sh, and install.sh
detect_platform() {
case "$(uname)" in
Linux) echo 'linux' ;;
Darwin) echo 'osx' ;;
*) echo 'unknown' ;;
esac
}
detect_hostname() {
local platform=$(detect_platform)
if [[ $platform == 'linux' ]]; then
hostname -f 2>/dev/null || hostname
else
hostname
fi
}
_detect_envtype_heuristic() {
local graphical=false
if [[ "${XDG_SESSION_TYPE:-}" == "x11" || "${XDG_SESSION_TYPE:-}" == "wayland" ]]; then
graphical=true
fi
if $graphical; then
if is_maxmind; then
echo 'local-work'
else
echo 'local-personal'
fi
else
if is_maxmind; then
echo 'remote-work'
else
echo 'remote-personal'
fi
fi
}
detect_envtype() {
if [[ -e "$HOME/.devcontainer-work" ]]; then
echo 'devcontainer-work'
elif [[ -e "$HOME/.local-work" ]]; then
echo 'local-work'
elif [[ -e "$HOME/.local-personal" ]]; then
echo 'local-personal'
elif [[ -e "$HOME/.remote-work" ]]; then
echo 'remote-work'
elif [[ -e "$HOME/.remote-personal" ]]; then
echo 'remote-personal'
# Legacy marker files
elif [[ -e "$HOME/.laptop" ]]; then
echo 'local-work'
elif [[ -e "$HOME/.desktop" ]]; then
echo 'local-personal'
else
_detect_envtype_heuristic
fi
}
is_maxmind() {
[[ $(detect_hostname) =~ "maxmind" ]]
}
# Export variables if sourced (not just function definitions)
if [[ "${BASH_SOURCE[0]}" != "${0}" ]]; then
export DOTFILES_PLATFORM=$(detect_platform)
export DOTFILES_HOSTNAME=$(detect_hostname)
export DOTFILES_ENVTYPE=$(detect_envtype)
fi