-
Notifications
You must be signed in to change notification settings - Fork 1
/
prelude.sh
163 lines (136 loc) · 5.5 KB
/
prelude.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
#!/bin/bash -x
# TODO make this into the library so that the
# testing of distro version can be used in if/else
if [[ "${__INITPC_PRELUDE_SOURCED__}" != "true" ]]; then
__INITPC_PRELUDE_SOURCED__='true'
# TODO toread:
# https://linuxopsys.com/topics/customizing-bash-prompt-in-linux-changing-colors
# https://gist.github.com/vratiu/9780109
# https://tldp.org/HOWTO/Bash-Prompt-HOWTO/x329.html
# https://opensource.com/article/19/9/linux-terminal-colors
# https://linuxopsys.com/topics/customizing-bash-prompt-in-linux-changing-colors
# https://www.cyberciti.biz/faq/bash-shell-change-the-color-of-my-shell-prompt-under-linux-or-unix/
# https://unix.stackexchange.com/questions/148/colorizing-your-terminal-and-shell-environment
# https://dev.to/ifenna__/adding-colors-to-bash-scripts-48g4
set -e # exit on error
set -x # debug logging
# TODO try removing -x option from scripts that include this
# shellcheck disable=SC2034
readonly RED='\033[0;31m'
# shellcheck disable=SC2034
readonly IYELLOW='\033[0;93m' # high intensity yellow
# shellcheck disable=SC2034
readonly GREEN='\033[0;32m'
# shellcheck disable=SC2034
readonly NC='\033[0m' # No Color
# makes the echo prompt yellow to improve readability
export PS4="\[${IYELLOW}\]+ \[${NC}\]"
# TODO renumber the exit codes according to the docs bellow
# SEE: https://manpages.ubuntu.com/manpages/lunar/man3/sysexits.h.3head.html
# SEE: https://tldp.org/LDP/abs/html/exitcodes.html
# shellcheck disable=SC2034
readonly EXIT_SUCCESS=0
# shellcheck disable=SC2034
readonly EXIT_FILE_IO_ERROR=50
# TODO - consider even better error code number (guaranteed to be unique)
# apt-get install returns 100 if a package was not found... 100 is not a good error code. >IMPORTANT<
# shellcheck disable=SC2034
readonly EXIT_INCORRECT_PLATFORM=99
# shellcheck disable=SC2034
readonly EXIT_INVALID_ARGUMENT=10
# TODO - finish
#
# @param $1 - distro ID
function distro_is
{
# TODO test the test... and make sure it is correct/valid/general enough
# TODO see:
# https://www.freedesktop.org/software/systemd/man/os-release.html
# https://0pointer.de/blog/projects/os-release.html
# https://manpages.ubuntu.com/manpages/focal/man5/os-release.5.html
# https://docs.oracle.com/cd/E88353_01/html/E37852/os-release-5.html
# https://github.com/chef/os_release
# https://man.archlinux.org/man/os-release.5.en
# https://manpages.debian.org/testing/systemd/os-release.5.en.html
# https://www.commandlinux.com/man-page/man5/os-release.5.html
# https://unix.stackexchange.com/questions/351557/on-what-linux-distributions-can-i-rely-on-the-presence-of-etc-os-release
if [[ ! -f /etc/os-release ]]; then
echo "/etc/os-release file not found."
echo "Aborting."
exit "${EXIT_INCORRECT_PLATFORM}"
fi
local ID
# shellcheck disable=SC1091
ID=$(source /etc/os-release && echo "${ID}")
echo "Distro ID=${ID}"
if [[ "${ID}" != "$1" ]]; then
echo "Error: The distro ID is ${ID} but it should be $1! Aborting."
exit "${EXIT_INCORRECT_PLATFORM}"
fi
}
function get_major_distro_version
{
if [[ ! -f /etc/os-release ]]; then
echo "/etc/os-release file not found."
echo "Aborting."
exit "${EXIT_INCORRECT_PLATFORM}"
fi
local VERSION_ID
# shellcheck disable=SC1091
VERSION_ID="$(source /etc/os-release && echo "${VERSION_ID}")"
if [[ ${VERSION_ID} == "" ]]; then
return
fi
echo "${VERSION_ID}" | grep -o "^[0-9]\+"
}
# @param $1 - minimal distro major version
function distro_version_ge
{
local MAJOR_VERSION
if ! MAJOR_VERSION="$(get_major_distro_version)"; then
exit "${EXIT_INCORRECT_PLATFORM}"
fi
# An empty VERSION_ID is interpretted as the (positive) infinity.
if [[ "${MAJOR_VERSION}" == "" ]]; then
echo "Warning: The version ID is empty."
return
fi
if [[ "${MAJOR_VERSION}" -lt "$1" ]]; then
echo "Error: Major distro version is ${MAJOR_VERSION} but it should be at least $1! Aborting."
exit "${EXIT_INCORRECT_PLATFORM}"
fi
}
function distro_version_le
{
local MAJOR_VERSION
if ! MAJOR_VERSION="$(get_major_distro_version)"; then
exit "${EXIT_INCORRECT_PLATFORM}"
fi
# An empty VERSION_ID is interpretted as the (positive) infinity.
if [[ "${MAJOR_VERSION}" == "" ]]; then
echo "Warning: The version ID is empty."
exit "${EXIT_INCORRECT_PLATFORM}"
fi
if [[ "${MAJOR_VERSION}" -gt "$1" ]]; then
echo "Error: Major distro version is ${MAJOR_VERSION} but it should be at most $1! Aborting."
exit "${EXIT_INCORRECT_PLATFORM}"
fi
}
function gnome_present
{
local GNOME_PRESENT="no"
if gnome-extensions --version; then
GNOME_PRESENT="yes"
echo "gnome-extensions executable found."
echo "Assuming that GNOME is present"
fi
if [[ ${GNOME_PRESENT} != "$1" ]]; then
echo "GNOME_PRESENT != $1. Aborting."
exit "${EXIT_INCORRECT_PLATFORM}"
fi
}
print0()
{
[ "$#" -eq 0 ] || printf '%s\0' "$@"
}
fi