-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathprelude.sh
More file actions
executable file
路218 lines (188 loc) 路 7.55 KB
/
Copy pathprelude.sh
File metadata and controls
executable file
路218 lines (188 loc) 路 7.55 KB
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
#!/bin/bash
# TODO write a naming convention and set up shfmt accordingly
# TODO make this into the library so that the
# testing of distro version can be used in if/else
#
# TODO refactor and rewrite this whole file, it is a bit ugly and buggy
# TODO use just one bash function notation not two
if [[ "${__INITPC_PRELUDE_SOURCED__}" != "true" ]]; then
__INITPC_PRELUDE_SOURCED__='true'
# Enable strict mode
set -euo pipefail
# 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
# 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
#
set -x # turn logging on here to avoid polluting logs with the obvious
# @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."
return "${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."
return "${EXIT_INCORRECT_PLATFORM}"
fi
}
function get_major_distro_version
{
if [[ ! -f /etc/os-release ]]; then
echo "/etc/os-release file not found."
echo "Aborting."
return "${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
return "${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."
return "${EXIT_INCORRECT_PLATFORM}"
fi
}
function distro_version_le
{
local MAJOR_VERSION
if ! MAJOR_VERSION="$(get_major_distro_version)"; then
return "${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 "${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."
return "${EXIT_INCORRECT_PLATFORM}"
fi
}
# TODO is this robust enough? What about sth. more robust?
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."
return "${EXIT_INCORRECT_PLATFORM}"
fi
return 0
}
print0()
{
[ "$#" -eq 0 ] || printf '%s\0' "$@"
}
# Appends a prefix in the first parameter to every line from stdin and executes
# the resulting command. Ignores comments and empty lines.
for_each()
{
set +x
local prefix="$1"
local line
while read -r line; do # Read a line and strip leading and trailing spaces
# Skip empty lines and comments
[[ "$line" =~ ^[[:space:]]*(#|$) ]] && continue
# TODO try rewriting this with eval so that the colors and
# debug msgs are printed correctly. I am using subshell instead of eval
# because of the debugging output being displayed incorrectly otherwise.
bash -cx "$prefix$line"
done
set -x
}
err()
{
echo "[$(date +'%Y-%m-%dT%H:%M:%S%z')]: $*" >&2
}
my_run_parts()
{
# TODO add pushd and popd here?
# maybe run-parts can also execute everything from main
# and the platform check would be a firtst file in the folder
# i.e. 00 and I would call it platform_check or sth like that
# what about that?
echo "Not implemented"
# run-parts for generic filenames (including dot)
# as a test run first
# run-parts --regex='^[^/]+$' --test . | grep files not matching the regex below
run-parts --regex='^[0-9][0-9][\.A-Za-z0-9_-]+$' .
# but filter the filenames and exit if there is a filename
# that does not match [0-9][0-9]*.sh
# Then run the real run and only filter by the strict regex
exit 1
# run-parts --regex='' --exit-on-error --verbose
}
ensure_line()
{
local line="$1"
local file="$2"
grep -Fxq -- "$line" "$file" 2> /dev/null \
|| printf '%s\n' "$line" >> "$file"
}
fi