-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgen-mirrors-json.sh
executable file
·61 lines (52 loc) · 1.71 KB
/
gen-mirrors-json.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
#!/bin/bash
ini_file="reborn-mirrorlist"
json_file="mirrorlist.json"
# Check if the mirrorlist file exists
if [ ! -f "$ini_file" ]; then
echo "Error: $ini_file not found."
exit 1
fi
# Extract date and revision from the mirrorlist file
metadata_line=$(grep -oP '^# --> RebornOS Mirrorlist from \K\d+-\d+' "$ini_file")
date=$(echo "$metadata_line" | cut -d'-' -f1)
revision=$(echo "$metadata_line" | cut -d'-' -f2)
# Start JSON structure
echo "{" >"$json_file"
{
echo " \"date\": $date,"
echo " \"revision\": $revision,"
echo " \"mirrors\": ["
} >>"$json_file"
# Read each line from the mirrorlist file
while IFS= read -r line; do
# Check for mirror entries
if [[ "$line" =~ ^#\ Name:.*$ ]]; then
# If it's a mirror entry, start a new JSON object for a mirror
if [ -n "$mirror_name" ]; then
echo " }," >>"$json_file"
fi
mirror_name=$(echo "$line" | sed -n 's/^# Name: \(.*\)$/\1/p')
echo " {" >>"$json_file"
echo " \"name\": \"$mirror_name\"," >>"$json_file"
elif [ -n "$mirror_name" ]; then
# Parse location and server for mirrors
case "$line" in
"# Location:"*)
mirror_location=$(echo "$line" | sed -n 's/^# Location: \(.*\)$/\1/p')
echo " \"location\": \"$mirror_location\"," >>"$json_file"
;;
"Server ="*)
mirror_server=$(echo "$line" | sed 's/^Server = \(.*\)$/\1/' | tr -d '[:space:]')
echo " \"server\": \"$mirror_server\"" >>"$json_file"
;;
esac
fi
done <"$ini_file"
# Close the last JSON object for the last mirror
if [ -n "$mirror_name" ]; then
echo " }" >>"$json_file"
fi
# End JSON structure
echo " ]" >>"$json_file"
echo "}" >>"$json_file"
echo "Conversion completed. JSON file created: $json_file"