99# Output: One URL per line, first is official source, rest are mirrors
1010change_sources_list_radxa_urls () {
1111 local urls=(
12- " https://radxa-repo.github.io"
1312 " https://mirrors.aghost.cn/radxa-deb"
1413 " https://mirrors.lzu.edu.cn/radxa-deb"
1514 " https://mirrors.hust.edu.cn/radxa-deb"
@@ -35,41 +34,89 @@ change_sources_list_debian_urls() {
3534 printf ' %s\n' " ${urls[@]} "
3635}
3736
38- # Check if target value exists in list
39- # Args: $1 - target value, $@ - list
40- # Returns: 0 if found, 1 if not found
41- __change_sources_contains () {
42- local target=" $1 " item
43- shift
44- for item in " $@ " ; do
45- [[ " $target " == " $item " ]] && return 0
46- done
47- return 1
48- }
49-
5037# Get all Radxa APT source configuration file paths
5138# Output: One file path per line
5239__change_sources_get_radxa_lists () {
5340 shopt -s nullglob
5441 local files=(/etc/apt/sources.list.d/* radxa* .list)
5542 shopt -u nullglob
56-
43+
5744 (( ${# files[@]} == 0 )) && return 0
58-
45+
5946 printf ' %s\n' " ${files[@]} "
6047}
6148
49+ # Get current Radxa mirror URL
50+ # Output: Current mirror URL if it's in known list, or empty otherwise
51+ change_sources_get_current_radxa_mirror () {
52+ local file url
53+ while IFS= read -r file; do
54+ [[ -n " $file " && -f " $file " ]] || continue
55+
56+ # Extract URL from deb/deb-src lines
57+ url=" $( grep -E ' ^[[:space:]]*(deb|deb-src)' " $file " | head -n1 | awk ' {print $2}' ) "
58+ if [[ -n " $url " ]]; then
59+ # Validate URL is in known mirror list (including official source)
60+ local known_mirrors=(" https://radxa-repo.github.io" )
61+ local mirror_list=()
62+ mapfile -t mirror_list < <( change_sources_list_radxa_urls)
63+ known_mirrors+=(" ${mirror_list[@]} " )
64+
65+ if __in_array " $url " " ${known_mirrors[@]} " ; then
66+ echo " $url "
67+ return 0
68+ fi
69+ fi
70+ done < <( __change_sources_get_radxa_lists)
71+ }
72+
73+ # Get current Debian/Ubuntu mirror URL
74+ # Output: Current mirror URL if it's in known list, or empty otherwise
75+ change_sources_get_current_debian_mirror () {
76+ shopt -s nullglob
77+ local lists=(/etc/apt/sources.list /etc/apt/sources.list.d/* .list)
78+ shopt -u nullglob
79+
80+ local file url
81+ for file in " ${lists[@]} " ; do
82+ [[ -f " $file " ]] || continue
83+
84+ # Skip Radxa source files
85+ [[ " $file " == * radxa* .list ]] && continue
86+
87+ # Extract URL from deb/deb-src lines, get only the base URL (hostname)
88+ url=" $( grep -E ' ^[[:space:]]*(deb|deb-src)' " $file " | head -n1 | awk ' {print $2}' | sed ' s|^\(https\?://[^/]*\).*|\1|' ) "
89+ if [[ -n " $url " ]]; then
90+ # Validate URL is in known mirror list (including official sources)
91+ local known_mirrors=(
92+ " https://deb.debian.org"
93+ " https://deb.ubuntu.com"
94+ )
95+ local mirror_list=()
96+ mapfile -t mirror_list < <( change_sources_list_debian_urls)
97+ known_mirrors+=(" ${mirror_list[@]} " )
98+
99+ if __in_array " $url " " ${known_mirrors[@]} " ; then
100+ echo " $url "
101+ return 0
102+ fi
103+ fi
104+ done
105+ }
106+
62107# Apply Radxa mirror replacement
63108# Args: $1 - new mirror URL
64109__change_sources_apply_radxa_mirror () {
65110 local new_mirror=" $1 "
66- local all_mirrors=()
67- mapfile -t all_mirrors < <( change_sources_list_radxa_urls)
111+ local all_mirrors=(" https://radxa-repo.github.io" )
112+ local mirror_list=()
113+ mapfile -t mirror_list < <( change_sources_list_radxa_urls)
114+ all_mirrors+=(" ${mirror_list[@]} " )
68115
69116 local file
70117 while IFS= read -r file; do
71118 [[ -n " $file " ]] || continue
72-
119+
73120 # Replace all known Radxa mirrors with new mirror
74121 local old_mirror
75122 for old_mirror in " ${all_mirrors[@]} " ; do
@@ -86,7 +133,7 @@ change_sources_set_radxa_mirror() {
86133 local all_mirrors=()
87134 mapfile -t all_mirrors < <( change_sources_list_radxa_urls)
88135
89- if ! __change_sources_contains " $mirror " " ${all_mirrors[@]} " ; then
136+ if ! __in_array " $mirror " " ${all_mirrors[@]} " ; then
90137 echo " Unsupported Radxa mirror: $mirror " >&2
91138 return 1
92139 fi
@@ -135,7 +182,7 @@ change_sources_set_debian_mirror() {
135182 local all_mirrors=()
136183 mapfile -t all_mirrors < <( change_sources_list_debian_urls)
137184
138- if ! __change_sources_contains " $mirror " " ${all_mirrors[@]} " ; then
185+ if ! __in_array " $mirror " " ${all_mirrors[@]} " ; then
139186 echo " Unsupported Debian/Ubuntu mirror: $mirror " >&2
140187 return 1
141188 fi
@@ -147,11 +194,31 @@ change_sources_set_debian_mirror() {
147194# Restore official APT sources
148195# Restores both Radxa and Debian/Ubuntu sources to official sources
149196change_sources_restore () {
150- local radxa_urls=()
151- mapfile -t radxa_urls < <( change_sources_list_radxa_urls)
152-
153197 # Restore Radxa to official source
154- __change_sources_apply_radxa_mirror " ${radxa_urls[0]} "
198+ __change_sources_apply_radxa_mirror " https://radxa-repo.github.io"
199+
200+ # Determine system type from /etc/os-release
201+ local os_id=" "
202+ if [[ -f /etc/os-release ]]; then
203+ # shellcheck disable=SC1091
204+ source /etc/os-release
205+ os_id=" $ID "
206+ fi
207+
208+ # Determine official mirror based on OS
209+ local official_mirror
210+ case " $os_id " in
211+ ubuntu)
212+ official_mirror=" https://deb.ubuntu.com"
213+ ;;
214+ debian)
215+ official_mirror=" https://deb.debian.org"
216+ ;;
217+ * )
218+ echo " Unable to determine OS type from /etc/os-release" >&2
219+ return 1
220+ ;;
221+ esac
155222
156223 # Restore Debian/Ubuntu sources to official sources
157224 local all_mirrors=(
@@ -166,29 +233,18 @@ change_sources_restore() {
166233 local lists=(/etc/apt/sources.list /etc/apt/sources.list.d/* .list)
167234 shopt -u nullglob
168235
169- local file
236+ local file old_mirror old_mirror_norm
170237 for file in " ${lists[@]} " ; do
171238 [[ -f " $file " ]] || continue
172239
173240 # Skip Radxa source files
174241 [[ " $file " == * radxa* .list ]] && continue
175242
176- # Check if file contains Ubuntu or Debian sources and restore accordingly
177- if grep -q " ubuntu" " $file " ; then
178- # Restore Ubuntu sources
179- local old_mirror old_mirror_norm
180- for old_mirror in " ${all_mirrors[@]} " ; do
181- old_mirror_norm=" ${old_mirror%/ } "
182- sed -i " s|$old_mirror_norm |https://deb.ubuntu.com|g" " $file "
183- done
184- else
185- # Restore Debian sources
186- local old_mirror old_mirror_norm
187- for old_mirror in " ${all_mirrors[@]} " ; do
188- old_mirror_norm=" ${old_mirror%/ } "
189- sed -i " s|$old_mirror_norm |https://deb.debian.org|g" " $file "
190- done
191- fi
243+ # Restore to official mirror based on detected OS
244+ for old_mirror in " ${all_mirrors[@]} " ; do
245+ old_mirror_norm=" ${old_mirror%/ } "
246+ sed -i " s|$old_mirror_norm |$official_mirror |g" " $file "
247+ done
192248 done
193249
194250 echo " APT sources restored to official sources" >&2
0 commit comments