#!/usr/bin/env bash

set -e

if [ $USER != root ]
then
    echo "This script should be run as root."
    exit 1
fi

if ! pacman -Qq pacman-mirrorlist &> /dev/null
then
    pacman -S pacman-mirrorlist
fi

if [ ! -f /etc/pacman.d/mirrorlist.pacnew ]
then
    cp /etc/pacman.d/mirrorlist /etc/pacman.d/mirrorlist.pacnew
fi

egrep -o 'Server = .+' /etc/pacman.d/mirrorlist.pacnew | /usr/bin/rankmirrors -n 6 - > /etc/pacman.d/mirrorlist