/
setup
executable file
·293 lines (254 loc) · 6.85 KB
/
setup
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
#!/bin/bash
# do not set -e as stow is supposed to fail in some cases
set -uo pipefail
if ! command -v stow >/dev/null 2>&1; then
echo "ERROR: \"stow\" not installed"
fi
help() {
cat <<EOF
usage: ./setup [subcommand] [flags] [args]
Subcommand:
help Returns help
install Run full installation
uninstall Run full uninstallation
stow Stow dir
unstow Unstow dir
Flags:
-a, --all Act on all subdirectories in \$DOTS_DIR
-s, --simulate Simulation mode
-d, --dots Set \$DOTS_DIR. Defaults to "./dots"
-t, --target Set \$TARGET_DIR. Defaults to "\$HOME"
Examples:
# simulate stowing a single dir
./setup stow --simulate bspwm
# stow all dirs
./setup stow --all
# unstow a single dir with custom dots dir
./setup unstow --dir dotfiles bspwm
EOF
}
DEFAULT_WARNING="WARNING: in simulation mode so not modifying filesystem."
DOTS_DIR="dots"
TARGET_DIR="$HOME"
SIMULATE=false
UNSTOW=false
ALL=false
# add no fold directories here
NO_FOLD_DIRS=("redshift" "dunst")
build() {
STOW_COMMAND="stow -v"
if $SIMULATE; then
STOW_COMMAND+=" -n"
fi
if $UNSTOW; then
STOW_COMMAND+=" -D"
fi
}
stow_one() {
build
# https://krzysztofzuraw.com/blog/2019/multiple-stow-directories/
if [[ "${NO_FOLD_DIRS[*]}" =~ "$1" ]]; then
STOW_COMMAND+="-t $TARGET_DIR -d $DOTS_DIR/$1 --no-folding . 2>&1"
else
STOW_COMMAND+="-t $TARGET_DIR -d $DOTS_DIR/$1 . 2>&1"
fi
eval "$STOW_COMMAND"
}
stow_all() {
local success=0 fail=0 MESSAGE SUCCESS_MATCH
MESSAGE="Linking all directories in \"./$DOTS_DIR\" to \"$TARGET_DIR\":"
SUCCESS_MATCH="^LINK:.*"
if $UNSTOW; then
MESSAGE="Unlinking all directories in \"./$DOTS_DIR\" to \"$TARGET_DIR\":"
SUCCESS_MATCH="^UNLINK:.*"
fi
if $SIMULATE; then
echo "RUNNING STOW SIMULATE:"
fi
echo "$MESSAGE"
for dir in dots/*; do
# skip
if [[ $dir == "dots/ssh" ]]; then
continue
else
output="$(stow_one "${dir##*/}")"
fi
# split output by newline, do not quote within array
IFS=$'\n' lines=($output)
for line in "${lines[@]}"; do
if [[ "$line" =~ $SUCCESS_MATCH ]]; then
((success++))
printf " %s\n" "$line"
elif [[ "$line" == "$DEFAULT_WARNING" ]]; then
:
elif [[ "$line" =~ ^WARNING:.* ]]; then
((fail++))
else
:
fi
done
done
if ! ((success + fail)); then
echo "NO DIRECTORIES STOWED!"
else
echo "STOW COMPLETE: $success SUCCESS; $fail FAIL"
fi
}
full_install() {
if $SIMULATE; then
printf "SIMULATION MODE: No files will be changed!\n\n"
fi
echo "Stow all dotfiles"
stow_all
echo "Install .Xresources:"
if [[ ! -f "./etc/.Xresources" ]]; then
echo ".Xresources not found, skipping..."
elif $SIMULATE; then
printf " Copying ./etc/.Xresources to %s/.Xresources\n" "$HOME"
else
cp ./etc/.Xresources "$HOME/.Xresources"
fi
echo "Install desktop files:"
if [[ ! -d "./etc/desktop" ]]; then
echo ".desktop files not found, skipping..."
elif $SIMULATE; then
printf " Copying ./etc/desktop/* to %s/.local/share/applications\n" "$HOME"
else
mkdir -p "$HOME/.local/share/applications"
cp ./etc/desktop/* "$HOME/.local/share/applications/"
fi
echo "Installing firefox customizations:"
ff_dir=$HOME/.mozilla/firefox
ff_path="$ff_dir/$(grep 'Default=.*\.default-release$' $ff_dir/profiles.ini | cut -d"=" -f2)"
if [[ ! -d "$ff_dir" ]]; then
printf " Firefox not installed, skipping...\n"
elif [[ ! -d $ff_path ]]; then
printf " Firefox default profile not available, skipping...\n"
elif $SIMULATE; then
printf " Copying ./etc/firefox/chrome dir to %s\n" "$ff_path"
printf " Copying ./etc/firefox/user.js to %s\n" "$ff_path"
else
cp -r ./etc/firefox/chrome "$ff_path"
cp ./etc/firefox/user.js "$ff_path"
fi
echo "Installing neovim plugins:"
if ! command -v nvim >/dev/null 2>&1; then
printf " nvim not installed, skipping..."
elif $SIMULATE; then
printf " Running command: 'nvim --headless \"+Lazy! sync\" +qa'"
else
nvim --headless "+Lazy! sync" +qa
fi
echo "Installation complete!"
}
full_uninstall() {
if $SIMULATE; then
printf "SIMULATION MODE: No files will be changed!\n\n"
fi
echo "Unstow all dotfiles"
stow_all
echo "Uninstall .Xresources:"
if $SIMULATE; then
printf " Removing %s/.Xresources\n" "$HOME"
else
rm "$HOME/.Xresources"
fi
echo "Remove desktop files:"
if $SIMULATE; then
printf " Removing %s/.local/share/applications/*.desktop\n" "$HOME"
else
rm "$HOME/.local/share/applications/*.desktop"
fi
echo "Installing firefox customizations:"
ff_dir=$HOME/.mozilla/firefox
ff_path="$ff_dir/$(grep 'Default=.*\.default-release$' $ff_dir/profiles.ini | cut -d"=" -f2)"
if $SIMULATE; then
printf " Removing %s/chrome\n" "$ff_path"
printf " Removing %s/user.js\n" "$ff_path"
else
rm -r "$ff_path/chrome"
rm "$ff_path/user.js"
fi
echo "Remove all neovim plugins:"
if $SIMULATE; then
printf " Removing %s/.local/share/nvim/lazy\n" "$HOME"
else
rm -r "$HOME/.local/share/nvim/lazy"
fi
echo "Installation complete!"
}
parse_global_flags() {
while [ $# -gt 0 ]; do
case $1 in
-s | --simulate) SIMULATE=true ;;
-a | --all) ALL=true ;;
-d | --dots)
DOTS_DIR=$2
shift
;;
-t | --target)
TARGET_DIR=$2
shift
;;
-*)
echo "$0: error - unrecognized option \"$1\"" 1>&2
help
exit 1
;;
*) break ;;
esac
shift
done
# set global args array to remaining params
args=($@)
}
declare -a args
subcommand=${1:-""}
case $subcommand in
"install")
shift
parse_global_flags "$@"
ALL=true
UNSTOW=false
full_install
;;
"uninstall")
shift
parse_global_flags "$@"
ALL=true
UNSTOW=true
full_uninstall
;;
"stow")
shift
parse_global_flags "$@"
# set args to positional params
set -- "${args[@]}"
UNSTOW=false
if $ALL; then
stow_all
else
stow_one "$@"
fi
;;
"unstow")
shift
parse_global_flags "$@"
set -- "${args[@]}"
UNSTOW=true
if $ALL; then
stow_all
else
stow_one "$@"
fi
;;
"" | "-h" | "--help" | "help")
help
exit 1
;;
*)
echo "$0: error - unrecognized command \"$1\""
help
exit 1
;;
esac