forked from ragdata-vault/dialog-dojo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.sh
410 lines (378 loc) · 11.8 KB
/
install.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
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
#!/usr/bin/env bash
# ==================================================================
# install.sh
# ==================================================================
# Dialog Dojo Installer
#
# File: install.sh
# Author: Ragdata
# Date: 12/09/2023
# License: MIT License
# Copyright: Copyright © 2023 Darren (Ragdata) Poulton
# ==================================================================
# PREFLIGHT
# ==================================================================
# set debug mode = false
declare -gx INSTALL_DEBUG=0
# if script is called with 'debug' as an argument, then set debug mode
if [[ "${1,,}" == "debug" ]]; then shift; INSTALL_DEBUG=1; set -- "${@}"; set -axeET; else set -aeET; fi
# check git is installed
if ! command -v git &> /dev/null; then
echo "Installing Missing Dependency: Git"
apt install -y git || exit 1
fi
# check dialog is installed
if ! command -v dialog &> /dev/null; then
echo "Installing Missing Dependency: Dialog"
apt install -y dialog || exit 1
fi
# ==================================================================
# VARIABLES
# ==================================================================
if [[ -f /usr/local/lib/dd/setup-vars ]]; then
source /usr/local/lib/dd/setup-vars
elif [[ -f ./src/lib/setup-vars ]]; then
source ./src/lib/setup-vars
else
echo "Unable to Load Critical Library File ... exiting ..."
exit 1
fi
#
# PACKAGE VERSION
#
declare -gx INSTALL_VERSION="0.1.0"
declare -gx INSTALL_BUILD="1001"
declare -gx INSTALL_BUILD_DATE="20230818:0426"
#
# ANSI VARIABLES
#
declare -gx ANSI_ESC=$'\033'
declare -gx ANSI_CSI="${ANSI_ESC}["
#
# COLOR VARIABLES
#
declare -gx RED="$(printf '%s31m' "$ANSI_CSI")"
declare -gx BLUE="$(printf '%s94m' "$ANSI_CSI")"
declare -gx GREEN="$(printf '%s32m' "$ANSI_CSI")"
declare -gx GOLD="$(printf '%s33m' "$ANSI_CSI")"
declare -gx WHITE="$(printf '%s97m' "$ANSI_CSI")"
declare -gx RESET="$(printf '%s0m' "$ANSI_CSI")"
#
# SYMBOLS
#
[[ -z "${SYMBOL_ERROR}" ]] && declare -gx SYMBOL_ERROR="🚫"
[[ -z "${SYMBOL_WARNING}" ]] && declare -gx SYMBOL_WARNING="⚠️"
[[ -z "${SYMBOL_INFO}" ]] && declare -gx SYMBOL_INFO="ℹ️"
[[ -z "${SYMBOL_SUCCESS}" ]] && declare -gx SYMBOL_SUCCESS="✅"
# ==================================================================
# FUNCTIONS
# ==================================================================
# ------------------------------------------------------------------
# checkBash
# ------------------------------------------------------------------
# ------------------------------------------------------------------
checkBash() { if [[ "${BASH_VERSION:0:1}" -lt 4 ]]; then errorReturn "This script requires a minimum Bash version of 4+"; fi; }
# ------------------------------------------------------------------
# checkDeps
# ------------------------------------------------------------------
# @description Checks whether dependencencies listed in arg array are installed on the current system
# ------------------------------------------------------------------
checkDeps()
{
local i
[[ $# -eq 0 ]] && return
[[ ! $(is::array "$1") ]] && errorReturn "'$1' Not an Array!" 1
local -n TOOLS="$1"
for i in "${!TOOLS[@]}"
do
if ! command -v "${TOOLS[$i]}" &> /dev/null; then
errorReturn "ERROR :: Command '${TOOLS[$i]}' Not Found!" 1
fi
done
}
# ------------------------------------------------------------------
# checkRoot
# ------------------------------------------------------------------
# ------------------------------------------------------------------
checkRoot() { if [[ "$EUID" -ne 0 ]]; then errorReturn "This script MUST be run as root!"; fi; }
# ------------------------------------------------------------------
# echoAlias
# ------------------------------------------------------------------
# @description Master alias function for `echo` command
#
# @arg $1 [string] String to be rendered
# @arg -c="$VAR" [option] Color alias as defined above (required)
# @arg -p='string' [option] String to prefix to $1 (optional)
# @arg -s='string' [option] String to suffix to $1 (optional)
# @arg -e [option] Enable escape codes (optional)
# @arg -n [option] Disable newline at end of rendered string (optional)
#
# @exitcode 0 Success
# @exitcode 1 Failure
# @exitcode 2 ERROR - Requires Argument
# @exitcode 3 ERROR - Invalid Argument
# ------------------------------------------------------------------
install::echoAlias()
{
local msg="${1:-}"
local COLOR=""
local OUTPUT=""
local PREFIX=""
local SUFFIX=""
local _0=""
local STREAM=1
local -a OUTARGS
shift
[[ -z "$msg" ]] && { echo "${RED}${SYMBOL_ERROR} ERROR :: install::echoAlias :: Requires Argument!${RESET}"; return 2; }
options=$(getopt -l "color:,prefix:,suffix:,escape,noline" -o "c:p:s:en" -a -- "$@")
eval set --"$options"
while true
do
case "$1" in
-c|--color)
COLOR="$2"
shift 2
;;
-p|--prefix)
PREFIX="$2"
shift 2
;;
-s|--suffix)
SUFFIX="$2"
shift 2
;;
-e|--escape)
OUTARGS+=("-e")
shift
;;
-n|--noline)
OUTARGS+=("-n")
shift
;;
--)
shift
break
;;
*)
echo "${RED}ERROR :: echoAlias ::Invalid Argument '$1'!${RESET}"
return 1
;;
esac
done
[[ -n "$COLOR" ]] && _0="${RESET}" || _0=""
OUTPUT="${COLOR}${PREFIX}${msg}${SUFFIX}${_0}"
[[ "$STREAM" -eq 2 ]] && echo "${OUTARGS[@]}" "${OUTPUT}" >&2 || echo "${OUTARGS[@]}" "${OUTPUT}"
# return 0
}
#
# COLOUR ALIASES
#
echoRed() { install::echoAlias "$1" -c "${RED}" "${@:2}"; }
echoBlue() { install::echoAlias "$1" -c "${BLUE}" "${@:2}"; }
echoGreen() { install::echoAlias "$1" -c "${GREEN}" "${@:2}"; }
echoGold() { install::echoAlias "$1" -c "${GOLD}" "${@:2}"; }
echoWhite() { install::echoAlias "$1" -c "${WHITE}" "${@:2}"; }
#
# MESSAGE ALIASES
#
echoError() { install::echoAlias "$SYMBOL_ERROR $1" -e -c "${RED}" "${@:2}"; }
echoWarning() { install::echoAlias "$SYMBOL_WARNING $1" -e -c "${GOLD}" "${@:2}"; }
echoInfo() { install::echoAlias "$SYMBOL_INFO $1" -c "${BLUE}" "${@:2}"; }
echoSuccess() { install::echoAlias "$SYMBOL_SUCCESS $1" -c "${GREEN}" "${@:2}"; }
errorReturn() { echoError "$1"; return "${2:-1}"; }
#
# EXIT ALIASES
#
exitReturn() { local r="${1:-0}"; [[ "${BASH_SOURCE[0]}" != "${0}" ]] && return "$r" || exit "$r"; }
errorExitReturn() { echoError "$1"; exitReturn "${2:-1}"; }
# ------------------------------------------------------------------
# scriptPath
# ------------------------------------------------------------------
# @description Determine the calling script's current path
#
# @noargs
#
# @stdout The calling script's current path
# ------------------------------------------------------------------
scriptPath() { printf '%s' "$(realpath "${BASH_SOURCE[0]}")"; }
# ------------------------------------------------------------------
# scriptDir
# ------------------------------------------------------------------
# @description Determine the calling script's current directory
#
# @noargs
#
# @stdout The calling script's current directory
# ------------------------------------------------------------------
scriptDir() { printf '%s' "$(dirname "$(realpath "${BASH_SOURCE[0]}")")"; }
# ------------------------------------------------------------------
# install::install
# ------------------------------------------------------------------
install::install()
{
local REPO dir file
local -a dirs=("bin" "cfg" "lib" "opt")
if [[ -f ./README.md ]]; then
REPO="$(scriptDir)"
else
REPO="$(mktemp -d -t dd-XXXXXX)"
# @TODO - Replace with package download once released
git clone git@github.com:ragdata/dialog-dojo "$REPO"
fi
for dir in "${dirs[@]}"
do
case "$dir" in
bin)
while IFS= read -r file
do
install -v -b -C -D -t /usr/local/bin "$file"
done < <(find "$REPO"/src/bin -type f);;
cfg)
while IFS= read -r file
do
install -v -b -C -D -t /opt/dd/cfg "$file"
done < <(find "$REPO"/src/cfg -type f);;
lib)
while IFS= read -r file
do
install -v -b -C -D -t /usr/local/lib/dd "$file"
done < <(find "$REPO"/src/lib -type f);;
opt)
while IFS= read -r file
do
install -v -b -C -D -t /opt/dd "$file"
done < <(find "$REPO"/src/opt -type f);;
esac
done
install::returnQuit
}
# ------------------------------------------------------------------
# install::uninstall
# ------------------------------------------------------------------
install::uninstall()
{
rm -f /usr/local/bin/dojo*
rm -f /usr/local/lib/dd/*
rmdir /usr/local/lib/dd
rm -rf /opt/dd/*
rmdir /opt/dd
install::returnQuit
}
# ------------------------------------------------------------------
# install::returnQuit
# ------------------------------------------------------------------
install::returnQuit()
{
clear
echo
echoSuccess "DONE!"
echoSuccess "Do you want to (R)eturn to the Menu, or (Q)uit? (R/${GOLD}Q${RESET}) " -n
while [[ ! "${RESP,,}" =~ [rq] ]]
do
read -r -n 1 RESP
[[ -z "$RESP" ]] && RESP="Q"
done
echo
case "${RESP,,}" in
r)
unset INST
unset RESP
install::menu
;;
q)
install::quit
;;
esac
}
# ------------------------------------------------------------------
# install::quit
# ------------------------------------------------------------------
install::quit()
{
echo
echoGold "Program terminated at user request"
echo
# tput cnorm
exit 0
}
# ------------------------------------------------------------------
# install::version
# ------------------------------------------------------------------
# @description Reports the version and build date of this release
#
# @noargs
#
# @stdout Version, Copyright, & Build Information
# ------------------------------------------------------------------
install::version()
{
local verbosity="${1:-}"
if [[ -z "$verbosity" ]]; then
echo "${INSTALL_VERSION}"
else
echo
echo "Dialog Dojo Installer"
echoWhite "Dialog Dojo Installer ${INSTALL_VERSION}"
echo "Copyright © 2022-2023 Darren (Ragdata) Poulton"
echo "Build: ${INSTALL_BUILD}"
echo "Build Date: ${INSTALL_BUILD_DATE}"
echo
fi
}
# ------------------------------------------------------------------
# install::menu
# ------------------------------------------------------------------
install::menu()
{
local option status
option=$(dialog --title "INSTALLER MENU" --backtitle "DIALOG DOJO INSTALLER" --clear --default-item "1" --menu "Select from the following options:" 15 50 5 \
"Install" "" \
"Uninstall" "" \
"Update" "" \
"About" "" \
"Quit" "" 3>&1 1>&2 2>&3)
status=$?
if [[ "$status" == "$DIALOG_OK" ]]; then
case "$option" in
"Install") install::install
install::returnQuit;;
"Uninstall") install::uninstall;;
"Update") install::install;;
"About") install::version verbose;;
"Quit") exit 0;;
esac
fi
}
# ==================================================================
# MAIN
# ==================================================================
checkBash
checkRoot
options=$(getopt -l "version::" -o "v::" -a -- "$@")
eval set --"$options"
while true
do
case "$1" in
-v|--version)
if [[ -n "$OPTARG" ]]; then
install::version "$OPTARG"
shift 2
else
install::version
shift
fi
exit 0
;;
--)
shift
# tput civis
install::menu
# tput cnorm
break
;;
*)
echoError "Invalid Argument!"
exit 2
;;
esac
done