-
Notifications
You must be signed in to change notification settings - Fork 0
/
dotfiles.sh
executable file
·174 lines (147 loc) · 4.64 KB
/
dotfiles.sh
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#!/bin/sh
# shellcheck source-path=SCRIPTDIR
: "${DOTFILES_HOME:="$HOME/.dotfiles"}"
dotfiles_link() {
link() {
target="$1"
origin="$DOTFILES_HOME/$target"
destination="$2"
if [ -L "$destination" ]; then
echo "Linking $target to $destination"
elif [ -f "$destination" ]; then
echo "WARNING: $destination already exists"
elif [ -d "$destination" ]; then
echo "WARNING: $destination already exists"
else
destination_parent="$(dirname "$destination")"
if [ ! -d "$destination_parent" ]; then
mkdir -p "$destination_parent"
fi
echo "Linking $target to $destination"
ln -fns "$origin" "$destination"
fi
}
link_contents() {
target="$1"
origin="$DOTFILES_HOME/$target"
destination="$2"
if [ ! -d "$destination" ]; then
mkdir -p "$destination"
fi
echo "Linking $target/* to $destination"
for f in "$origin"/*; do
if [ -f "$f" ]; then
name="$(basename "$f")"
ln -fns "$f" "$destination/$name"
fi
done
for d in "$origin"/*/; do
if [ -d "$d" ]; then
name="$(basename "$d")"
ln -fns "$f" "$destination/$name"
fi
done
}
# Source XDG environment variables
. "$DOTFILES_HOME/zsh/.zshenv"
# Link executables
link dotfiles.sh "$XDG_BIN_HOME/dotfiles"
link bin/sqlite-to-json.sh "$XDG_BIN_HOME/sqlite-to-json"
# Link common dotfiles
link alacritty "$XDG_CONFIG_HOME/alacritty"
link fourmolu/fourmolu.yaml "$XDG_CONFIG_HOME/fourmolu.yaml"
link git "$XDG_CONFIG_HOME/git"
link ghc "$XDG_CONFIG_HOME/ghc"
link ideavim "$XDG_CONFIG_HOME/ideavim"
link npm "$XDG_CONFIG_HOME/npm"
link nvim "$XDG_CONFIG_HOME/nvim"
link tig "$XDG_CONFIG_HOME/tig"
link zsh "$XDG_CONFIG_HOME/zsh"
link zsh/.zshenv "$HOME/.zshenv"
# Link host-specific dotfiles
if [ "$(uname)" = Linux ]; then
link sublime-merge "$XDG_CONFIG_HOME/sublime-merge/Packages/User"
link sublime-text "$XDG_CONFIG_HOME/sublime-text/Packages/User"
elif [ "$(uname)" = Darwin ]; then
link_contents mac/LaunchAgents "$HOME/Library/LaunchAgents"
link_contents mac/Services "$HOME/Library/Services"
link sublime-merge "$HOME/Library/Application Support/Sublime Merge/Packages/User"
link sublime-text "$HOME/Library/Application Support/Sublime Text/Packages/User"
fi
}
dotfiles_provision() {
# Source environment variables
. "$DOTFILES_HOME/zsh/.zshenv"
. "$DOTFILES_HOME/zsh/.zprofile"
# Bootstrap XDG directories
for xdgdir in "$XDG_BIN_HOME" "$XDG_CONFIG_HOME" "$XDG_DATA_HOME" "$XDG_CACHE_HOME" "$XDG_STATE_HOME"; do
mkdir -p "$xdgdir"
chmod 0700 "$xdgdir"
done
# Create standard directories and fix permissions
for dir in ~/.gnupg ~/.ssh; do
mkdir -p "$dir"
find "$dir" -type d -exec chmod 700 {} \;
find "$dir" -type f -exec chmod 600 {} \;
done
# Remove last login message from new terminal sessions
if [ ! -f ~/.hushlogin ]; then
touch ~/.hushlogin
fi
if [ "$(uname)" = Darwin ]; then
# Set preferences
. "$DOTFILES_HOME/mac/preferences.sh"
# Update Homebrew and install packages
if command -v brew >/dev/null 2>&1; then
brew update
brew upgrade
brew tap homebrew/bundle
brew bundle --file="$DOTFILES_HOME/mac/Brewfile"
else
echo "Homebrew not installed. Visit: https://brew.sh"
fi
fi
}
dotfiles_environment() {
. "$DOTFILES_HOME/zsh/.zshenv"
. "$DOTFILES_HOME/zsh/.zprofile"
if [ "$(uname)" = Darwin ]; then
. "$DOTFILES_HOME/mac/environment.sh"
fi
}
sourced=0
if [ -n "$ZSH_VERSION" ]; then
case $ZSH_EVAL_CONTEXT in
*:file) sourced=1 ;;
esac
elif [ -n "$BASH_VERSION" ]; then
(return 0 2>/dev/null) && sourced=1
else
case ${0##*/} in
sh | -sh | dash | -dash | ksh | -ksh) sourced=1 ;;
esac
fi
if [ "$sourced" -eq 0 ]; then
set -eu
usage="usage: $(basename "$0") [link|provision|environment]"
command="${1-}"
case "$command" in
link)
dotfiles_link
;;
provision)
dotfiles_provision
;;
environment)
dotfiles_environment
;;
-h | --help)
echo "$usage"
;;
*)
echo "$usage" && exit 1
;;
esac
else
unset sourced
fi