-
Notifications
You must be signed in to change notification settings - Fork 4
/
start.sh
executable file
·300 lines (242 loc) · 7.11 KB
/
start.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
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
#!/usr/bin/env bash
# https://stackoverflow.com/questions/3822621/how-to-exit-if-a-command-failed
set -eu
set -o pipefail
# a global variable that points to dotfiles root directory.
# it used also in scripts/.
root="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
main_root="${root}"
# shellcheck source=main.sh
source "${root}/scripts/lib/main.sh"
# start.sh
program_name=$0
trap '_end' INT
_end() {
echo "see you in a better tomorrow [you signal start.sh execuation]"
exit
}
_usage() {
echo ""
echo "usage: ${program_name}} [-y] [-h] script [script options]"
echo " -h display help"
echo " -d as dependency (internal usage)"
echo " -y yes to all"
echo ""
echo " ${program_name} new for creating a new script"
echo " ${program_name} list for see available scripts"
echo ""
}
_main() {
## global variables ##
######################
# global variable indicates show help for user in specific script
# there is no need to use it in your script
local show_help=false
# ask no questions, use sane defaults
local yes_to_all=0
# as_dependency shows that this start.sh is going to install a dependency
local as_dependency=false
######################
# parses options flags
while getopts 'dhy' argv; do
case ${argv} in
h)
show_help=true
;;
d)
as_dependency=true
;;
y)
yes_to_all=1
;;
*)
_usage
;;
esac
done
for ((i = 2; i <= OPTIND; i++)); do
shift
done
if [[ ${as_dependency} = false ]]; then
# shellcheck source=header.sh
source "${root}/scripts/lib/header.sh"
fi
# handles root user
if [[ ${EUID} -eq 0 ]]; then
message "pre" "it must run without the root permissions with a regular user." "error"
return 1
fi
# handles given script run and result
local script
local start
local took
# https://stackoverflow.com/questions/7832080/test-if-a-variable-is-set-in-bash-when-using-set-o-nounset
if [[ "${1:+defined}" = "defined" ]]; then
script=$1
shift
else
_usage
script="list"
fi
case ${script} in
"list")
script="lib/list"
;;
"new")
script="lib/new"
;;
"update")
git subtree pull --prefix scripts/lib https://github.com/1995parham/dotfiles.lib.git main --squash
return 0
;;
*) ;;
esac
# shellcheck disable=1090
if ! [[ -f "${root}/scripts/${script}.sh" ]] || ! source "${root}/scripts/${script}.sh" 2>/dev/null; then
message "pre" "404 script not found" "notice"
local host
host="${HOSTNAME}"
host="${host%.*}"
if ! [[ -f "${root}/${host}/scripts/${script}.sh" ]] || ! source "${root}/${host}/scripts/${script}.sh" 2>/dev/null; then
message "pre" "404 script not found for ${host}" "notice"
_usage
return 1
fi
message "pre" "run scirpt for specific host: ${host}" "notice"
root="${root}/${host}"
fi
_run "$@"
local host
host="${HOSTNAME}"
host="${host%.*}"
# shellcheck disable=1090
if ! [[ -f "${root}/${host}/scripts/${script}.sh" ]] || ! source "${root}/${host}/scripts/${script}.sh" 2>/dev/null; then
return 0
fi
message "pre" "run scirpt for specific host: ${host}" "notice"
root="${root}/${host}"
_run "$@"
}
_run() {
start=$(date +%s)
if [[ "${show_help}" = true ]]; then
# prints the start.sh and the script helps
_usage
echo
usage
else
# run the script
msg() { message "${script}" "$@"; }
msg "$(usage)"
# handle dependencies by executing the start.sh
# for each of them separately
if [[ "$(declare -p dependencies 2>/dev/null)" =~ "declare -a" ]]; then
_dependencies "${dependencies[@]}"
fi
run "$@"
# handle additional packages by executing the start.sh
# for each of them separately
if [[ "$(declare -p additionals 2>/dev/null)" =~ "declare -a" ]]; then
_additionals "${additionals[@]}"
fi
fi
echo
took=$(($(date +%s) - start))
printf "done. it took %d seconds.\n" "${took}"
}
_additionals() {
declare -a additionals
additionals=("$@")
if [[ "${#additionals[@]}" -eq 0 ]]; then
return
fi
output=$(echo -n "additionals: |")
output="${output}"$(printf "%s|" "${additionals[@]}")
msg "${output}"
for additional in "${additionals[@]}"; do
read -ra additional <<<"${additional}"
if yes_or_no "${script}" "do you want to install ${additional[0]} as an additional package?"; then
local options="-d"
if [[ "${yes_to_all}" = 1 ]]; then
options="${options}y"
fi
"${main_root}/start.sh" "${options}" "${additional[@]}"
fi
done
}
_dependencies() {
declare -a dependencies
dependencies=("$@")
if [[ "${#dependencies[@]}" -eq 0 ]]; then
return
fi
output=$(echo -n "dependencies: |")
output="${output}"$(printf "%s|" "${dependencies[@]}")
msg "${output}"
if yes_or_no "${script}" "do you want to install dependencies?"; then
local options="-d"
if [[ "${yes_to_all}" = 1 ]]; then
options="${options}y"
fi
for dependency in "${dependencies[@]}"; do
read -ra dependency <<<"${dependency}"
"${main_root}/start.sh" "${options}" "${dependency[@]}"
done
fi
}
run() {
if declare -f pre_main >/dev/null; then
pre_main "$@"
fi
install
if declare -f main >/dev/null; then
main "$@"
fi
if declare -f "main_${USER}" >/dev/null; then
msg " Attention on deck ${USER}"
"main_${USER}" "$@"
fi
}
install() {
if [[ "${OSTYPE}" == "darwin"* ]]; then
msg " darwin, using brew"
if declare -f main_brew >/dev/null; then
main_brew
else
msg "main_brew not found, there is nothing to do" "error"
exit
fi
return
fi
if [[ -n "$(command -v apt)" ]]; then
msg " linux with apt installed, using apt"
if declare -f main_apt >/dev/null; then
main_apt
else
msg "main_apt not found, there is nothing to do" "error"
exit
fi
return
fi
if [[ -n "$(command -v pacman)" ]]; then
msg " linux with pacman installed, using pacman/yay"
if declare -f main_pacman >/dev/null; then
main_pacman
else
msg "main_pacman not found, there is nothing to do" "error"
exit
fi
return
fi
if [[ -n "$(command -v xbps-install)" ]]; then
msg " linux with xbps installed, using xbps"
if declare -f main_xbps >/dev/null; then
main_xbps
else
msg "main_xbps not found, there is nothing to do" "error"
exit
fi
return
fi
}
_main "$@"