-
Notifications
You must be signed in to change notification settings - Fork 12
/
xmirror.in
executable file
·311 lines (265 loc) · 8.51 KB
/
xmirror.in
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
#!/bin/bash
usage() {
cat 1>&2 <<EOF
xmirror [-dhnv] [-l mirrorlist] [-r rootdir] [-s mirrorurl]
Interactive script for changing XBPS mirrors
-d, --default Reset the current mirror to the default, skipping the
interactive TUI.
-h, --help Show this help and exit.
-l, --mirror-list Use an alternative mirror list file. This should be a
tab-separated value file with the columns: region, url,
location, tier. Default: /usr/share/xmirror/mirrors.lst
-n, --no-fetch Don't attempt to update the mirror list.
-r, --rootdir Use an alternative rootdir. Acts like xbps's -r flag.
-s, --set Set the current mirror to mirrorurl and exit, skipping
the interactive TUI.
-v, --version Show the version and exit.
Copyright (c) 2023 classabbyamp, released under the BSD-2-Clause license
EOF
exit 1
}
version() {
echo "xmirror v@@VERSION@@" 1>&2
exit 0
}
# dialog colors
RED="\Z1"
WHITE="\Z7"
BOLD="\Zb"
RESET="\Zn"
INITMSG=$(fold -sw 76 <<EOF
For the best experience, choose a mirror geographically close to you, or use the Fastly Global CDN (listed in ${RED}World${RESET}). To use the default mirror (${RED}repo-default.voidlinux.org${RESET}), which can point to any Tier 1 mirror, Select ${BOLD}${RED}Default${RESET}.
Tier 1 mirrors are maintained by the Void Linux Infrastructure Team and will always have the latest packages available. Tier 2 mirrors are not managed by Void and do not have any guarantees of freshness or completeness of packages, nor are they required to sync every available architecture or sub-repository.
EOF
)
MENULABEL=$(fold -sw 74 <<EOF
Use UP and DOWN keys to navigate menus. Use TAB to switch between buttons and ENTER to select.
EOF
)
DIALOG() {
rm -f "$ANSWER"
dialog --colors --keep-tite --no-shadow --no-mouse \
--backtitle "${BOLD}${WHITE}Void Linux Mirror Selector -- https://www.voidlinux.org (v@@VERSION@@)${RESET}" \
--cancel-label "Back" --aspect 20 "$@" 2>"$ANSWER"
return $?
}
die() {
rm -f "$ANSWER"
exit "${1:-0}"
}
log() {
echo xmirror: "$@" 1>&2
}
check_required_programs() {
for cmd in find sed "$@"; do
if ! command -v "$cmd" >/dev/null 2>&1; then
log "could not find $cmd"
exit 1
fi
done
XBPS_INSTALL="$(check_xbps_program install)"
XBPS_FETCH="$(check_xbps_program fetch)"
}
check_xbps_program() {
local prog="$1"
if command -v "xbps-$prog" >/dev/null 2>&1; then
echo "xbps-$prog"
elif command -v "xbps-$prog.static" >/dev/null 2>&1; then
echo "xbps-$prog.static"
else
log "xmirror: could not find xbps-$prog or xbps-$prog.static"
exit 1
fi
}
check_root() {
if [ "$(id -u)" != 0 ]; then
log "must be run as root"
exit 1
fi
}
# uses names and abbreviations from https://planetarynames.wr.usgs.gov/Abbreviations
region_name() {
case "$1" in
AF) echo "Africa" ;;
AN) echo "Antarctica" ;;
AS) echo "Asia" ;;
EU) echo "Europe" ;;
NA) echo "North America" ;;
OC) echo "Oceania" ;;
SA) echo "South and Central America" ;;
World) echo "Globally Available" ;;
*) echo "$1" ;;
esac
}
get_mirrors() {
if [ -z "$NOFETCH" ]; then
log "updating mirrorlist in ${ROOTDIR}/usr/share/xmirror"
if ! $XBPS_FETCH -o "$MIRRORLIST" "$MIRRORURL" &>/dev/null; then
log "unable to fetch new mirror list"
fi
fi
while read -r line; do
[[ "$line" = '#'* ]] && continue
while IFS=$'\t' read -r region rest; do
allmirrors["$region"]+="$rest"$'\n'
done <<< "$line"
done < "$MIRRORLIST"
for region in $(printf '%s\n' "${!allmirrors[@]}" | sort -u); do
regions+=( "$region" "$(region_name "$region")" )
done
regions+=( "Default" "Reset to the default mirror" )
regions+=( "Exit" "Exit this wizard" )
}
write_mirrors_int() {
local FILES REPOSITORY="$1"
FILES="$(find "${ROOTDIR}"/usr/share/xbps.d/ -name '*-repository-*.conf' | sort -u | sed 's|/usr/share|/etc|g')"
DIALOG --cr-wrap --yesno "${BOLD}The following operations will be executed:${RESET}\n\n
The current mirror will be switched to: ${RED}${REPOSITORY}${RESET}
The following files in ${BOLD}${ROOTDIR}/etc/xbps.d${RESET} will be created or overwritten, if they exist:
${RED}${FILES// /$'\n'}${RESET}\n\n
${BOLD}Do you want to continue?${RESET}" 20 80 || return
{
if write_mirrors "$REPOSITORY"; then
printf "\nMirror successfully changed to %s!\n" "$REPOSITORY"
else
for f in "${ROOTDIR}/etc/xbps.d"/*-repository-*.conf.old; do
mv "$f" "${f%.old}"
done
printf "\nMirror update failed! See log above for details.\n"
fi
} |& DIALOG --title " Updating mirror... " --programbox 16 80
exit 0
}
write_mirrors() {
set -e
local REPOSITORY="$1"
if ! [[ "$REPOSITORY" = *"://"* ]]; then
log "warning: mirror protocol not specified, assuming http://"
REPOSITORY="http://$REPOSITORY"
fi
mkdir -p "${ROOTDIR}/etc/xbps.d"
cp --no-preserve=mode,ownership --backup=simple --suffix=.old \
"${ROOTDIR}/usr/share/xbps.d"/*-repository-*.conf "${ROOTDIR}/etc/xbps.d/"
sed -i "s|https://repo-default.voidlinux.org|${REPOSITORY%/}|g" "${ROOTDIR}/etc/xbps.d"/*-repository-*.conf
$XBPS_INSTALL -S -r "${ROOTDIR}"
set +e
}
reset_mirrors_int() {
local FILES
FILES="$(find "${ROOTDIR}"/usr/share/xbps.d/ -name '*-repository-*.conf' | sort -u | sed 's|/usr/share|/etc|g')"
DIALOG --cr-wrap --yesno "${BOLD}The following operations will be executed:${RESET}\n\n
The current mirror will be reset to the default.
The following files in ${BOLD}${ROOTDIR}/etc/xbps.d${RESET} will be removed, if they exist:
${RED}${FILES// /$'\n'}${RESET}\n\n
${BOLD}Do you want to continue?${RESET}" 20 80 || return
{
if reset_mirrors "$FILES"; then
printf "\nMirror successfully reset!\n"
else
printf "\nMirror reset failed! See log above for details.\n"
fi
} |& DIALOG --title " Resetting mirror to default... " --programbox 16 80
exit 0
}
reset_mirrors() {
local FILES="$1"
set -e
# shellcheck disable=SC2086
rm -f $FILES
$XBPS_INSTALL -S -r "${ROOTDIR}"
set +e
}
region_menu() {
local sel
DIALOG --title " Choose a Region " \
--menu "$MENULABEL" 10 78 0 \
"${regions[@]}"
sel="$(cat "$ANSWER")"
case $sel in
"Exit"|'') exit 0 ;;
"Default") reset_mirrors_int ;;
*) mirror_menu "$sel" ;;
esac
}
mirror_menu() {
local sel region="$1"
unset mirrors
declare -a mirrors
while read -r line; do
[ -z "$line" ] && continue
while IFS=$'\t' read -r url location tier rest; do
mirrors+=( "$url" "$location (Tier $tier)" )
done <<< "$line"
done <<< "${allmirrors[$region]}"
while true; do
DIALOG --title " Choose a Mirror (Region: $(region_name "$region")) " \
--menu "$MENULABEL" 10 78 0 \
"${mirrors[@]}"
sel="$(cat "$ANSWER")"
case $sel in
"") return ;;
*) write_mirrors_int "$sel" ;;
esac
done
}
TEMP=$(getopt -o 'l:r:s:dnvh' --long 'mirror-list:,rootdir:,set:,default,no-fetch,version,help' -n xmirror -- "$@")
# shellcheck disable=SC2181
if [ $? -ne 0 ]; then
usage
fi
eval set -- "$TEMP"
unset TEMP
while true; do
case "$1" in
'-l'|'--mirror-list') MIRRORLIST="$2"; NOFETCH=1; shift 2 ;;
'-r'|'--rootdir') ROOTDIR="$2"; shift 2 ;;
'-s'|'--set') SETMIRROR="$2"; shift 2 ; [ -z "$RESETMIRROR" ] || usage ;;
'-d'|'--default') RESETMIRROR=1; shift ; [ -z "$SETMIRROR" ] || usage ;;
'-n'|'--no-fetch') NOFETCH=1; shift ;;
'-v'|'--version') version ;;
'-h'|'--help') usage ;;
''|'--') break ;;
*) usage ;;
esac
done
check_root
if [ -n "$SETMIRROR" ]; then
check_required_programs
SETMIRROR="${SETMIRROR%%/current*}"
log "updating mirror to $SETMIRROR..."
if write_mirrors "$SETMIRROR"; then
log "mirror successfully updated to $SETMIRROR."
exit 0
else
for f in "${ROOTDIR}/etc/xbps.d"/*-repository-*.conf.old; do
mv "$f" "${f%.old}"
done
log "mirror update failed."
exit 1
fi
fi
if [ -n "$RESETMIRROR" ]; then
check_required_programs
log "xmirror: resetting mirror..."
FILES="$(find "${ROOTDIR}"/usr/share/xbps.d/ -name '*-repository-*.conf' | sort -u | sed 's|/usr/share|/etc|g')"
if reset_mirrors "$FILES"; then
log "xmirror: mirror successfully reset."
exit 0
else
log "xmirror: mirror reset failed."
exit 1
fi
fi
: "${MIRRORLIST:=${ROOTDIR}/usr/share/xmirror/mirrors.lst}"
: "${MIRRORURL:=@@MIRRORURL@@}"
declare -A allmirrors
declare -a regions
check_required_programs dialog
trap "die" INT TERM QUIT EXIT
get_mirrors
ANSWER=$(mktemp -t xmirror-XXXXXXXX) || exit 1
DIALOG --title "${BOLD} Void Linux Mirror Selection Wizard ${RESET}" --msgbox "$INITMSG" 15 80
while true; do
region_menu
done
# vim: set ft=bash: