-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstall.bash
executable file
·421 lines (341 loc) · 10.3 KB
/
install.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
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
#!/usr/bin/bash
# SPDX-FileCopyrightText: Steven Ward
# SPDX-License-Identifier: OSL-3.0
# Run this script from the target directory.
# shellcheck disable=SC2059
SCRIPT_NAME="$(basename -- "${BASH_SOURCE[0]}")"
SCRIPT_DIR="$(dirname -- "${BASH_SOURCE[0]}")"
SCRIPT_VERSION='2022-09-08'
SCRIPT_AUTHOR='Steven Ward'
SCRIPT_LICENSE='OSL-3.0'
VERBOSE=false
RELATIVE=false
COPY_ALL=false
DELETE=false
DRY_RUN=false
function print_version
{
cat <<EOT
$SCRIPT_NAME $SCRIPT_VERSION
License: $SCRIPT_LICENSE
Written by $SCRIPT_AUTHOR
EOT
}
function print_help
{
printf 'Usage: %q [OPTION]...\n' "$SCRIPT_NAME"
cat <<EOT
Install dotfiles. Run this script from the target directory.
Files in the "copy" directory will be copied to the target directory.
Files in the "link" directory will be symbolically linked to the target directory.
In both cases, parent directories are created, preserving directory structure.
OPTIONS
-V : Print the version information, then exit.
-h : Print this message, then exit.
-v : Print extra output.
Commands such as cp, ln, mkdir, rm, and rmdir are verbose by default.
-r : Use a relative path when creating symbolic links.
-c : Copy all files, even those in the "link" directory.
-d : Attempt to delete almost everything created by this script.
Only attempt to delete files and symbolic links that are identical to their source.
Only attempt to delete empty folders that contained dotfiles.
-n : Show what would be done without doing anything.
EOT
}
function print_warning
{
printf 'Warning: ' 1>&2
printf -- "$@" 1>&2
printf '\n' 1>&2
}
function print_error
{
printf 'Error: ' 1>&2
printf -- "$@" 1>&2
printf '\n' 1>&2
printf 'Try "%q -h" for more information.\n' "$SCRIPT_NAME" 1>&2
exit 1
}
function print_verbose
{
if $VERBOSE
then
printf '# '
printf -- "$@"
printf '\n'
fi
}
function copy_dotfiles
{
SRC_DIR="$1"
print_verbose 'SRC_DIR=%q' "$SRC_DIR"
if [[ ! -d "$SRC_DIR" ]]
then
print_error 'Directory does not exist: %q' "$SRC_DIR"
fi
# https://mywiki.wooledge.org/BashFAQ/001#Input_source_selection
find "$SRC_DIR" -type f -printf '%P\0' | while IFS= read -r -d '' FILE
do
print_verbose 'FILE=%q' "$FILE"
DST_DIR=$(dirname -- "$FILE")
print_verbose 'DST_DIR=%q' "$DST_DIR"
if [[ -f "$FILE" ]]
then
print_verbose 'Destination file exists'
if cmp --quiet -- "$FILE" "$SRC_DIR/$FILE"
then
print_verbose 'Destination and source files are the same'
continue
fi
if [[ ! -s "$SRC_DIR/$FILE" ]]
then
print_verbose 'Source file is empty'
continue
fi
if [[ "$FILE" -nt "$SRC_DIR/$FILE" ]]
then
print_verbose 'Destination file is newer than source file'
continue
fi
fi
if [[ "$DST_DIR" != . ]]
then
if $DRY_RUN
then
if [[ ! -d "$DST_DIR" ]]
then
echo \
mkdir --verbose --parents -- "$DST_DIR"
fi
else
if [[ ! -d "$DST_DIR" ]]
then
mkdir --verbose --parents -- "$DST_DIR" || return
fi
fi
fi
if $DRY_RUN
then
echo \
cp --preserve --verbose --backup=numbered --target-directory "$DST_DIR" -- "$SRC_DIR/$FILE"
else
cp --preserve --verbose --backup=numbered --target-directory "$DST_DIR" -- "$SRC_DIR/$FILE" || return
fi
done
}
function delete_copied_dotfiles
{
SRC_DIR="$1"
print_verbose 'SRC_DIR=%q' "$SRC_DIR"
if [[ ! -d "$SRC_DIR" ]]
then
print_error 'Directory does not exist: %q' "$SRC_DIR"
fi
# https://mywiki.wooledge.org/BashFAQ/001#Input_source_selection
find "$SRC_DIR" -type f -printf '%P\0' | while IFS= read -r -d '' FILE
do
print_verbose 'FILE=%q' "$FILE"
DST_DIR=$(dirname -- "$FILE")
print_verbose 'DST_DIR=%q' "$DST_DIR"
if [[ ! -f "$FILE" ]]
then
print_verbose 'Destination does not file exist'
continue
fi
if ! cmp --quiet -- "$FILE" "$SRC_DIR/$FILE"
then
print_verbose 'Destination and source files are not the same'
continue
fi
if $DRY_RUN
then
echo \
rm --verbose -- "$FILE"
else
rm --verbose -- "$FILE" || return
fi
if [[ "$DST_DIR" != . ]]
then
if $DRY_RUN
then
echo \
rmdir --verbose --parents --ignore-fail-on-non-empty -- "$DST_DIR"
else
rmdir --verbose --parents --ignore-fail-on-non-empty -- "$DST_DIR" || return
fi
fi
done
}
function link_dotfiles
{
SRC_DIR="$1"
print_verbose 'SRC_DIR=%q' "$SRC_DIR"
if [[ ! -d "$SRC_DIR" ]]
then
print_error 'Directory does not exist: %q' "$SRC_DIR"
fi
# https://mywiki.wooledge.org/BashFAQ/001#Input_source_selection
find "$SRC_DIR" -type f -printf '%P\0' | while IFS= read -r -d '' FILE
do
print_verbose 'FILE=%q' "$FILE"
DST_DIR=$(dirname -- "$FILE")
print_verbose 'DST_DIR=%q' "$DST_DIR"
if [[ -h "$FILE" ]]
then
print_verbose 'Destination link exists'
if [[ "$(realpath -- "$FILE")" == "$(realpath -- "$SRC_DIR/$FILE")" ]]
then
print_verbose 'Destination link target is the same as the source file'
continue
fi
fi
if [[ "$DST_DIR" != . ]]
then
if $DRY_RUN
then
if [[ ! -d "$DST_DIR" ]]
then
echo \
mkdir --verbose --parents -- "$DST_DIR"
fi
else
if [[ ! -d "$DST_DIR" ]]
then
mkdir --verbose --parents -- "$DST_DIR" || return
fi
fi
fi
if $RELATIVE
then
if $DRY_RUN
then
echo \
ln --symbolic --verbose --backup=numbered --target-directory "$DST_DIR" --relative -- "$SRC_DIR/$FILE"
else
ln --symbolic --verbose --backup=numbered --target-directory "$DST_DIR" --relative -- "$SRC_DIR/$FILE" || return
fi
else
if $DRY_RUN
then
echo \
ln --symbolic --verbose --backup=numbered --target-directory "$DST_DIR" -- "$(realpath -- "$SRC_DIR/$FILE")"
else
ln --symbolic --verbose --backup=numbered --target-directory "$DST_DIR" -- "$(realpath -- "$SRC_DIR/$FILE")" || return
fi
fi
done
}
function delete_linked_dotfiles
{
SRC_DIR="$1"
print_verbose 'SRC_DIR=%q' "$SRC_DIR"
if [[ ! -d "$SRC_DIR" ]]
then
print_error 'Directory does not exist: %q' "$SRC_DIR"
fi
# https://mywiki.wooledge.org/BashFAQ/001#Input_source_selection
find "$SRC_DIR" -type f -printf '%P\0' | while IFS= read -r -d '' FILE
do
print_verbose 'FILE=%q' "$FILE"
DST_DIR=$(dirname -- "$FILE")
print_verbose 'DST_DIR=%q' "$DST_DIR"
if [[ ! -h "$FILE" ]]
then
print_verbose 'Destination link does not exist'
continue
fi
if [[ "$(realpath -- "$FILE")" != "$(realpath -- "$SRC_DIR/$FILE")" ]]
then
print_verbose 'Destination link target is not the same as the source file'
continue
fi
if $DRY_RUN
then
echo \
rm --verbose -- "$FILE"
else
rm --verbose -- "$FILE" || return
fi
if [[ "$DST_DIR" != . ]]
then
if $DRY_RUN
then
echo \
rmdir --verbose --parents --ignore-fail-on-non-empty -- "$DST_DIR"
else
rmdir --verbose --parents --ignore-fail-on-non-empty -- "$DST_DIR" || return
fi
fi
done
}
function parse_options
{
while getopts 'Vhvrcdn' OPTION
do
case "$OPTION" in
V) print_version ; exit ;;
h) print_help ; exit ;;
v) VERBOSE=true ;;
r) RELATIVE=true ;;
c) COPY_ALL=true ;;
d) DELETE=true ;;
n) DRY_RUN=true ;;
*)
# Note: $OPTION is '?'
#print_error "Option is unknown."
exit 1
;;
esac
done
shift $((OPTIND - 1))
print_verbose 'RELATIVE=%s' "$RELATIVE"
print_verbose 'COPY_ALL=%s' "$COPY_ALL"
print_verbose 'DELETE=%s' "$DELETE"
print_verbose 'DRY_RUN=%s' "$DRY_RUN"
}
function main
{
if [[ "$PWD" == "$(realpath -- "$SCRIPT_DIR")"* ]]
then
print_error 'May not install dotfiles within itself'
fi
REL_DOTFILES_DIR="$(realpath --relative-to "$PWD" -- "$SCRIPT_DIR")"
print_verbose 'REL_DOTFILES_DIR=%q' "$REL_DOTFILES_DIR"
# shellcheck disable=SC1091
source "$SCRIPT_DIR"/link/.config/bash/xdg-envvars.bash || return
if $DELETE
then
# Only delete dotfiles, not unrelated empty dirs, such as XDG base dirs.
delete_copied_dotfiles "$REL_DOTFILES_DIR"/copy || return
if $COPY_ALL
then
delete_copied_dotfiles "$REL_DOTFILES_DIR"/link || return
else
delete_linked_dotfiles "$REL_DOTFILES_DIR"/link || return
fi
if ! $DRY_RUN
then
fc-cache || return
fi
else
copy_dotfiles "$REL_DOTFILES_DIR"/copy || return
if $COPY_ALL
then
copy_dotfiles "$REL_DOTFILES_DIR"/link || return
else
link_dotfiles "$REL_DOTFILES_DIR"/link || return
fi
if ! $DRY_RUN
then
fc-cache || return
fi
if ! $DRY_RUN
then
python3 -m compileall ~/.local/lib/python/ || return
python3 -O -m compileall ~/.local/lib/python/ || return
python3 -OO -m compileall ~/.local/lib/python/ || return
fi
fi
}
parse_options "$@"
main