-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathn-update-bridge.bash
executable file
·206 lines (172 loc) · 7.23 KB
/
n-update-bridge.bash
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
#!/bin/bash
#
# m-bridge.bash Update and Configuration Migration Script
#
# This script automates the update process for 'm-bridge.bash'. It checks for a newer
# version of the script (or its legacy counterpart 'linuxAIO'), backs up the current
# version by renaming it to 'm-bridge.bash.old', and then downloads and installs the
# latest version. Additionally, it transfers existing configuration settings (such as
# manager branch and prerequisite checks) from the old version to the new one.
#
########################################################################################
####[ Functions ]#######################################################################
####
# Reverts changes made to 'm-bridge.bash' if the script is interrupted or fails.
#
# EXITS:
# - 1: Terminates the script immediately.
revert() {
if [[ -f m-bridge.bash.old && ! -f m-bridge.bash ]]; then
echo ""
echo -n "${E_INFO}Restoring the previous version of 'm-bridge.bash'..."
mv m-bridge.bash.old m-bridge.bash
chmod +x m-bridge.bash
fi
exit 1
}
####
# Downloads the latest 'm-bridge.bash' from $E_RAW_URL and makes it executable.
download_bridge() {
echo "${E_INFO}Downloading latest version of 'm-bridge.bash'..."
curl -O "$E_RAW_URL"/m-bridge.bash || {
E_STDERR "Failed to download 'm-bridge.bash'"
revert
}
chmod +x m-bridge.bash
}
####
# Transfers configuration settings from the old 'm-bridge.bash' to the new version.
transfer_bridge_data() {
local manager_branch
local manager_branch_found
manager_branch=$(grep '^manager_branch=.*' m-bridge.bash.old)
manager_branch_found="$?"
local skip_prereq_check
local skip_prereq_check_found
skip_prereq_check=$(grep '^export E_SKIP_PREREQ_CHECK=.*' m-bridge.bash.old)
skip_prereq_check_found="$?"
echo "${E_INFO}Applying existing configurations to the new 'm-bridge.bash'..."
if (( manager_branch_found == 0 )); then
sed -i "s/^manager_branch=.*/$manager_branch/" m-bridge.bash
else
echo "${E_WARN}Failed to find 'manager_branch' in 'm-bridge.bash.old'"
fi
if (( skip_prereq_check_found == 0 )); then
sed -i "s/^export E_SKIP_PREREQ_CHECK=.*/$skip_prereq_check/" m-bridge.bash
else
echo "${E_WARN}Failed to find 'E_SKIP_PREREQ_CHECK' in 'm-bridge.bash.old'"
fi
}
####
# Notifies the user that the current Manager version (revision 40 or earlier) is very
# outdated and does not support the automatic transfer of configurations to the new
# 'm-bridge.bash'.
#
# EXITS:
# - 1: Terminates the script after displaying the notification.
revision_40() {
echo "${E_WARN}You are using a very old version of the Manager, where it's not" \
"possible to automatically transfer configurations to the new"\
"'m-bridge.bash'." >&2
echo "${E_NOTE}'m-bridge.bash' has replaced 'linuxAIO'"
echo "${E_IMP}It's highly recommended to back up your current configurations and" \
"version of NadekoBot, then re-download NadekoBot using the newest version of" \
"the Manager."
echo "${E_NOTE}The newest version of the Manager can be found at" \
"https://github.com/StrangeRanger/nadeko-manager-scripts/blob/main/m-bridge.bash"
exit 1
}
####
# Performs additional checks and modifications for 'm-bridge.bash' revision 45 and
# earlier, ensuring compatibility with updated structures.
#
# RETURNS:
# - 0: If the function completes successfully or no actions are required.
revision_45() {
local additional_changes=false
[[ -f $E_BOT_DIR/$E_CREDS_EXAMPLE && -f $E_BOT_DIR/NadekoBot.dll ]] \
&& return 0
if [[ -d "$E_BOT_DIR" ]]; then
additional_changes=true
echo "${E_WARN}The new version of 'linuxAIO', now called 'm-bridge.bash', has" \
"several breaking changes since revision 45 and earlier. They will be" \
"handled automatically."
read -rp "${E_NOTE}Press [Enter] to continue"
echo "${E_INFO}Renaming '$E_BOT_DIR' to '$E_BOT_DIR.rev45.bak'..."
mv "$E_BOT_DIR" "$E_BOT_DIR.rev45.bak"
echo "${E_INFO}Copying '$E_BOT_DIR.rev45.bak/output' to '$E_BOT_DIR'..."
cp -r "$E_BOT_DIR.rev45.bak/output" "$E_BOT_DIR"
echo "${E_IMP}It is highly recommended to downloaded the newest version of" \
"NadekoBot before continuing."
fi
revision_47.5 "$additional_changes"
}
####
# Updates variable names in 'm-bridge.bash.old' to match the new naming conventions.
#
# PARAMETERS:
# - $1: additional_changes (Optional, Default: false)
# - Indicates whether additional changes are required.
# - Accepted values: true, false.
#
# EXITS:
# - 1: If the function fails to update the variables in 'm-bridge.bash'.
revision_47.5() {
local additional_changes="${1:-false}"
if [[ $additional_changes == true ]]; then
echo "${E_NOTE}There are several additional changes that must be made to" \
"'m-bridge.bash'"
else
echo "${E_NOTE}There are several changes that must be made to 'm-bridge.bash'"
fi
read -rp "${E_NOTE}Press 'Enter' to continue"
echo "${E_INFO}Updating variables in 'm-bridge.bash.old'..."
sed -i \
-e 's/installer_repo/manager_repo/g' \
-e 's/installer_branch/manager_branch/g' \
-e 's/E_LINUXAIO_REVISION/E_BRIDGE_REVISION/g' \
-e 's/E_BRIDGE_REVISION/E_BRIDGE_REVISION/g' \
-e 's/C_CURRENT_LINUXAIO_REVISION/C_LATEST_BRIDGE_REVISION/g' \
"m-bridge.bash.old" \
|| E_STDERR "Failed to update variables in 'm-bridge.bash'" "1"
}
####[ Trapping Logic ]##################################################################
trap 'revert' SIGINT
####[ Main ]############################################################################
printf "%s" "$E_CLR_LN" # Clear the "Downloading 'm-bridge.bash'..." message.
read -rp "${E_NOTE}Press [Enter] to download the latest version"
if [[ -f m-bridge.bash.old ]]; then
echo "${E_INFO}Removing existing 'm-bridge.bash.old'..."
rm m-bridge.bash.old
fi
if [[ -f linuxAIO ]]; then # Used in revisions 40 to 47.5.
[[ -f linuxAIO.old ]] && rm linuxAIO.old
echo "${E_INFO}Backing up 'linuxAIO' as 'm-bridge.bash.old'..."
mv linuxAIO m-bridge.bash.old
elif [[ -f m-bridge.bash ]]; then
echo "${E_INFO}Backing up 'm-bridge.bash' as 'm-bridge.bash.old'..."
mv m-bridge.bash m-bridge.bash.old
fi
chmod -x m-bridge.bash.old
echo "${E_INFO}Performing revision checks..."
if [[ -n $E_LINUXAIO_REVISION ]] && (( E_LINUXAIO_REVISION <= 40 )); then
revision_40 # Will exit script after the function call.
elif [[ $E_LINUXAIO_REVISION -le 47 && $E_CURRENT_LINUXAIO_REVISION == 47.5 ]]; then
download_bridge
if (( E_LINUXAIO_REVISION <= 45 )); then
revision_45
else
revision_47.5
fi
transfer_bridge_data
elif (( E_BRIDGE_REVISION != C_LATEST_BRIDGE_REVISION )); then
download_bridge
transfer_bridge_data
else
echo "${E_SUCCESS}You are already using the latest version of 'm-bridge.bash'"
exit 0
fi
echo "${E_SUCCESS}Successfully downloaded the newest version of 'm-bridge.bash' with" \
"existing configurations applied"
echo "${E_IMP}Review the 'm-bridge.bash.old' file for configurations that were not" \
"automatically transferred to the new 'm-bridge.bash'"