-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.sh
executable file
·108 lines (86 loc) · 2.35 KB
/
setup.sh
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
#!/usr/bin/env bash
set -o errexit -o pipefail
# Super basic semi-agnostic package install wrapper to bootstrap a system. Must
# be ran twice to install BOTH user and system packages
# Usage: (install)
# sudo ./setup.sh --system
# ./setup.sh
# Usage: (update)
# sudo ./setup.sh --update --system
# ./setup.sh --update
DEBUG="${DEBUG:-0}"
DRYRUN="${DRYRUN:-0}"
package::foreach() {
filename="${1:?}"
hook="${2:?}"
if [[ ! -f "$filename" ]]; then
log::fatal "File '$filename' does not exist."
fi
log::debug "Reading file '$filename'"
while read -r line || [[ -n "$line" ]]; do
# Skip empty lines
[[ -z "$line" ]] && continue
log::debug "Iterating over line '$line'"
declare -a wrkarr=($line) # Split by space
log::info "Processing package '${wrkarr[*]}'"
util::is_dryrun && continue || true
"$hook" "${wrkarr[@]}" || {
log::debug "Function hook $hook returned non-zero code. Failed on package '${line}'."
log::fatal "Failed to process value '$line' in file '$filename'"
}
done < "$filename"
}
init() {
for f in ./lib/*.sh; do
source "$f"
done
}
main() {
declare -a skip_targets
local package_operation=install
local package_type=user
while [ $# -gt 0 ]; do
case "$1" in
-S|--system)
package_type=system
;;
-U|--update)
package_operation=update
;;
-D|--debug)
DEBUG=1
;;
-U|--dryrun)
DRYRUN=1
;;
--skip-*)
skip_targets+=("${1##--skip-}")
;;
*)
printf '%s\n' "Invalid option '$1'"
exit 128
;;
esac
shift
done
log::info "Performing $package_operation"
prefix_dir="./pkgs/${package_type}"
log::debug "Traversing directory '$prefix_dir'"
for f in "${prefix_dir}"/*; do
declare -a wrkarr
filename="${f##*/}"
wrkarr=($(util::split "$filename" "."))
# [-1] packages
# [-2] <packagemanager>
target="${wrkarr[-2]}"
log::info "Found package list for target '$target'"
if [[ " ${skip_targets[*]} " =~ " $target " ]]; then
log::debug "Skipping target '$target'"
continue
fi
printf -v hook '%s::%s' "$target" "$package_operation"
package::foreach "$f" "$hook"
done
}
init
main "$@"