forked from engrchachi/official_devices
-
Notifications
You must be signed in to change notification settings - Fork 0
/
json_ota_helper.sh
executable file
·356 lines (321 loc) · 12 KB
/
json_ota_helper.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
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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
#!/bin/bash
RED='\033[0;31m'
GREEN='\033[0;32m'
BLUE='\033[0;34m'
CYAN='\033[0;36m'
ORANGE='\033[0;33m'
ENDCOLOR='\033[0m'
display_header() {
echo -e "${GREEN}===========================================================${ENDCOLOR}"
echo -e "${BLUE} ______ __ __ _ _ __ ${ENDCOLOR}"
echo -e "${BLUE} / ____/ ______ / /_ __/ /_(_)___ ____ | |/ / ${ENDCOLOR}"
echo -e "${BLUE} / __/ | | / / __ \/ / / / / __/ / __ \/ __ \ | / ${ENDCOLOR}"
echo -e "${BLUE} / /___ | |/ / /_/ / / /_/ / /_/ / /_/ / / / / / | ${ENDCOLOR}"
echo -e "${BLUE} /_____/ |___/\____/_/\__,_/\__/_/\____/_/ /_/ /_/|_| ${ENDCOLOR}"
echo -e "${BLUE} ${ENDCOLOR}"
echo -e "${BLUE} Json OTA helper ${ENDCOLOR}"
echo -e "${BLUE} ${ENDCOLOR}"
echo -e "${BLUE} #KeepEvolving ${ENDCOLOR}"
echo -e "${GREEN}===========================================================${ENDCOLOR}"
}
clear
dependencies="coreutils git jq"
missing_dependencies=()
package_managers=("dpkg" "rpm" "pacman" "equery" "apt-get" "pacman" "dnf" "emerge")
for dependency in $dependencies; do
found=false
for package_manager in "${package_managers[@]}"; do
if command -v "$package_manager" >/dev/null 2>&1; then
if "$package_manager" -s "$dependency" >/dev/null 2>&1 || \
"$package_manager" -q "$dependency" >/dev/null 2>&1 || \
"$package_manager" -Q "$dependency" >/dev/null 2>&1 || \
"$package_manager" -q list "$dependency" >/dev/null 2>&1; then
found=true
break
fi
fi
done
if [ "$found" = false ]; then
missing_dependencies+=("$dependency")
fi
done
if [ ${#missing_dependencies[@]} -ne 0 ]; then
clear && display_header
echo -e "${ORANGE}Missing dependencies:${ENDCOLOR}"
for dependency in "${missing_dependencies[@]}"; do
case $dependency in
coreutils)
echo -e "${BLUE}$dependency: A collection of essential command-line utilities for basic file and text manipulation.${ENDCOLOR}"
;;
git)
echo -e "${BLUE}$dependency: A distributed version control system.${ENDCOLOR}"
;;
jq)
echo -e "${BLUE}$dependency: A lightweight and flexible command-line tool for parsing and manipulating JSON data.${ENDCOLOR}"
;;
esac
done
while true; do
read -p "Do you want to install these dependencies? (y/n): " choice
case $choice in
[Yy]|[Yy][Ee][Ss])
if [ -x "$(command -v apt-get)" ]; then
clear && display_header
echo -e "${ORANGE}Debian/Ubuntu detected, installing required dependencies...${ENDCOLOR}"
sudo apt-get update && sudo apt-get install -y "${missing_dependencies[@]}" || {
echo -e "${RED}Error: Failed to install required dependencies using apt-get.${ENDCOLOR}"
exit 1
}
elif [ -x "$(command -v pacman)" ]; then
clear && display_header
echo -e "${ORANGE}Arch detected, installing required dependencies...${ENDCOLOR}"
sudo pacman -Sy --noconfirm "${missing_dependencies[@]}" || {
echo -e "${RED}Error: Failed to install required dependencies using pacman.${ENDCOLOR}"
exit 1
}
elif [ -x "$(command -v dnf)" ]; then
clear && display_header
echo -e "${ORANGE}Fedora detected, installing required dependencies...${ENDCOLOR}"
sudo dnf update -y && sudo dnf install -y "${missing_dependencies[@]}" || {
echo -e "${RED}Error: Failed to install required dependencies using dnf.${ENDCOLOR}"
exit 1
}
elif [ -x "$(command -v emerge)" ]; then
echo -e "${ORANGE}Gentoo detected, installing required dependencies...${ENDCOLOR}"
clear && display_header
sudo emerge -av "${missing_dependencies[@]}" || {
echo -e "${RED}Error: Failed to install required dependencies using emerge.${ENDCOLOR}"
exit 1
}
else
clear && display_header
echo -e "${RED}Error: Unsupported distro or package manager detected.${ENDCOLOR}"
exit 1
fi
clear && display_header
echo -e "${GREEN}Dependencies successfully installed. Running...${ENDCOLOR}"
break
;;
[Nn]|[Nn][Oo])
clear
echo -e "${RED}Dependencies not satisfied... Exiting!${ENDCOLOR}"
exit 0
;;
*)
echo "Invalid selection. Please enter 'yes' or 'no'."
;;
esac
done
fi
clear && display_header
display_help() {
echo
echo -e "${BLUE}Usage:${ENDCOLOR} ./json_ota_helper.sh ${ORANGE}<input_json>${ENDCOLOR}"
echo
echo -e "${RED}Note:${ENDCOLOR} The input json should contain the following properties:"
echo -e "${CYAN} - datetime: Unix timestamp of the build"
echo -e " - filehash: md5 checksum of the build.zip"
echo -e " - filename: Name of the build.zip"
echo -e " - id: sha256 checksum of the build.zip"
echo -e " - size: Size of the build.zip in bytes${ENDCOLOR}"
}
if [ $# -ne 1 ]; then
display_help
exit 1
fi
input_json="$1"
if [ ! -f "$input_json" ]; then
echo -e "${RED}Input json, ${CYAN}$input_json${RED} not found!${ENDCOLOR}"
display_help
exit 1
fi
filename=$(jq -r '.filename' "$input_json")
if [ -z "$filename" ]; then
echo -e "${RED}Invalid input json: ${CYAN}$input_json${RED}"
echo "The input JSON file is missing the 'filename' property or has an invalid format."
echo "Please make sure the input json contains a 'filename' property in the format 'evolution_<codename>-ota-<>.json'"
exit 1
fi
codename=$(echo "$filename" | sed -E 's/^evolution_([^.-]+)-ota-.+$/\1/')
if [ -z "$codename" ]; then
display_help
exit 1
fi
output_json="./builds/${codename}.json"
if [ ! -f "$output_json" ]; then
echo -e "${RED}Output json, ${CYAN}$output_json${RED} not found!${ENDCOLOR}"
exit 1
fi
old_data=$(cat "$output_json")
if [ "$(<"$input_json")" = "$old_data" ]; then
echo "No changes required. All properties match."
exit 0
fi
required_properties=("datetime" "filehash" "filename" "id" "size")
for prop in "${required_properties[@]}"; do
if ! jq -e ".${prop}" "$input_json" >/dev/null; then
echo -e "${RED}Invalid input json: ${CYAN}$input_json${RED}"
echo "The input JSON file is missing the '${prop}' property."
echo "Please make sure the input json contains all the required properties:"
for req_prop in "${required_properties[@]}"; do
echo "- ${req_prop}"
done
exit 1
fi
done
datetime=$(jq -r '.datetime' "$input_json")
filehash=$(jq -r '.filehash' "$input_json")
id=$(jq -r '.id' "$input_json")
size=$(jq -r '.size' "$input_json")
url="https://sourceforge.net/projects/evolution-x/files/${codename}/${filename}/download/"
display_diff() {
local old_value=$1
local new_value=$2
local property=$3
if [ "$old_value" != "$new_value" ]; then
echo -e " ${CYAN}${property}:${ENDCOLOR}"
echo -e " ${CYAN}Old:${ENDCOLOR} ${RED}${old_value}${ENDCOLOR}"
echo -e " ${CYAN}New:${ENDCOLOR} ${GREEN}${new_value}${ENDCOLOR}"
return 1
fi
}
echo -e "${ORANGE}Updating ${codename}.json:${ENDCOLOR}"
changes_found=0
display_diff "$(echo "$old_data" | jq -r '.datetime')" "$datetime" "datetime" || changes_found=1
display_diff "$(echo "$old_data" | jq -r '.filehash')" "$filehash" "filehash" || changes_found=1
display_diff "$(echo "$old_data" | jq -r '.id')" "$id" "id" || changes_found=1
display_diff "$(echo "$old_data" | jq -r '.size')" "$size" "size" || changes_found=1
display_diff "$(echo "$old_data" | jq -r '.url')" "$url" "url" || changes_found=1
display_diff "$(echo "$old_data" | jq -r '.filename')" "$filename" "filename" || changes_found=1
if [ "$changes_found" -eq 0 ]; then
echo -e "${ORANGE}No changes required. All properties match.${ENDCOLOR}"
exit 0
fi
temp_file=$(mktemp)
jq --argjson datetime "$datetime" --arg filehash "$filehash" --arg id "$id" --argjson size "$size" --arg url "$url" --arg filename "$filename" \
'.datetime = $datetime
| .filehash = $filehash
| .id = $id
| .size = $size
| .url = $url
| .filename = $filename' \
"$output_json" > "$temp_file"
echo -e "${GREEN}Updated ${codename}.json${ENDCOLOR}"
echo
jq --indent 3 . "$temp_file" > "$temp_file.indented"
jq . "$temp_file.indented"
echo
mv "$temp_file.indented" "$output_json"
add_changelog() {
read -p "Do you want to add a changelog? (yes/no): " answer
if [[ $answer =~ ^[Yy][Ee][Ss]$ ]]; then
select_changelog_edit_method
elif [[ $answer =~ ^[Nn][Oo]$ ]]; then
exit 0
else
echo "Invalid selection. Please enter 'yes' or 'no'."
add_changelog
fi
}
git_commit() {
git add ./builds/${codename}.json
git add changelogs/${codename}/${filename}.txt
commit_name="$(tr '[:lower:]' '[:upper:]' <<< ${codename:0:1})${codename:1}: $(date +'%m/%d/%Y') Update"
echo "Commit Name: $commit_name"
read -p "Do you want to sign the commit? (yes/no): " sign_commit
if [[ $sign_commit =~ ^[Yy][Ee][Ss]$ ]]; then
git commit -s -m "$commit_name"
echo -e "${GREEN}Commit created successfully.${ENDCOLOR}"
exit 0
elif [[ $sign_commit =~ ^[Nn][Oo]$ ]]; then
git commit -m "$commit_name"
echo -e "${GREEN}Commit created successfully.${ENDCOLOR}"
exit 0
else
echo "Invalid selection. Please enter 'yes' or 'no'."
git_commit
fi
}
check_command() {
if ! command -v "$1" &>/dev/null; then
clear && display_header
echo -e "${RED}Error: $1 is not installed! Please install it and try again.${ENDCOLOR}"
sleep 1
return 1
fi
}
select_changelog_edit_method() {
clear
display_header
read -p "Select an editor to create the changelog with:
1. Nano
2. Vim
3. Gedit
4. Emacs
5. VSCode
6. Enter your own command
7. Exit
(1-7): " selection
case "$selection" in
1)
if check_command "nano"; then
clear
nano changelogs/${codename}/${filename}.txt
git_commit
fi
;;
2)
if check_command "vim"; then
clear
vim changelogs/${codename}/${filename}.txt
git_commit
fi
;;
3)
if check_command "gedit"; then
clear
gedit changelogs/${codename}/${filename}.txt
git_commit
fi
;;
4)
if check_command "emacs"; then
clear
emacs changelogs/${codename}/${filename}.txt
git_commit
fi
;;
5)
if check_command "code"; then
clear
code --wait changelogs/${codename}/${filename}.txt
git_commit
fi
;;
6)
clear && display_header
read -p "Enter a valid program name to open the changelog file (changelogs/${codename}/${filename}.txt): " custom_cmd
if command -v "$custom_cmd" &>/dev/null; then
eval "$custom_cmd changelogs/${codename}/${filename}.txt"
git_commit
else
clear && display_header
echo -e "${ORANGE}$custom_cmd not found, returning to the main menu..${ENDCOLOR}"
sleep 1
fi
;;
7)
clear
echo -e "${RED}Session ended.${ENDCOLOR}"
exit 0
;;
*)
clear && display_header
echo -e "${ORANGE}Invalid selection. Please enter (1-6).${ENDCOLOR}"
sleep 1
select_changelog_edit_method
;;
esac
}
add_changelog
select_changelog_edit_method