Skip to content

Instantly share code, notes, and snippets.

Created August 5, 2014 15:20
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 anonymous/08ec03732f3da5c869df to your computer and use it in GitHub Desktop.
Save anonymous/08ec03732f3da5c869df to your computer and use it in GitHub Desktop.
#!/bin/bash
# Download pacman ranked mirror list
# Country codes: AU AT BY BE BR BG CA CL CN CO CZ DK EE FI FR DE GR HU IN IE IL
# IT JP KZ KR LV LU MK NL NC NZ NO PL PT RO RU RS SG SK ZA ES LK SE CH TW TR UA
# GB US UZ VN
country='US'
url="https://www.archlinux.org/mirrorlist/?country=US&protocol=http&ip_version=4&use_mirror_status=on"
# Download
tmp_ml=$(mktemp --suffix=-mirrorlist) # temporary mirrorlist
if curl -s "$url" -o "$tmp_ml"; then
if ! grep "^## Arch Linux repository mirrorlist" "$tmp_ml" > /dev/null; then
echo " Error: Download invalid"
exit 1
fi
else
echo " Error: Download failed"
exit 1
fi
# Edit
sed -i 's/^#Server/Server/g' "$tmp_ml"
# View
while true; do
read -p " View the downloaded mirrorlist? (y/n): " yn
case $yn in
[Yy] ) if hash vim 2>&- ; then # use vim if available
editor=vim
editop=$(printf '%s' +"set syn=sh");
else
editor=nano; fi
"$editor" "$editop" "$tmp_ml"
break 2 ;;
[Nn] ) break 2 ;;
[*] ) true
esac
done
# Permission test
prm_tst () {
if [ "$UID" != 0 ] && [ ! hash sudo 2>&- ] ; then
echo " Program "sudo" required for regular user"
exit 1
else
sudo=sudo
fi ; }
# Backup and Install
bck_ml () {
if [ -f /etc/pacman.d/mirrorlist ]; then
$sudo mv /etc/pacman.d/mirrorlist /etc/pacman.d/mirrorlist_$(date +%Y%m%d%H%M)
else
echo " Warning: no /etc/pacman.d/mirrorlist"
fi ; }
while true; do
read -p " Install new mirrorlist and backup previous? (y/n): " yn
case $yn in
[Yy] ) prm_tst
bck_ml
$sudo install -Dm644 "$tmp_ml" /etc/pacman.d/mirrorlist
break 2 ;;
[Nn] ) break 2 ;;
[*] ) true
esac
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment