Created
February 9, 2020 00:35
-
-
Save b0o/07ce4daab6000e24efad1d9183f58577 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
set -euo pipefail | |
mlnew="/etc/pacman.d/mirrorlist.pacnew" | |
mldest="/etc/pacman.d/mirrorlist" | |
if [[ ! -f "$mlnew" ]]; then | |
echo "mirrorlist is up-to-date" >&2 | |
exit | |
fi | |
proto="https:" | |
if [[ ${1:-""} =~ --?i(nsecure)? ]]; then | |
proto="https?:" | |
shift | |
fi | |
country="${1:-"United States"}" | |
mapfile -t sc << EOF | |
BEGIN { a=0 }; | |
/## $country/{ b=1; next }; | |
/##/{ b=0 }; | |
/#Server = $proto/{ if(b == 1) print "Server = " \$3 } | |
EOF | |
tmp="$(mktemp -u "/tmp/mirrorlist-XXXX")" | |
awk "${sc[*]}" < "$mlnew" > "$tmp" || { | |
echo "awk failure" >&2 | |
exit 1 | |
} | |
echo "ranking mirrors..." >&2 | |
rankmirrors -vn 10 "$tmp" | tee "${tmp}-ranked" | |
echo "rankmirrors success" >&2 | |
echo "installing new mirrorlist to $mldest..." >&2 | |
sudo mv "${tmp}-ranked" "$mldest" | |
echo "removing $mlnew..." >&2 | |
sudo rm "$mlnew" | |
echo "cleaning up..." >&2 | |
rm "$tmp" | |
echo "$0 finished" >&2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment