#!/usr/bin/bash # Download pacman ranked mirrorlist countries=(AT AU BE BG BR BY CA CH CL CN CO CZ DE DK EE ES FI FR GB GR HU IE IL IN IT JP KR KZ LK LU LV MK NC NL NO NZ PL PT RO RS RU SE SG SK TR TW UA US UZ VN ZA) country="" [ "$1" ] && country="$1" # Usage display if incorrect number of parameters given if [ $# -gt 1 -o "$1" = -h -o "$1" = --help ]; then echo "${0##*/} [*country code] - download pacman ranked mirrorlist" echo " "${countries[@]}"" | fmt -c -w 80 exit 1; fi # Root user access test if [ $EUID != 0 ]; then echo "Root user access required"; exit 1; fi # Select country from list if ! [ "$country" ]; then PS3="Select country: " select country in "${countries[@]}"; do test -n "$country" && break echo "Select 1, or 2..." done; fi # Test if $country is in $countries array if ! [[ " ${countries[*]} " == *" $country "* ]]; then echo "Invalid country code." exit 1; fi # Download url="https://www.archlinux.org/mirrorlist/?country=${country}&protocol=http&ip_version=4&use_mirror_status=on" 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=${EDITOR}; fi "$editor" "$editop" "$tmp_ml" break 2 ;; [Nn] ) break 2 ;; [*] ) true esac done # Backup bck_ml () { if [ -f /etc/pacman.d/mirrorlist ]; then mv /etc/pacman.d/mirrorlist /etc/pacman.d/mirrorlist_$(date +%Y%m%d%H%M) else echo "Warning: no /etc/pacman.d/mirrorlist" fi ; } # Install while true; do read -p "Install new mirrorlist and backup previous? (y/n): " yn case $yn in [Yy] ) bck_ml install -Dm644 "$tmp_ml" /etc/pacman.d/mirrorlist break 2 ;; [Nn] ) break 2 ;; [*] ) true esac done