-
Notifications
You must be signed in to change notification settings - Fork 0
/
init
62 lines (54 loc) · 1.61 KB
/
init
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
if shellm-ndef; then
shellm-define "
init
check
shellm_activate_check
shellm_deactivate_check
have_command
have_package"
## \brief Old functions, DO NOT USE.
## @fn void init (void)
## @brief Initialize environment variables for shellm scripts
init() {
if ! echo "$*" | grep -q 'no-data'; then
# shellcheck disable=SC2154
DATADIR="${shellm}/usr/data/${BASH_SOURCE[0]##*/}"
mkdir -p "${DATADIR}" 2>/dev/null
fi
}
## @fn void check (file)
## @brief Check if packages/executables in file are installed,
## or in $0 if no file given (then ask to continue or not if some are missing)
## @param [$1] File to check (default $0)
## @return Echo: missing packages/executables, question if $0
check() {
[ "$shellm_ignore_check" = "yes" ] && return 0
local s ret=true a=${1:-$0}
for s in $(get_packages "$a"); do
have_package "$s" || { err "Package $s not found !"; false; }
ret=$( ([ $? -eq 0 ] && $ret) && echo true || echo false)
done
for s in $(get_depends "$a"); do
have_command "$s" || { err "Executable $s not found !"; false; }
ret=$( ([ $? -eq 0 ] && $ret) && echo true || echo false)
done
if [ -z "$1" ] && ! $ret; then
err; err "Some required packages and/or executables were not found..."
question "Would you like to continue anyway ? (unpredictable behavior) [yN]" || final 0
line "-"
fi
}
shellm_activate_check() {
export shellm_ignore_check=no
}
shellm_deactivate_check() {
export shellm_ignore_check=yes
}
have_command() {
command -v "$1" >/dev/null
}
have_package() {
# shellcheck disable=SC2016
dpkg-query -W -f '${Package}\n' | /bin/grep -wq "$1"
}
fi # __CORE_UTILS_INIT_SH