-
Notifications
You must be signed in to change notification settings - Fork 0
/
install
executable file
·437 lines (383 loc) · 10.2 KB
/
install
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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
#!/usr/bin/env bash
set -eo pipefail
shopt -s nullglob
SCRIPT_DIR="$( cd "$( dirname -- "${BASH_SOURCE[0]}" )" && pwd -P )"
PACKAGE_DIR="${SCRIPT_DIR}/pkg"
TARGET_DIR="$HOME"
HAS_INSTALLED=()
if [[ "$( uname )" = "Darwin" ]] ; then
LINK_FLAGS=-hs
else
LINK_FLAGS=-ns
fi
MODE=Install
VERBOSE=
DRY_RUN=
RUN_SCRIPTS=()
if [[ -t 1 ]] ; then
RESET="\e[0m"
DGRAY="\e[90m"
GRAY="\e[37m"
GREEN="\e[32m"
RED="\e[31m"
BLUE="\e[34m"
YELLOW="\e[33m"
else
RESET=""
DGRAY=""
GRAY=""
GREEN=""
RED=""
BLUE=""
YELLOW=""
fi
cmd() {
printf "${BLUE}[info] ${RESET}> %s\n" "$*"
if [[ -z "$DRY_RUN" ]] ; then
"$@"
fi
}
warn() {
printf "${YELLOW}[warn] ${RESET}%s\n" "$*" >&2
}
info() {
printf "${BLUE}[info] ${RESET}%s\n" "$*"
}
debug() {
if [[ -n "$VERBOSE" ]] ; then
printf "${DGRAY}[debug] ${RESET}%s\n" "$*"
fi
}
fatal() {
printf "${RED}[fatal] ${RESET}%s\n" "$*" >&2
exit 1
}
link_dir() {
local source="$1"
local target="$2"
if [[ ! -L "$source" ]] && [[ -d "$source" ]] ; then
local strategy
if [[ -e "${source}/dotfiles-strategy" ]] ; then
strategy="$( cat "${source}/dotfiles-strategy" )"
fi
if [[ "$strategy" = "link" ]] ; then
link_file "$source" "$target"
return 0
fi
if [[ "$MODE" == Install ]] ; then
if [[ -e "$target" ]] ; then
if [[ ! -d "$target" ]] ; then
fatal "Target folder ${target} (for ${source}) exists but is not a directory!"
fi
debug "Target directory $target exists, not creating"
else
debug "Target directory $target missing, creating"
cmd mkdir -p "$target" || fatal "Could not create directory ${target}"
fi
else
if [[ ! -d "$target" ]] ; then
debug "Skipping uninstall of missing $target directory"
return 0
fi
fi
fi
local src
for src in "$source"/* ; do
local name="${src##*/}"
# Skip . and dotfiles- prefixed files
if [[ "$name" == .* ]] || [[ "$name" == dotfiles-* ]] ; then
continue
fi
if [[ "$name" == _* ]] ; then
name="${name#_}"
fi
local target="$2/${name}"
if [[ -L "$src" ]] || [[ ! -d "$src" ]] ; then
link_file "$src" "$target"
else
link_dir "$src" "$target"
fi
done
}
backup_rename() {
local src="$1"
local i=0
local target
while : ; do
target="${src}~${i}"
if [[ ! -e "$target" ]] ; then
break
fi
let i=i+1
done
info "Backup existing ${src} to ${target}"
cmd mv -n "$src" "$target" || \
warn "Backup failed"
}
link_file() {
local source="$1"
local target="$2"
local sourcetarget
if [[ "$MODE" == Install ]] ; then
# Note we have to test for symlink in case the symlink is dead
if [[ -L "$source" ]]; then
local sourcetarget="$( readlink "$source" )"
if [[ "$sourcetarget" = '$HOME/'* ]] ; then
sourcetarget="$HOME/${sourcetarget#'$HOME/'}"
local dirname="$( dirname -- "$source" )"
local basename="$( basename -- "$source" )"
local bouncename="$dirname/.bounce-$basename"
if [[ -L "$bouncename" ]] ; then
local rl="$( readlink "$bouncename" )"
if [[ "$rl" != "$sourcetarget" ]] ; then
debug "Resetting existing bounce: ${bouncename}"
cmd rm "$bouncename" || \
fatal "Could not remove existing bounce link: $bouncename"
cmd ln "$LINK_FLAGS" "$sourcetarget" "$bouncename" || \
fatal "Could not create bounce link: $sourcetarget -> $bouncename"
fi
elif [[ -e "$bouncename" ]] ; then
fatal "Non-link bounce? ${bouncename}"
else
debug "Creating bounce: ${bouncename}"
cmd ln "$LINK_FLAGS" "$sourcetarget" "$bouncename" || \
fatal "Could not create bounce link: $sourcetarget -> $bouncename"
fi
source="$bouncename"
fi
fi
if [[ -L "$target" ]]; then
# Symbolic link, so...
local rl=$( readlink "$target" )
case "$rl" in
"$source")
debug "Already installed: ${target}"
return
;;
"$SCRIPT_DIR"*)
warn "Removing mismatch: ${target} -> ${rl}"
cmd rm -- "$target"
;;
*)
backup_rename "$target"
;;
esac
elif [[ -e "$target" ]]; then
backup_rename "$target"
fi
if [[ ! -e "$target" ]]; then
debug "Linking ${source} -> ${target}"
cmd ln "$LINK_FLAGS" "$source" "$target" || \
fatal "Link creation failed: ${source} -> ${target}"
else
fatal "Target exists: ${source} -> ${target}"
fi
else
if [[ -L "$target" ]] ; then
local rl=$( readlink "$target" )
if [[ "$rl" == "$SCRIPT_DIR"/* ]] ; then
debug "Removing ${target} -> ${source}"
cmd rm -- "$target" ||
fatal "Unable to remove $target"
else
debug "$target symlink exists, but does not point to our dir: $rl"
fi
elif [[ -e "$target" ]] ; then
debug "$target exists, but is not symlink"
else
debug "$target missing"
fi
fi
}
normpath() {
cd -- "$1" && pwd
}
has_installed() {
local pkg
for pkg in "${HAS_INSTALLED[@]}" ; do
if [[ $pkg == "$1" ]] ; then
return 0
fi
done
return 1
}
run_install_script() {
if [[ -e "$1" ]] ; then
info "Running install script $1"
"${SCRIPT_DIR}/lib/run-script" "$1"
else
debug "Install script $1 not found, skipping"
fi
}
install_packages() {
local targetdir="$1"
local needs
local dep
local script
shift
while [[ $# -gt 0 ]] ; do
pkg="$1"
if pkg=$( normpath "$pkg" ) ; then
if [[ -e "$pkg/dotfiles-needs" ]] ; then
needs=
while IFS=$"\n" read -r dep || [[ -n "$dep" ]] ; do
dep=$( normpath "$pkg/$dep" )
if ! has_installed "$dep" ; then
set -- "$dep" "$@"
needs=1
fi
done < "$pkg/dotfiles-needs"
if [[ -n "$needs" ]] ; then
continue
fi
fi
info "${MODE} package $pkg to $TARGET_DIR"
link_dir "$pkg" "$targetdir"
if [[ -z "$DRY_RUN" ]] ; then
for script in "${RUN_SCRIPTS[@]}" ; do
script="${pkg}/dotfiles-script-${script}"
if [[ -x "${script}" ]] ; then
info "Running script ${script}"
rc=0 && "${script}" || rc=$?
if [[ $rc -ne 0 ]] ; then
fatal "Error running script ${script}, exit code $rc"
fi
fi
done
fi
else
warn "Could not normalize path $pkg -- does this exist?"
fi
HAS_INSTALLED=( "${HAS_INSTALLED[@]}" "$pkg" )
shift
done
}
ensure_pkg() {
local pkgmode
local pkg
local vers
local rel
local id
local match
local i
local rv
local state
if [[ -z "$DISTRIB_ID" ]] ; then
if [[ -f /etc/lsb-release ]] ; then
. /etc/lsb-release
else
echo "Unable to determine linux release version" >&2
return 1
fi
fi
OPTIND=0
while [[ "$OPTIND" -lt "$#" ]] ; do
pkg=''
pkgmode=''
vers=()
id=''
rel=''
while getopts "i:r:" opt ; do
case "$opt" in
i)
rel=''
id="$OPTARG"
;;
r)
if [[ -z "$id" ]] ; then
echo "Distribution id must be specified before release" >&2
return 1
fi
rel="$OPTARG"
if [[ -z "$rel" ]] ; then
echo "Release must be non-empty" >&2
return 1
fi
vers=( "${vers[@]}" "$id" "$rel" )
;;
*)
echo "Unknown option chosen ${opt}" >&2
return 1
;;
esac
done
if [[ -z "$rel" ]] ; then
vers=( "${vers[@]}" "$id" "" )
fi
pkgmode="${@:$OPTIND:1}"
(( OPTIND++ ))
pkg="${@:$OPTIND:1}"
(( OPTIND++ ))
if [[ -z "$pkgmode" ]] || [[ -z "$pkg" ]] ; then
echo "Package mode or package name is missing" >&2
return 1
fi
match=0
if [[ "${#vers}" -eq 0 ]] ; then
match=1
else
i=0
while [[ $i -lt ${#vers} ]] ; do
id="${vers[@]:$i:1}"
(( i=i+1))
rel="${vers[@]:$i:1}"
(( i=i+1 ))
if [[ "$id" = "" ]] || [[ "$id" = "$DISTRIB_ID" ]] ; then
if [[ "$rel" = "" ]] || [[ "$rel" = "$DISTRIB_RELEASE" ]] ; then
match=1
break
fi
fi
done
fi
if [[ "$match" = 1 ]] ; then
case "$pkgmode" in
apt)
rv=0 && state="$( dpkg-query --showformat='${db:Status-Status}' -W "$pkg" 2>/dev/null )" || rv=$?
if [[ $rv -eq 0 ]] && [[ "$state" = installed ]] ; then
echo "$pkg already installed"
else
echo "Installing $pkg"
sudo apt install -y "$pkg"
fi
return 0
;;
skip)
echo "Skipping package $pkg"
return 0
;;
*)
echo "Unknown package mode $pkgmode"
return 1
;;
esac
fi
done
echo "Failed to install package (last named $pkg)" >&2
return 1
}
export -f ensure_pkg
while getopts "dvut:s:" opt ; do
case "$opt" in
d)
DRY_RUN=1
warn "Running in dry-run mode"
;;
v) VERBOSE=1 ;;
t) TARGET_DIR="$OPTARG" ;;
u) MODE=Uninstall ;;
s) RUN_SCRIPTS=( "${RUN_SCRIPTS[@]}" "$OPTARG" ) ;;
*) fatal "Unknown option chosen ${opt}" ;;
esac
done
shift $((OPTIND -1))
if [[ $# -gt 0 ]] ; then
PACKAGES=("$@")
else
PACKAGES=("$PACKAGE_DIR"/*)
fi
# info "Updating submodules"
# cmd git submodule update --init
info "Installing dotfiles"
install_packages "$TARGET_DIR" "${PACKAGES[@]}"
info "Done"