-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbootstrap
executable file
·580 lines (464 loc) · 16.7 KB
/
bootstrap
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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
#!/usr/bin/env bash
set -e
# -- variables we need to download source files and create a smog package -- #
# -- note: make sure we don't overwrite those, when we source 'config.sh' -- #
BOOTSTRAP=bootstrap
# target smog version
REF=0.0.1
MODE=tag
# it's also possible to bootstrap in branch mode, but highly discouraged
# you could do that by uncommenting these two lines
#REF=master
#MODE=branch
SMOGHOST=github.com
SMOGPATH=vgratian/smog
SMOGPKG="\$PKG/$SMOGHOST/$SMOGPATH" # local repo path (with unexpanded $PKG)
SMOGURL="https://$SMOGHOST/$SMOGPATH" # git repo url (excludes trailing .git/)
SMOGROOT= # will store unexpanded $ROOT
# some file names
CONFIG=config
CONFIGDEF=config-def
BASHCOMPL=bash-completion
RAWCONTENT_URL=https://raw.githubusercontent.com/$SMOGPATH
BOOTSTRAP_URL="$RAWCONTENT_URL/master/$BOOTSTRAP" # latest version
CONFIGDEF_URL="$RAWCONTENT_URL/$REF/$CONFIGDEF" # should match target version
BOOTSTRAP_PATH= # directory for temporary files
# short guide about interactive bootstrapping
INTERACTIVE="you will be asked what parameters to use
-> hit ENTER to use the default value [in square brackets]
-> enter double quotes (\"\") to unset or use empty string
-> value can contain bash commands or environment variables
-> find detailed explanation at: $SMOGURL#config"
# variables required to configure smog (i.e. create the file $CONFIG)
# default values will be read from default config file $CONFIGDEF
VARS=( GIT EDITOR ROOT PKG BIN LIB SMOG MDD LOCALBRANCH NPROCS BASHRC LDSOCONF )
# short comment for each variable (more detailed explanation in README#config)
declare -A COMMENTS=(
[GIT]="git command"
[EDITOR]="text editor command"
[ROOT]="root directory for folders defined below"
[PKG]="directory for cloning and storing repositories"
[BIN]="directory for creating symlinks to binaries and executables"
[LIB]="directory for creating to shared libraries"
[SMOG]="directory where smog itself is cloned (can't be modified)"
[MDD]="directory for smog metadata files"
[LOCALBRANCH]="name of the local working branch"
[NPROCS]="number of processes to use when syncing packages"
[BASHRC]="bashrc filepath (in \$ROOT): to update PATH with \$BIN and add smog autocompletion"
[LDSOCONF]="ldconfig filepath (absolute): to allow \$LIB to be cached by ldconfig"
)
# comments for $BASHRC (if we edit it)
EXPORT_PATH_COMMENT="# directory of binary symlinks maintained by smog"
SOURCE_COMP_COMMENT="# autocomplete script for smog commands and packages"
# terminal colors
BOLD=
GREY=
CLEAR=
if tput colors > /dev/null 2>&1; then
BOLD="\033[1m"
CLEAR="\033[0m"
GREY="\033[90m"
fi
version() {
echo "bootstrapper for smog '$MODE $REF'"
}
usage() {
version
cat << EOF
DESCRPTION:
This script will configure and install smog. The bootstrapper will run
interactively and ask what parameters to use. Use the 'silent' flag to
run silently and use default values.
With the 'undo' argument, the script will uninstall smog. CAUTION:
this will permanently delete not only smog, but also all packages that
you might have created with it.
USAGE:
Download the bootstrap script:
wget -nv $BOOTSTRAP_URL
Run the bootstrap:
bash $BOOTSTRAP
Or bootstrap with less questions:
bash $BOOTSTRAP silent
Undo the bootstrap:
bash $BOOTSTRAP undo
Show target version:
bash $BOOTSTRAP version
Show this help message:
bash $BOOTSTRAP help
EOF
}
abort() {
echo "${1-abort}" >&2
exit 1
}
# clean up bootstrap directory
cleanup() {
printf "\ncleaning up\n%b" $GREY
# i'm paranoic
[ "${BOOTSTRAP_PATH::5}" == /tmp/ ] && [ -d "$BOOTSTRAP_PATH" ] \
&& rm -rvf "$BOOTSTRAP_PATH"
printf "%b\n" $CLEAR
}
# read default config and create a new config file
configure() {
# download default config from remote
printf "%b" $GREY
wget -nv "$CONFIGDEF_URL" || abort
printf "%b" $CLEAR
test -r "$CONFIGDEF" || abort
printf "downloaded '%s'\n" "$CONFIGDEF_URL"
touch "$CONFIG"
printf "\n%b" $BOLD
if [ -z "$INTERACTIVE" ]; then
printf "configuring smog with default parameters\n%b" $CLEAR
else
printf "configuring smog interactively:%b\n%s\n" $CLEAR "$INTERACTIVE"
fi
# read default value of each variable and write into new config file
for v in "${VARS[@]}"; do
# unexpanded default value
val=$(grep ^$v= $CONFIGDEF | tail -1 | cut -d= -f2-)
if [ "$INTERACTIVE" ]; then
# print comment about parameter
printf "\n%b%b%s:%b%b " "$GREY" "$BOLD" "$v" "$CLEAR" "$GREY"
printf "${COMMENTS[$v]}"
printf "%b\n" "$CLEAR"
# ask user input
if [ "$v" == SMOG ]; then
read -p "value of $v [$val]: (enter to continue)"
else
read -p "value for $v [$val]: "
[ -n "$REPLY" ] && val="$REPLY"
fi
fi
# write to config file
printf "# %s\n" "${COMMENTS[$v]}" >> $CONFIG
printf "%s=%s\n\n" "$v" "$val" >> $CONFIG
done
echo
echo "created config '$BOOTSTRAP_PATH/$CONFIG'"
echo "please review and hit ENTER to continue"
read && echo
}
# check if variables are OK
validate() {
local ok=ok
echo "validating $CONFIG.. "
. "$BOOTSTRAP_PATH/$CONFIG"
# TODO check bash version
echo -n "checking GIT [$GIT].. "
command -v $GIT > /dev/null && echo "OK" || ok=
echo -n "checking EDITOR [$EDITOR].. "
command -v $EDITOR > /dev/null && echo "OK" || ok=
# check variables which can't be empty
echo "checking required variables.. "
for v in ROOT PKG SMOG MDD LOCALBRANCH NPROCS; do
if [ -z "${!v}" ]; then
echo " -> '$v' can't be empty"
ok=
fi
done
# nprocs must be unsigned integer
if ! ( test "$NPROCS" -ge 0 ) 2> /dev/null; then
echo " -> 'NPROCS' must be 0 or positive integer"
ok=
fi
# PKG should not contain subdirectories (avoid messing up directories later)
if [ -d "$ROOT/$PKG" ] && ls -F "$ROOT/$PKG" | grep -q /$; then
echo " -> '$ROOT/$PKG' should not contain subdirectories"
ok=
fi
# SMOG should not be changed
local smogpkg=$(grep '^SMOG=' "$BOOTSTRAP_PATH/$CONFIG" | cut -d= -f2-)
[ ${smogpkg::1} == '"' ] && smogpkg="${smogpkg:1:-1}"
if [ "$smogpkg" != "$SMOGPKG" ]; then
echo " -> 'SMOG' value is '$smogpkg', but expected '$SMOGPKG'"
ok=
fi
# SMOG should not exist or be empty
if [ -d "$ROOT/$SMOG" ] && [ $(ls "$ROOT/$SMOG" | wc -l) -ne 0 ]; then
echo " -> '$ROOT/$SMOG' should not exist or be empty"
ok=
fi
# store unexpanded $ROOT for later use in bashrc
SMOGROOT=$(grep '^ROOT=' "$BOOTSTRAP_PATH/$CONFIG" | cut -d= -f2-)
# to be safe
if [ -z "$SMOGROOT" ]; then
SMOGROOT="$ROOT"
elif [ ${SMOGROOT::1} == '"' ] && [ ${SMOGROOT: -1} == '"' ]; then
SMOGROOT="${SMOGROOT:1:-1}"
fi
test -z $ok && abort || printf "OK\n\n"
}
filesystem_create() {
echo "creating filesystem"
for d in BIN LIB PKG SMOG; do
[ -z "${!d}" ] && continue
[ -d "$ROOT/${!d}" ] && continue # TODO warn?
mkdir -vp "$ROOT/${!d}"
done
cd "$ROOT/$SMOG"
printf "changed directory to '%s'\n" "$ROOT/$SMOG"
printf "cloning repository '%s'\n" "$SMOGURL"
printf "%b" $GREY
# from here on, we simulate how smog would create a package
$GIT clone --depth=1 -c advice.detachedHead=false -b "$REF" "$SMOGURL.git/" .
# get current head SHA if in branch mode
if [ $MODE == branch ]; then
SHA=$( $GIT log origin/$REF -1 --pretty=format:%H )
fi
$GIT checkout -b "$LOCALBRANCH"
echo -e "$CLEAR"
# copy config file we created
mv -v "$BOOTSTRAP_PATH/$CONFIG" .
# create metadata directory and metadata file for smog
mkdir -vp "$ROOT/$MDD"
local md=("url: $SMOGURL.git/" "path: $SMOGHOST/$SMOGPATH" "mode: $MODE" "ref: $REF")
[ $MODE == branch ] && md+=("sha: $SHA")
printf '%s\n' "${md[@]}" > "$ROOT/$MDD/smog"
printf "created metadata file '%s'\n\n" "$ROOT/$MDD/smog"
}
filesystem_remove() {
echo "removing filesystem"
local d
# sanity check
for d in ROOT SMOG MDD; do
if [ -z "${!d}" ] && [ ! -d "${!d}" ]; then
abort "variable '$d' is empty or invalid"
fi
done
# remove smog metadata (should be the only file in $MDD)
rm -v "$ROOT/$MDD/smog"
rm -vd "$ROOT/$MDD"
rm -rf "$ROOT/$SMOG" && echo "removed '$ROOT/$SMOG'"
rm -rf "$ROOT/$PKG" && echo "removed '$ROOT/$PKG'"
# remember: these can be shared with other programs
for d in LIB BIN; do
[ -z "${!d}" ] && continue
if [ ! -d "${!d}" ] || [ -z "$(ls -A $ROOT/${!d})" ]; then
rm -vd "$ROOT/${!d}"
else
echo "skipped non-empty '$ROOT/${!d}'"
fi
done
}
bash_integrate() {
[ -z "$BIN" ] && return
printf "symlink "
ln -sv "$ROOT/$SMOG/smog" "$ROOT/$BIN/smog"
printf "smog: smog\n" > "$ROOT/$MDD/smog.bin"
printf "created metadata file '%s'\n\n" "$ROOT/$MDD/smog.bin"
if [ -z "$BASHRC" ]; then
echo "'\$BASHRC' is not defined, you can manually integrate smog to your bash:"
echo " -> update your PATH to include '$SMOGROOT/$BIN'"
echo " -> source autocompletion script '$SMOGROOT/$SMOG/$BASHCOMPL'"
return
fi
local changed= line= export_path="export PATH=\"$SMOGROOT/$BIN:\$PATH\""
# check if bashrc already contains $BIN
if grep '^export PATH=' "$ROOT/$BASHRC" 2>/dev/null | grep -qE "/$BIN[:\"]"; then
echo "seems like '$BASHRC' already contains path '$ROOT/$BIN'"
# put after last 'export PATH...' line
else
line=$( grep -n '^export PATH' "$ROOT/$BASHRC" 2>/dev/null | tail -1 | cut -sd: -f1 )
if [ "$line" -gt 0 2>/dev/null ]; then
((line+=1))
sed -i \'"$line" i "$export_path"\' "$ROOT/$BASHRC"
sed -i \'"$line" i "$EXPORT_PATH_COMMENT"\' "$ROOT/$BASHRC"
echo "updated \$PATH in '$SMOGROOT/$BASHRC' at $line"
else
echo "$EXPORT_PATH_COMMENT" >> "$ROOT/$BASHRC"
echo "$export_path" >> "$ROOT/$BASHRC"
echo "updated \$PATH in '$SMOGROOT/$BASHRC'"
fi
changed=yes
fi
local source_comp=". \"$SMOGROOT/$SMOG/$BASHCOMPL\""
if complete -p > /dev/null 2>&1; then
echo "$SOURCE_COMP_COMMENT" >> "$ROOT/$BASHRC"
echo "$source_comp" >> "$ROOT/$BASHRC"
echo "added autocompletion script in '$SMOGROOT/$BASHRC'" \ &&
changed=yes
else
echo "skipped autocompletion script: does your bash support it?"
fi
if [ -n $changed ]; then
printf "%b" $BOLD
printf "tip: run '. %s' to update your bash session\n" "$BASHRC"
printf "%b" $CLEAR
fi
echo
}
# remove bootstrapper additions to bashrc
bash_cleanup() {
[ -z "$BIN" ] && return
# remove symlink to smog
if [ -L "$ROOT/$BIN/smog" ] && [ "$(readlink "$ROOT/$BIN/smog")" == "$ROOT/$SMOG/smog" ]; then
unlink "$ROOT/$BIN/smog"
echo "unlinked '$ROOT/$BIN/smog'"
rm -vf "$ROOT/$MDD/smog.bin"
fi
[ -z "$BASHRC" ] && return
# unexpanded ROOT
local root=$(grep '^ROOT=' "$ROOT/$SMOG/$CONFIG" | tail -1 | cut -sd= -f2- )
if [ -z "$root" ]; then
root="$ROOT"
elif [ "${root::1}" == '"' ] && [ "${root: -1}" == '"' ]; then
root="${root:1:-1}"
fi
local line= rmbin=
# remove $BIN from $PATH
if [ -d "$ROOT/$BIN" ] && [ -n "$(ls -A $ROOT/$BIN)" ]; then
echo "directory '$root/$BIN' not empty"
else
line=$(grep -n "^export PATH=\"$root/$BIN:\$PATH\"$" "$ROOT/$BASHRC" | tail -1 | cut -sd: -f1 )
if [ "$line" -gt 0 2>/dev/null ]; then
sed -i "$line d" "$ROOT/$BASHRC" && rmbin=yes
echo "removed \$BIN from \$PATH in '$root/$BASHRC' at $line"
((line-=1))
# remove comment as well
if [ "$(head -$line $ROOT/$BASHRC | tail -1 )" == "$EXPORT_PATH_COMMENT" ]; then
sed -i "$line d" "$ROOT/$BASHRC"
fi
fi
fi
[ -z $rmbin ] && echo "did not remove \$BIN from \$PATH in '$root/$BASHRC'"
complete -p > /dev/null 2>&1 || return 0
# remove autocomplete script
local source_comp=". \"$root/$SMOG/$BASHCOMPL\""
line=
line=$(grep -n "^$source_comp$" "$ROOT/$BASHRC" | tail -1 | cut -sd: -f1 )
if [ "$line" -gt 0 2>/dev/null ]; then
sed -i "$line d" "$ROOT/$BASHRC"
echo "removed autocompletion script in '$root/$BASHRC' at $line"
((line-=1))
# remove comment
if [ "$(head -$line $ROOT/$BASHRC | tail -1 )" == "$SOURCE_COMP_COMMENT" ]; then
sed -i "$line d" "$ROOT/$BASHRC"
fi
fi
}
ldconf_integrate() {
[ -z "$LIB" ] && return
if [ -z "$LDSOCONF" ]; then
echo "'\$LDSOCONF' unset, but smog can link sharedlibs in '$SMOGROOT/$LIB'"
echo "you can manually configure ldconfig to cache this directory."
return
fi
echo "integrating '$SMOGROOT/$LIB' to ldconfig"
local fn=$(basename $LDSOCONF)
local dest=$(dirname $LDSOCONF)
if ! ldconfig --version > /dev/null 2>&1; then
echo "skip: seems like you don't use ldconfig"
return
fi
if [ ! -d "$dest" ]; then
echo "skip: directory '$dest' does not exist"
return
fi
echo "$ROOT/$LIB" > $fn
# test if we have permission to write in $dest
if ( >> "$dest/test" ) 2> /dev/null; then
mv -v "$fn" "$dest"
else
echo "sudo required to create '$LDSOCONF'"
read -p 'continue? [y/N]: '
if [ "$REPLY" != y ] && [ "$REPLY" != yes ]; then
echo "you can manually configure ldconfig to cache '$SMOGROOT/$LIB'"
return
fi
sudo mv -v "$fn" "$dest"
fi
if [ $? -eq 0 ]; then
printf "%b" $BOLD
printf "tip: after 'smog link PKG', run 'sudo ldconfig' to update ld cache"
printf "%b\n\n" $CLEAR
fi
}
ldconf_cleanup() {
[ -z "$LIB" ] || [ -z "$LDSOCONF" ] && return
if [ -d "$ROOT/$LIB" ] && [ -n "$(ls -A $ROOT/$LIB)" ]; then
echo "not removed '$LDSOCONF', since '$ROOT/$LIB' is not empty"
return
fi
if ! rm -v "$LDSOCONF" 2>/dev/null; then
echo "sudo required to remove '$LDSOCONF'"
read -p 'continue? [y/N]: '
if [ "$REPLY" == y ] || [ "$REPLY" == yes ]; then
sudo rm -v "$LDSOCONF"
fi
fi
}
bootstrap() {
BOOTSTRAP_PATH=$(mktemp -d /tmp/smogXXXX)
trap cleanup EXIT
cd "$BOOTSTRAP_PATH"
echo "changed directory '$BOOTSTRAP_PATH'"
configure && validate
filesystem_create
bash_integrate
ldconf_integrate
cleanup
trap - EXIT
printf "%b" $BOLD
printf "successfully bootstrapped smog\n"
printf "%b" $CLEAR
echo " -> run 'smog sync smog' to check for new version of smog"
echo " -> run 'smog update smog' to update smog to a new version"
echo " -> run 'bash bootstrap undo' to remove smog"
}
remove_pkgs() {
local -a pkgs=$( ./smog list | grep -v '^smog$' )
[ -z "${pkgs[0]}" ] && return
read -p "remove ${#pkgs[@]} smog package(s)? [y/N]: "
[ "$REPLY" != y ] && [ "$REPLY" != yes ] && abort
local pkg
local -i count
for pkg in "${pkgs[@]}"; do
./smog remove "$pkg" -f && ((count+=1))
done
printf "%b" $BOLD
printf "removed %d smog packages\n\n" "$count"
printf "%b" $CLEAR
}
unbootstrap() {
./smog version > /dev/null 2>&1 || abort "please run this script in smog directory"
echo -en "$BOLD"
echo "WARNING: This will remove smog and"
echo " all packages you have created with smog"
echo " all package metadata"
echo " any local changes in package repositories"
echo " any symlinks created by smog"
echo -e "$CLEAR"
read -p "continue? [y/N]: "
[ "$REPLY" == y ] || [ "$REPLY" == yes ] || abort
read -p "type 'I am sure' to continue: "
[ "$REPLY" == 'I am sure' ] || abort
. ./$CONFIG || abort "can't source '$CONFIG'"
# uninstall and unlink all pkgs
remove_pkgs
# remove our traces in bash environment and ldconfig
bash_cleanup
ldconf_cleanup
# remove files and directories created by bootstrap
filesystem_remove
printf "%b" $BOLD
printf "successfully removed smog from your system\n"
printf "%b" $CLEAR
}
# reject to run as root user
[ "$EUID" -eq 0 ] && abort "you can't run this as root"
[ $(id -u) -eq 0 ] 2>/dev/null && "you can't run this with sudo"
case $1 in
help | -h | -help | --help | usage )
usage ;;
version )
version ;;
undo )
unbootstrap ;;
silent )
INTERACTIVE=
bootstrap ;;
* )
[ -z "$1" ] && bootstrap || abort "invalid argument: $1" ;;
esac