-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·618 lines (498 loc) · 15.1 KB
/
install.sh
File metadata and controls
executable file
·618 lines (498 loc) · 15.1 KB
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
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
#!/bin/bash
trap 'clean' EXIT
trap 'exit 130' HUP INT QUIT TERM
shopt -u extglob
typeset -A CFG=(
[aurroot]=/home/aur
[base]=pkg
[exec]="${0##*/}"
[hook]=hooks
[ifs]="$IFS"
[logps]=::
)
typeset -A C=(
[r]=';38;5;1m'
[g]=';38;5;2m'
[y]=';38;5;3m'
[b]=';38;5;4m'
[m]=';38;5;5m'
[c]=';38;5;6m'
)
export PS3='==>'
# TODO: documentation
function err {
printf '%s: %b\n' "${CFG[exec]}" "$2" >&2
(( $1 > 0 )) && exit $1
}
function clean {
printf '\e[?25h' # re-enable cursor in case of `logWait`-interrupt
rm -f ${CFG[logfile]} \
|| err 0 "failed to remove logfile: \e[1m${CFG[logfile]}\e[0m"
if [[ -e ${CFG[aur]} ]]; then
rm -rf ${CFG[aur]} \
|| err 0 "failed to remove aurutils install directory: \e[1m${CFG[aur]}\e[0m"
fi
}
function dep {
typeset -a missing
for dep in "$@"; do
if ! command -v $dep &>/dev/null; then
missing+=($dep)
fi
done
local IFS=,
(( ${#missing[@]} > 0 )) && err 5 "missing dependencies: ${missing[*]}"
}
# -------------------------------------------------------- logging and prompting
function stripTermColor {
for name in "$@"; do
typeset -n n_str=$name
printf -v n_str '%b' "$n_str"
n_str="${n_str//[^[:print:]]/}"
n_str="${n_str//\[[[:digit:]];[[:digit:]][[:digit:]];[[:digit:]];[[:digit:]]m/}"
n_str="${n_str//\[[[:digit:]]m/}"
done
}
function log {
printf '\e[1%s%s\e[0m %b%s' "${C[${1:-c}]}" "${CFG[logps]}" "$2" "${N-$'\n'}" >&2
}
function logInfo {
if [[ -z ${C[$1]} ]]; then
printf ' %b\n' "$1" >&2
else
printf ' \e[1%s->\e[0m %b\n' "${C[$1]}" "$2" >&2
fi
}
# Waits for job to complete. Job status and exit code are checked with `eval`;
# the `eval`-ed values are hardcoded.
function logWait {
typeset msg="$2"
stripTermColor msg
typeset -i lnLen=$(( ${#CFG[logps]} + ${#msg} + 1 ))
N= log $1 "$2"
printf '\e[?25l'
while eval "$3" &>/dev/null; do
printf '\e[K'
for _ in {0..2}; do
read -t 0.2
printf '.'
done
read -t 0.4
printf '\b\b\b'
done
printf '...\n\e[?25h'
# determine job success
eval "$4"
typeset -i x=$?
if (( $x > 0 )); then
printf "\e[A\e[$(( lnLen + 3 ))C \e[1${C[r]}Failed\e[0m\n"
return $x
fi
printf "\e[A\e[$(( lnLen + 3 ))C \e[1${C[g]}Done\e[0m\n"
}
function _prompt {
read -p "$1"
typeset -i x=$?
(( $x > 0 )) && exit $x || return 0
}
function prompt {
typeset pSel=$2 pStr='y/N'
[[ $pSel == y ]] && pStr='Y/n'
typeset prompt
printf -v prompt '\e[1%s%s\e[0m %b [%s] ' "${C[$1]}" "${CFG[logps]}" "$3" "$pStr"
while _prompt "$prompt"; do
[[ -z $REPLY ]] && printf '\e[A%s%s\n' "$prompt" "$pSel"
: ${REPLY:=$pSel}
case ${REPLY,,} in
y) return 0 ;;
n) return 1 ;;
esac
printf '\e[A\e[K'
done
}
# -------------------------------------------------------------- generic utility
function index {
typeset -ig INDEX=0
typeset -n n_arr=$1
for (( i=0; i<${#n_arr[@]}; i++ )); do
INDEX=$i
[[ ${n_arr[$i]} == $2 ]] && return $i
done
INDEX=-1
return -1
}
function arrSet {
typeset -n n_arr=$1
for (( i=0; i<${#n_arr[@]}; i++ )); do
n_arr[$i]="$2"
done
}
function initializeState {
typeset -n n_state=$1 n_arr=$2
for (( i=0; i<${#n_arr[@]}; i++ )); do
n_state[$i]=0
done
}
function suExec {
su -c "$1" || {
typeset -i x=$?
(( $x == 1 )) && err 4 'failed to acquire elevated privileges'
(( $x > 0 )) && err $x "fatal: \e[0${C[r]}`<${CFG[logfile]}`\e[0m"
}
}
# --------------------------------------------------------------- parsing
function parse {
typeset -n n_file=$2 n_dir=$3
typeset dir="${1%/}" trim="$4"
for glob in "$dir"/*; do
if [[ -d $glob ]]; then
glob="${glob#$dir/}"
n_dir+=("$glob")
else
glob="${glob#$dir/}"
n_file+=("${glob%$trim}")
fi
done
}
function parsePkgList {
typeset -n n_core=$1 n_aur=$2
typeset list=$3
typeset -i i=0
log c "Parsing \e[1m$list\e[0m"
while read; do
[[ $REPLY =~ ^\s*$ || $REPLY =~ ^\s*# ]] && continue
# user selection between mutually exclusive packages
if [[ $REPLY =~ '|' ]]; then
typeset -a altList=() altState=()
local IFS='|'
for pkg in $REPLY; do
altList+=("$pkg")
altState+=(0)
done
PS3="\e[1${C[y]}$PS3\e[0m Mutually exclusive packages - select one (^D to confirm):" inplaceSelectOne altList altState
index altState 1
REPLY=${altList[$INDEX]}
log c "\e[1m1\e[0m of \e[1m${#altList[@]}\e[0m packages selected: $REPLY"
fi
REPLY=${REPLY%%#*}
[[ $REPLY =~ :AUR$ ]] && n_aur+=(${REPLY%:AUR}) || n_core+=($REPLY)
let i++
done <"${CFG[base]}/$list.txt"
logWait c "Parsing \e[1m$list\e[0m" 'false'
logInfo "Found \e[1m$i\e[0m package(s) in $list"
(( $i > 0 ))
}
# --------------------------------------------------------------- list selection
function indexSelect {
typeset -n n_state=$1 n_index=$2
typeset input="$3"
# prevent assignment error in case REPLY is non-decimal number
printf '%d' "$input" &>/dev/null && n_index=$input
if (( $n_index <= 0 || $n_index > ${#n_state[@]} )); then
n_index=-1
return 0
fi
let n_index--
(( ${n_state[$n_index]} == 0 )) && n_state[$n_index]=1 || n_state[$n_index]=0
}
function rangeSelect {
typeset -n n_state=$1
typeset range="$2"
typeset -a bounds=("${range%-*}" "${range#*-}")
# needs C-style loop since members may be empty
for (( i=0; i<${#bounds[@]}; i++ )); do
if [[ -z ${bounds[$i]} ]] \
|| ! printf '%d' "${bounds[$i]}" &>/dev/null \
|| (( ${bounds[$i]} <= 0 || ${bounds[$i]} > ${#n_state[@]} )); then
return 0
fi
done
for (( i=${bounds[0]}; i<=${bounds[1]}; i++ )); do
n_state[$(( i-1 ))]=1
done
}
# Accepts input in the form
# INPUT := TOKEN[,TOKEN]... | * | -
# TOKEN := SINGLE | RANGE
# SINGLE := INDEX
# RANGE := INDEX-INDEX
# where 0 < INDEX <= number of items.
#
# RANGE performs an inclusive selection between two indices. The first index
# should be smaller than or equal to the second index.
#
# The two special values "*" and "-" select / deselect all items.
# Malformed inputs result in no-ops.
function _inplaceSelect {
typeset -n n_items=$1 n_state=$2
typeset -i i=1
# print to screen
for (( i=0; i<${#n_items[@]}; i++ )); do
(( ${n_state[$i]} == 0 )) \
&& printf '%d) %s\n' $(( $i+1 )) "${n_items[$i]}" \
|| printf '%d) \e[1%s%s\e[0m%s\n' \
$(( $i+1 )) "${C[b]}" "${PREFIX:-* }" "${n_items[$i]}"
done
printf -v PS3 '%b' "$PS3"
# FIX: return produces additional screen line
read -p "${PS3% } " </dev/tty # executed in nested loop - requires explicit fd
if (( $? == 1 )); then
printf '^D\n'
return 1
fi
# handle selection
typeset -ig SELECTION=0
if (( ${SELECTONE:-0} == 1 )); then
indexSelect ${!n_state} SELECTION "$REPLY"
return 0
fi
if [[ $REPLY == \* ]]; then
arrSet ${!n_state} 1
return 0
elif [[ $REPLY == - ]]; then
arrSet ${!n_state} 0
return 0
fi
# sanitize globbing or otherwise problematic characters characters
while [[ $REPLY =~ [^\\](\*|\?|\[|\'|\") ]]; do
REPLY="${REPLY//"${BASH_REMATCH[1]}"/\\"${BASH_REMATCH[1]}"}"
done
local IFS=,
for field in $REPLY; do
[[ $field =~ - ]] \
&& rangeSelect ${!n_state} "$field" \
|| indexSelect ${!n_state} SELECTION "$field"
done
}
function inplaceSelect {
typeset -n n_items=$1 n_state=$2
typeset -i h=$(( ${#n_items[@]} + 1 ))
log g
while :; do
PS3="$PS3" _inplaceSelect ${!n_items} ${!n_state} || break
printf "\b\e[${h}A\e[J"
done
}
function inplaceSelectOne {
typeset -n n_items=$1 n_state=$2
typeset -i h=$(( ${#n_items[@]} + 1 ))
arrSet ${!n_state} 0
n_state[0]=1
log g
while :; do
SELECTONE=1 PS3="$PS3" _inplaceSelect ${!n_items} ${!n_state} || break
(( $SELECTION >= 0 )) && arrSet ${!n_state} 0 && n_state[$SELECTION]=1
printf "\b\e[${h}A\e[J"
done
(( ${SELECTION:--1} >= 0 )) && arrSet ${!n_state} 0 && n_state[$SELECTION]=1
}
# Updates `pkgSelect` with user selection from package lists in `pkgBase`.
function selectFromBase {
typeset -n n_out=$1 n_sum=$2 n_items=$3 n_state=$4
n_sum+=${#n_items[@]}
index ${!n_items} core
n_state[$INDEX]=1
PS3="\e[1${C[g]}$PS3\e[0m Lists to install (^D to confirm): " inplaceSelect ${!n_items} ${!n_state}
for (( i=0; i<${#n_items[@]}; i++ )); do
(( ${n_state[$i]} == 1 )) && n_out+=("${n_items[$i]}")
done
}
# Updates `pkgSelect` with user selection from package lists in `CFG[base]/$1`.
function selectFromDir {
typeset -n n_out=$1 n_sum=$2
typeset dir=$3
typeset -a pkgLists=() pkgState=()
for glob in ${CFG[base]}/$dir/*; do
glob="${glob#${CFG[base]}/$dir/}"
pkgLists+=("${glob%.txt}")
done
initializeState pkgState pkgLists
n_sum+=${#pkgLists[@]}
index pkgLists core
(( $INDEX != -1 )) && pkgState[$INDEX]=1
PS3="\e[1${C[g]}$PS3\e[0m Lists to install (^D to confirm): " inplaceSelect pkgLists pkgState
if [[ ! ${pkgState[@]} =~ 1 ]]; then
logInfo "No list selected - skipping $dir"
return 1
fi
for (( i=0; i<${#pkgLists[@]}; i++ )); do
(( ${pkgState[$i]} == 1 )) && n_out+=("$dir/${pkgLists[$i]}")
done
}
function selectHooks {
typeset -n n_out=$1 n_hooks=$2 n_hookState=$3
PS3="\e[1${C[g]}$PS3\e[0m Hooks to execute (^D to confirm): " inplaceSelect hooks hookState
for (( i=0; i<${#n_hooks[@]}; i++ )); do
(( ${n_hookState[$i]} == 1 )) && n_out+=("${n_hooks[$i]}")
done
}
# ------------------------------------------------------ installation utilitites
function aurutilsInstall {
if command -v aur &>/dev/null; then
return 0
fi
log y "\e[1maurutils\e[0m not found. Trying to install."
dep git make
CFG[aur]=`mktemp -d -p "${XDG_CACHE_HOME:-$HOME/.cache}" aurutils.XXXXXXXXXX`
read -r -d '' <<-EOF
typeset -A CFG=(${CFG[@]@K})
typeset -A C=(${C[@]@K})
(git clone --depth=1 https://github.com/aurutils/aurutils ${CFG[aur]} &>${CFG[logfile]} \
&& makepkg --dir=${CFG[aur]} -si &>${CFG[logfile]}) &
logWait c 'Installing aurutils' 'kill -0 \$!' 'wait \$!' || exit 4
EOF
suExec "$REPLY"
}
function aurutilsConfigure {
aur repo --list-repo &>/dev/null && return 0
log y 'No local package repository found'
log c "Configuring new repository in ${CFG[aurroot]} ..."
[[ -d ${CFG[aurroot]} ]] && err 4 'target directory already exists'
dep mkdir repo-add
read -r -d '' PACMAN_CONF <<-EOF
[aur]
SigLevel = Optional TrustAll
Server = file://${CFG[aurroot]}/db
EOF
read -r -d '' <<-EOF
printf '\n%s\n' "$PACMAN_CONF" >> /etc/pacman.conf
mkdir -m 777 -p ${CFG[aurroot]}/{db,build}
repo-add ${CFG[aurroot]}/db/aur.db.tar.gz
EOF
log y 'Updating pacman.conf'
suExec "$REPLY"
log r "Remember to export \e[1mAURDEST=${CFG[aurroot]}/build\e[0m for future use"
}
function aurSync {
typeset -n n_pkg=$1
export AURDEST=${CFG[aurroot]}/build AUR_PACMAN_AUTH=su
logWait c "Building ${#n_pkg[@]} packages" 'false'
aur sync --noview -rnC "${n_pkg[@]}" \
|| err 4 'failed to build one or more AUR packages'
}
function install {
typeset -n n_pkg=$1
typeset -i i=0
while read; do
let i++
done < <(pacman --needed -Sp --print-format='%n' "${n_pkg[@]}")
if (( $i == 0 )); then
log c 'All packages already installed, nothing to do'
return 0
fi
(( $i < ${#n_pkg[@]} )) \
&& logInfo "\e[1m$(( ${#n_pkg[@]} - $i ))\e[0m of \e[1m${#n_pkg[@]}\e[0m packages already installed"
read -r -d '' <<-EOF
typeset -A CFG=(${CFG[@]@K})
typeset -A C=(${C[@]@K})
pacman -S --noconfirm ${n_pkg[@]} &>${CFG[logfile]} &
logWait y 'Installing \e[1m$i\e[0m package(s)' 'kill -0 \$!' 'wait \$!' \
|| exit 4
EOF
log y 'Acquiring elevated privileges for package installation:'
suExec "$REPLY"
}
function execHooks {
typeset -n n_items=$1 n_state=$2
typeset -a selected=()
if (( ${#n_items[@]} == 0 )); then
log r "No hooks found - \e[1${C[r]}skipping\e[0m"
return 1
fi
log m "Found \e[1m${#n_items[@]}\e[0m hook(s)"
selectHooks selected ${!n_items} ${!n_state}
if [[ ! ${n_state[@]} =~ 1 ]]; then
log y "No hooks selected - \e[1${C[y]}skipping hook execution\e[0m"
return 1
fi
log c "Running \e[1m${#selected[@]}\e[0m hook(s)..."
for (( i=0; i<${#selected[@]}; i++ )); do
logInfo "hook $(( i+1 )): \e[1m${selected[$i]}\e[0m"
mapfile <${CFG[hook]}/${selected[$i]}
local IFS=
read -r -d '' <<-EOF
typeset -A CFG=(${CFG[@]@K})
typeset -A C=(${C[@]@K})
${MAPFILE[*]}
EOF
bash -c "$REPLY"
done
}
# ------------------------------------------------------------------------------
if (( $UID == 0 )); then
prompt y n '\e[1mWARNING:\e[0m script is not desinged to be run as root - continue?' \
|| err 7 "\e[1${C[r]}aborting\e[0m"
fi
dep mktemp pacman su
# using TMPDIR causes r/w-issues as root (fs.protected_regular)
CFG[logfile]=`mktemp -p "${XDG_CACHE_HOME:-$HOME/.cache}" "${CFG[exec]}".log.XXXXXXXXXX`
log c "Logfile created at \e[1m${CFG[logfile]:-}\e[0m"
log
# export all functions for use in subshells and hooks
mapfile < <(declare -F)
export -f ${MAPFILE[@]#declare -f* }
# ------------------------------------------------------- package list selection
typeset -a pkgBase=() pkgBaseState=()
typeset -a pkgDir=()
typeset -a pkgSelect=() pkgCore=() pkgAur=()
typeset -i listSum=0
parse ${CFG[base]} pkgBase pkgDir '.txt'
initializeState pkgBaseState pkgBase
IFS=,
log m "Found \e[1m${#pkgBase[@]}\e[0m package list(s): ${pkgBase[*]}"
IFS="${CFG[ifs]}"
selectFromBase pkgSelect listSum pkgBase pkgBaseState
IFS=,
if (( ${#pkgDir[@]} == 1 )); then
log
log m "Found \e[1m${#pkgDir[@]}\e[0m package directory: ${pkgDir[*]}"
elif (( ${#pkgDir[@]} > 1 )); then
log
log m "Found \e[1m${#pkgDir[@]}\e[0m package directories: ${pkgDir[*]}"
fi
IFS="${CFG[ifs]}"
for dir in "${pkgDir[@]}"; do
prompt g n "Install packages for \e[1m$dir\e[0m?" || continue
selectFromDir pkgSelect listSum "$dir"
done
if (( ${#pkgSelect[@]} > 0 )); then
log
IFS=,
log c "\e[1m${#pkgSelect[@]}\e[0m of \e[1m$listSum\e[0m list(s) selected: ${pkgSelect[*]}"
IFS="${CFG[ifs]}"
for list in "${pkgSelect[@]}"; do
parsePkgList pkgCore pkgAur "$list"
done
log m "Found \e[1m${#pkgCore[@]}\e[0m packages in \e[1m${#pkgSelect[@]}\e[0m list(s)"
log
(( ${#pkgAur[@]} > 0 )) \
&& log m 'AUR packages will be built and installed after core packages'
read -r -d '' <<-EOF
typeset -A CFG=(${CFG[@]@K})
typeset -A C=(${C[@]@K})
pacman -Sy &>${CFG[logfile]} &
logWait c 'Updating mirrors' 'kill -0 \$!' 'wait \$!' || exit 4
EOF
log y 'Acquiring elevated privileges for mirror update:'
suExec "$REPLY"
install pkgCore
if (( ${#pkgAur[@]} > 0 )); then
log c 'Installing AUR packages...'
aurutilsInstall
aurutilsConfigure
aurSync pkgAur
install pkgAur
fi
else
log y "No lists selected - \e[1${C[y]}skipping package installation\e[0m"
fi
# ------------------------------------------------------ post-installation hooks
log
if prompt g n 'Run post-installation hooks?'; then
typeset -a hooks=() hookState=()
parse ${CFG[hook]} hooks _
initializeState hookState hooks
# FIX: factor out of function -> ugly control flow
execHooks hooks hookState
fi
log c 'All done - cleaning up'