Skip to content

Instantly share code, notes, and snippets.

@b0o
Created February 9, 2020 00:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save b0o/07ce4daab6000e24efad1d9183f58577 to your computer and use it in GitHub Desktop.
Save b0o/07ce4daab6000e24efad1d9183f58577 to your computer and use it in GitHub Desktop.
#!/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