-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
install.sh
executable file
·446 lines (420 loc) · 14.6 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
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
#!/usr/bin/env bash
# shellcheck disable=SC2015,SC2155
# ==================================================================
# install.sh
# ==================================================================
# Bash-Bits Importer Install File
#
# File: install.sh
# Author: Ragdata
# Date: 15/04/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
# ==================================================================
# VARIABLES
# ==================================================================
#
# INSTALLER VERSION
#
export INSTALL_VERSION="0.1.0"
export INSTALL_BUILD="1001"
export INSTALL_BUILD_DATE="20230813:1940"
#
# DEFAULT PATHS
#
[[ -z "${BB_BASE_DIR}" ]] && declare -gx BB_BASE_DIR="$HOME/.bb"
[[ -z "${BB_CACHE_DIR}" ]] && declare -gx BB_CACHE_DIR="${BB_BASE_DIR}/cache"
[[ -z "${BB_LOG_DIR}" ]] && declare -gx BB_LOG_DIR="${BB_BASE_DIR}/log"
[[ -z "${BB_LOG}" ]] && declare -gx BB_LOG="${BB_LOG_DIR}/import"
#
# ANSI VARIABLES
#
declare ANSI_ESC=$'\033'
declare ANSI_CSI="${ANSI_ESC}["
#
# COLOR VARIABLES
#
declare RED="$(printf '%s31m' "$ANSI_CSI")"
declare BLUE="$(printf '%s94m' "$ANSI_CSI")"
declare GREEN="$(printf '%s32m' "$ANSI_CSI")"
declare GOLD="$(printf '%s33m' "$ANSI_CSI")"
declare RESET="$(printf '%s0m' "$ANSI_CSI")"
#
# LOCAL CONFIG VARIABLES
#
[[ -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
# ==================================================================
# ------------------------------------------------------------------
# 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}"; }
#
# 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}"; }
# ------------------------------------------------------------------
# scriptPath
# ------------------------------------------------------------------
# @description Determine the calling script's current path
#
# @noargs
#
# @stdout The calling script's current path
# ------------------------------------------------------------------
scriptPath() { printf '%s' "$(realpath "${BASH_SOURCE[0]}")"; }
# ------------------------------------------------------------------
# install::install
# ------------------------------------------------------------------
install::install()
{
clear
echo
echoGold "=================================================================="
echoGold "INSTALLING BB-IMPORT MODULE"
echoGold "=================================================================="
echo
local url="bb-import"
local location="https://raw.githubusercontent.com/bash-bits/bb-import/master/src/bb-import.sh"
[[ "$1" -eq 1 ]] && installPath="/usr/local/bin/bb-import" || installPath="${BB_BASE_DIR}/bb-import"
local cachePath="${BB_CACHE_DIR}/links/bb-import"
local tmpFile="$cachePath.tmp"
local locFile="${BB_CACHE_DIR}/locations/bb-import"
if [[ ! -d "${BB_CACHE_DIR}" ]]; then
echo "Creating Directories:"
echo " ${BB_CACHE_DIR}"
echo " ${BB_LOG_DIR}"
echo " ${BB_CACHE_DIR}/data"
echo " ${BB_CACHE_DIR}/links"
echo " ${BB_CACHE_DIR}/locations"
mkdir -p "${BB_CACHE_DIR}" "${BB_CACHE_DIR}/data" "${BB_CACHE_DIR}/links" "${BB_CACHE_DIR}/locations" "${BB_LOG_DIR}" || errorReturn "Unable to Create Base Directories!" 2
fi
if [[ -f "$cachePath" ]]; then
echo "Deleting old version ..."
rm -f "$(readlink "$cachePath")"
rm -f "$cachePath"
rm -f "$locFile"
if [[ -f /usr/local/bin/bb-import ]]; then
sudo mv /usr/local/bin/bb-import /usr/local/bin/bb-import~ || errorReturn "Unable to Remove Previous Version!" 3
elif [[ -f "${BB_BASE_DIR}/bb-import" ]]; then
mv "${BB_BASE_DIR}/bb-import" "${BB_BASE_DIR}/bb-import~" || errorReturn "Unable to Remove Previous Version!" 3
fi
fi
# download bb-import
echo "Downloading '$location' -> '$tmpFile'"
install::retry curl -sfLS --netrc-optional --connect-timeout 5 --output "$tmpFile" "$location" || { local r=$?; echoWarning "Failed to download '$location'" >&2; return "$r"; }
# record location in locFile
echo "Recording Location in '$locFile'"
echo "$location" > "$locFile"
# install bb-import
echo "Installing BB-Import to '$installPath'"
if [[ "$1" -eq 1 ]]; then
sudo cp "$tmpFile" "$installPath" || { r=$?; echo "Failed to install BB-Import in '$installPath'"; return "$r"; }
sudo chmod +x "$installPath"
else
cp "$tmpFile" "$installPath" || { r=$?; echo "Failed to install BB-Import in '$installPath'"; return "$r"; }
fi
# calculate file hash
echo "Calculating File Hash"
hash="$(sha1sum < "$tmpFile" | { read -r first rest; echo "$first"; })" || return 1
echo "Calculated hash '$url' -> '$hash'"
hashFile="${BB_CACHE_DIR}/data/$hash"
mv "$tmpFile" "$hashFile" || return 1
# create symlink
echo "Creating Symlink for '$cachePath' -> '$hashFile'"
ln -fs "$hashFile" "$cachePath" || return 1
echo
echoGold "INSTALLATION COMPLETE!"
echoGold "=================================================================="
echo
case "$1" in
1) install::global "${BB_CACHE_DIR}" "$cachePath" "$hashFile";;
2) install::local "${BB_CACHE_DIR}" "$cachePath" "$hashFile";;
esac
}
# ------------------------------------------------------------------
# install::uninstall
# ------------------------------------------------------------------
install::uninstall()
{
echo
echoSuccess "Uninstalling ... " -n
rm -Rf "${BB_BASE_DIR}"
sudo rm -f /usr/local/bin/bb-import
echoSuccess "DONE!"
echo
}
# ------------------------------------------------------------------
# install::quit
# ------------------------------------------------------------------
install::quit()
{
echo
echoGold "Program terminated at user request"
echo
exit 0
}
# ------------------------------------------------------------------
# install::retry
# ------------------------------------------------------------------
install::retry()
{
local exitCode=""
local retryCount=0
local numberRetries="${retries:-5}"
while [ "$retryCount" -lt "$numberRetries" ]
do
[[ "$retryCount" -gt 0 ]] && echo "Retry #$retryCount"
exitCode=0
"$@" || exitCode=$?
[[ "$exitCode" -eq 0 ]] && break
# TODO :: add exponential back-off
sleep 1
retryCount=$(( "$retryCount" + 1 ))
done
return "$exitCode"
}
# ------------------------------------------------------------------
# install::returnQuit
# ------------------------------------------------------------------
install::returnQuit()
{
echo
echoSuccess "Do you want to (R)eturn to the Menu, or (Q)uit? (R/${GOLD}Q${RESET}) " -n
while [[ ! "$RESP" =~ [rRqQ] ]]
do
read -r -n 1 RESP
[[ -z "$RESP" ]] && RESP="Q"
done
echo
case "$RESP" in
r|R)
unset INST
unset RESP
install::menu
;;
q|Q)
install::quit
;;
esac
}
# ------------------------------------------------------------------
# install::global
# ------------------------------------------------------------------
install::global()
{
local cache="$1"
local cacheDir="$(dirname "$cache")"
local cachePath="$2"
local relative="$3"
echoGold "=================================================================="
echoGold "USAGE :: GLOBAL INSTALL"
echoGold "=================================================================="
echo
echoRed "NOTE: YOU ARE GOING TO WANT TO WRITE SOME OF THIS DOWN SOMEWHERE!"
echo
echo "Include the following shebang at the top of every file you want to"
echo "use BB-Import in:"
echo
echoGold "#!/usr/bin/env bb-import"
echo
echo "And import files like so:"
echo
echo "bb-import bb-ansi # IMPLICIT IMPORT of Bash-Bits Module"
echo "bb-import bb-ansi@1.1.0 # IMPLICIT IMPORT of specific version"
echo "bb-import github-org/repo # NAMESPACED IMPORT of a GitHub Repository"
echo "bb-import https://example.com/project # EXPLICIT IMPORT"
echo "bb-import ../../myfile.sh # RELATIVE IMPORT"
echo
echo "And that's really all there is to it."
echo
echo "Important Locations:"
echo " - Cache Directory: '$cacheDir'"
echo " - Cache Path: '$cachePath'"
echo " - Symlink: '$relative'"
echo
echoGold "=================================================================="
}
# ------------------------------------------------------------------
# install::local
# ------------------------------------------------------------------
install::local()
{
local cache="$1"
local cacheDir="$(dirname "$cache")"
local cachePath="$2"
local relative="$3"
echoGold "=================================================================="
echoGold "USAGE :: LOCAL INSTALL"
echoGold "=================================================================="
echo
echoRed "NOTE: YOU ARE GOING TO WANT TO WRITE SOME OF THIS DOWN SOMEWHERE!"
echo
echo "Include the following code at the top of every file you want to"
echo "use BB-Import in:"
echo
echoGold "source ${BB_BASE_DIR}/bb-import"
echo
echo "And import files like so:"
echo
echo "bb-import bb-ansi # IMPLICIT IMPORT of Bash-Bits Module"
echo "bb-import bb-ansi@1.1.0 # IMPLICIT IMPORT of specific version"
echo "bb-import github-org/repo # NAMESPACED IMPORT of a GitHub Repository"
echo "bb-import https://example.com/project # EXPLICIT IMPORT"
echo "bb-import ../../myfile.sh # RELATIVE IMPORT"
echo
echo "And that's really all there is to it."
echo
echo "Important Locations:"
echo " - Cache Directory: '$cacheDir'"
echo " - Cache Path: '$cachePath'"
echo " - Symlink: '$relative'"
echo
echoGold "=================================================================="
}
# ------------------------------------------------------------------
# install::version
# ------------------------------------------------------------------
install::version()
{
echo
echo "Bash-Bits Modular Bash Library"
echoWhite "BB-Import Module Installer ${INSTALL_VERSION}"
echo "Copyright © 2022-2023 Darren (Ragdata) Poulton"
echo "Build: ${INSTALL_BUILD}"
echo "Build Date: ${INSTALL_BUILD_DATE}"
echo
}
# ------------------------------------------------------------------
# install::menu
# ------------------------------------------------------------------
install::menu()
{
clear
echo
echoGold "=================================================================="
echoGold "BASH-BITS CORE INSTALLER MENU"
echoGold "=================================================================="
echo
echo "Install Type:"
echo " 1) Global (/usr/local/bin/bb-import)"
echo " 2) Local (${BB_BASE_DIR}/bb-import)"
echo "Other Options:"
echo " 3) Uninstall"
echo " 4) About"
echo " Q) Quit"
echo
echoSuccess "Type your selection: (${GOLD}1${RESET}/2/3/Q) " -n
while [[ ! "$INST" =~ [123Qq] ]]
do
read -r -n 1 INST
[[ -z "$INST" ]] && INST="1"
done
echo
case "$INST" in
1|2)
install::install "$INST"
;;
3)
install::uninstall
;;
4)
install::version
;;
q|Q)
install::quit
;;
esac
install::returnQuit
}
# ==================================================================
# MAIN
# ==================================================================
install::menu