-
Notifications
You must be signed in to change notification settings - Fork 1
/
installer-prep
executable file
·337 lines (285 loc) · 10.9 KB
/
installer-prep
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
#!/bin/bash
#
# This script checks the operating system, architecture, bit type, and other factors to
# determine if the system is compatible with NadekoBot. If the system is supported, it
# will download and execute the main installer.
#
########################################################################################
####[ Exported and Global Variables ]###################################################
# Refer to the 'README' note at the beginning of 'linuxAIO' for more information.
readonly C_CURRENT_LINUXAIO_REVISION=44
readonly C_MAIN_INSTALLER="nadeko-main-installer"
## Modify output text color.
E_YELLOW="$(printf '\033[1;33m')"
E_GREEN="$(printf '\033[0;32m')"
E_BLUE="$(printf '\033[0;34m')"
E_CYAN="$(printf '\033[0;36m')"
E_RED="$(printf '\033[1;31m')"
E_NC="$(printf '\033[0m')"
E_GREY="$(printf '\033[0;90m')"
E_CLRLN="$(printf '\r\033[K')"
export E_YELLOW E_GREEN E_BLUE E_CYAN E_RED E_NC E_GREY E_CLRLN
## Short-hand colorized messages.
E_SUCCESS="${E_GREEN}==>${E_NC} "
E_WARNING="${E_YELLOW}==>${E_NC} "
E_ERROR="${E_RED}ERROR:${E_NC} "
E_INFO="${E_BLUE}==>${E_NC} "
E_NOTE="${E_CYAN}==>${E_NC} "
export E_SUCCESS E_WARNING E_ERROR E_INFO E_NOTE
export E_ROOT_DIR="$PWD"
export E_INSTALLER_PREP="$E_ROOT_DIR/installer-prep"
####[ Functions ]#######################################################################
####
# Identify the operating system, version number, architecture, bit type (32 or 64), etc.
# This information is then made available to the script, and the rest of the installer.
#
# NEW GLOBALS:
# - E_DISTRO: Distribution name.
# - E_VER: Distribution version.
# - E_SVER: Distribution short version.
# - E_ARCH: Architecture.
# - pname: Pretty name.
# - bits: Bit type.
detect_sys_info() {
if [[ -f /etc/os-release ]]; then
. /etc/os-release
E_DISTRO="$ID"
E_VER="$VERSION_ID" # Version: x.x.x...
E_SVER=${E_VER//.*/} # Version: x
pname="$PRETTY_NAME"
else
E_DISTRO=$(uname -s)
E_VER=$(uname -r)
fi
case $(uname -m) in
x86_64) bits="64"; E_ARCH="x64" ;;
i*86) bits="32"; E_ARCH="x86" ;;
armv*) bits="32"; E_ARCH="?" ;;
*) bits="?"; E_ARCH="?" ;;
esac
}
####
# Download the latest version of 'linuxAIO' if $E_LINUXAIO_REVISION and
# $C_CURRENT_LINUXAIO_REVISION aren't of equal value.
linuxAIO_update() {
echo "${E_WARNING}You are using an older version of 'linuxAIO'"
read -rp "${E_NOTE}Press [Enter] to download the latest version"
echo "${E_INFO}Downloading latest version of 'linuxAIO'..."
if [[ -f linuxAIO.old ]]; then
echo "${E_INFO}Removing existing 'linuxAIO.old'..."
rm linuxAIO.old
fi
if [[ -f linuxAIO.sh ]]; then
echo "${E_INFO}Backing up 'linuxAIO.sh' as 'linuxAIO.old'..."
mv linuxAIO.sh linuxAIO.old
elif [[ -f linuxAIO ]]; then
echo "${E_INFO}Backing up 'linuxAIO' as 'linuxAIO.old'..."
mv linuxAIO linuxAIO.old
fi
chmod -x linuxAIO.old
## Due to changes in 'linuxAIO', the existing configurations cannot be directly
## applied to the newest version of 'linuxAIO'. As a result, the existing
## configurations will instead be backed up as 'linuxAIO.old', and the end-user will
## have to reconfigure 'linuxAIO' manually.
##
# shellcheck disable=SC2153
# $_LINUXAIO_REVISION and $_RAW_URL are from revision 38 and earlier. This means
# that the if statement doesn't need to test for $E_LINUXAIO_REVISION to check if
# the current version of 'linuxAIO' is revision 38 or earlier.
if [[ $_LINUXAIO_REVISION ]] && ((_LINUXAIO_REVISION <= 38)); then
curl -O "$_RAW_URL/linuxAIO" || {
E_STDERR "Failed to download 'linuxAIO'"
echo "${E_INFO}Restoring previous version of 'linuxAIO'..."
[[ -f linuxAIO.old ]] \
&& mv linuxAIO.old linuxAIO \
&& chmod +x linuxAIO
exit 1
}
sudo chmod +x linuxAIO
echo "${E_NOTE}NOT applying existing configurations to 'linuxAIO'"
echo "${E_SUCCESS}Successfully downloaded the newest version of 'linuxAIO'"
## Download the newest version of 'linuxAIO' and apply existing changes to it.
elif [[ $E_LINUXAIO_REVISION != "$C_CURRENT_LINUXAIO_REVISION" ]]; then
local installer_branch
local installer_branch_found
installer_branch=$(grep '^installer_branch=.*' linuxAIO.old)
installer_branch_found="$?"
local bot_install_version
local bot_install_version_found
bot_install_version=$(grep '^export E_BOT_INSTALL_VERSION=.*' linuxAIO.old)
bot_install_version_found="$?"
curl -O "$E_RAW_URL"/linuxAIO || {
E_STDERR "Failed to download 'linuxAIO'"
echo "${E_INFO}Restoring previous version of 'linuxAIO'..."
[[ -f linuxAIO.old ]] \
&& mv linuxAIO.old linuxAIO \
&& chmod +x linuxAIO
exit 1
}
sudo chmod +x linuxAIO
echo "${E_INFO}Applying existing configurations to the new 'linuxAIO'..."
[[ $installer_branch_found == 0 ]] \
&& sed -i "s/^installer_branch=.*/$installer_branch/" linuxAIO
[[ $bot_install_version_found == 0 ]] \
&& sed -i "s/^export E_BOT_INSTALL_VERSION=.*/$bot_install_version/" linuxAIO
echo "${E_SUCCESS}Successfully downloaded the newest version of 'linuxAIO'" \
"and applied existing changes"
echo "${E_NOTE}Make sure to update the variables in 'linuxAIO' that were not" \
"automatically updated, using 'linuxAIO.old'"
else
echo "${E_SUCCESS}You are already using the latest version of 'linuxAIO'"
fi
clean_exit 0
}
####
# Provide the end-user with the option to continue, even if their system isn't
# officially supported.
unsupported() {
local choice
echo "${E_ERROR}Your operating system is not OFFICIALLY supported for the" \
"installation, setup, and/or use of NadekoBot" >&2
echo "${E_WARNING}${E_YELLOW}WARNING${E_NC}: By continuing, you accept that" \
"unexpected behaviors may occur. If you run into any errors or problems with" \
"the installation and use of the NadekoBot, you are on your own."
read -rp "${E_INFO}Would you like to continue anyways? [y/N] " choice
choice=${choice,,}
case "$choice" in
y|yes)
clear -x
execute_main_installer
;;
*)
clean_exit 0
;;
esac
}
####
# Cleanly exit the installer by removing temporary files and exiting with an appropriate
# message and exit code.
#
# PARAMETERS:
# - $1: exit_code (Required)
# - $2: use_extra_newline (Optional, Default: false)
# - Whether to output an extra blank line, to separate the previous output from
# the output of this function.
# - Acceptable values:
# - true
# - false
clean_exit() {
local exit_code="$1"
local use_extra_newline="${2:-false}"
# Files to be removed.
local installer_files=("installer-prep" "file-backup" "prereqs-installer"
"nadeko-latest-installer" "nadeko-runner" "nadeko-main-installer")
trap - EXIT
[[ $use_extra_newline == true ]] && echo ""
## While we specify output for SIGHUP and SIGTERM in 'nadeko-main-installer', we do
## the same here because the those signals don't propagate to the parent script.
case "$exit_code" in
0|1) echo "" ;;
129) echo -e "\n${E_WARNING}Hangup signal detected (SIGHUP)" ;;
130) echo -e "\n${E_WARNING}User interrupt detected (SIGINT)" ;;
143) echo -e "\n${E_WARNING}Termination signal detected (SIGTERM)" ;;
*) echo -e "\n${E_WARNING}Exiting with code: $exit_code" ;;
esac
echo "${E_INFO}Cleaning up..."
cd "$E_ROOT_DIR" || E_STDERR "Failed to move working directory to '$E_ROOT_DIR'" "1"
for file in "${installer_files[@]}"; do
[[ -f $file ]] && rm "$file"
done
echo "${E_INFO}Exiting..."
exit "$exit_code"
}
####
# Download and execute the main installer. This is one of the final actions performed by
# this script.
execute_main_installer() {
E_DOWNLOAD_SCRIPT "$C_MAIN_INSTALLER" "true"
./"$C_MAIN_INSTALLER"
clean_exit "$?"
}
###
### [ Functions To Be Exported ]
###
####
# Download the specified script and modify it's execution permissions.
#
# PARAMETERS:
# - $1: script_name (Required)
# - The name of the script to download.
# - $2: script_output (Optional, Default: false)
# - Whether to indicated that the script is being downloaded.
# - Acceptable values:
# - true
# - false
E_DOWNLOAD_SCRIPT() {
local script_name="$1"
local script_output="${2:-false}"
[[ $script_output == true ]] \
&& printf "%sDownloading '%s'..." "${E_INFO}" "$script_name"
curl -O -s "$E_RAW_URL"/"$script_name"
sudo chmod +x "$script_name"
}
####
# Output an error message, and if desired, exit the script.
#
# PARAMETERS:
# - $1: error_message (Required)
# - $2: exit_code (Optional, Default: "")
# - If provided, exit with the provided value.
# - $3: additional_message (Optional, Default: "")
# - If provided, output an additional message.
E_STDERR() {
local error_message="$1"
local exit_code="${2:-}"
local additional_message="${3:-}"
echo "${E_ERROR}$error_message" >&2
[[ $additional_message ]] && echo -e "$additional_message" >&2
[[ $exit_code ]] && exit "$exit_code"
}
####[ Trapping Logic ]##################################################################
trap 'clean_exit "129" "true"' SIGHUP
trap 'clean_exit "130" "true"' SIGINT
trap 'clean_exit "143" "true"' SIGTERM
trap 'clean_exit "$?" "true"' EXIT
####[ Prepping ]########################################################################
# shellcheck disable=SC2153
# $_LINUXAIO_REVISION is from revision 38 and earlier. $E_LINUXAIO_REVISION is used
# for revision 39 and later.
if [[ $E_LINUXAIO_REVISION != "$C_CURRENT_LINUXAIO_REVISION" ]]; then
# NOTE: The script will exit after the function has run.
linuxAIO_update
fi
####[ Main ]############################################################################
clear -x
detect_sys_info
export E_DISTRO E_SVER E_VER E_ARCH
export -f E_DOWNLOAD_SCRIPT E_STDERR
echo "SYSTEM INFO
Bit Type: $bits
Architecture: $E_ARCH
Distro: ${pname:=$E_DISTRO}
Distro Version: $E_VER
"
if [[ $bits == 64 ]]; then
if [[ $E_DISTRO == "ubuntu" ]]; then
case "$E_VER" in
24.04|22.04) execute_main_installer ;;
*) unsupported ;;
esac
elif [[ $E_DISTRO == "debian" ]]; then
case "$E_SVER" in
12|11) execute_main_installer ;;
*) unsupported ;;
esac
elif [[ $E_DISTRO == "linuxmint" ]]; then
case "$E_SVER" in
22|21) execute_main_installer ;;
*) unsupported ;;
esac
else
unsupported
fi
else
unsupported
fi