summarylogtreecommitdiffstats
path: root/mirrorlist-rankmirrors.sh
blob: 2b3f365f5fa2c8171b2bba20d640ce38ef59e644 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#!/bin/sh

CONFIG_FILE="/etc/pacman.d/mirrorlist-rankmirrors.conf"
MIRRORLIST="/etc/pacman.d/mirrorlist"
TMP_FILE="/tmp/mirrorlist"

if [ "$(id -u)" != 0 ]; then
	echo "error: you cannot perform this operation unless you are root."
	exit 1
fi

# shellcheck source=/etc/pacman.d/mirrorlist-rankmirrors.conf
. "$CONFIG_FILE"
if [ -z "$COUNTRY" ]; then
	echo "error: file $CONFIG_FILE is not configured."
	exit 2
fi

[ -f "$MIRRORLIST.pacnew" ] && file="$MIRRORLIST.pacnew" || file="$MIRRORLIST"
if [ ! -f "$file" ]; then
	echo "error: file $file doesn't exist."
	exit 3
fi

if grep -q "# Server list generated by rankmirrors" "$file"; then
	echo "==> There is nothing to do, file $file has already been processed."
	exit 0
fi

if [ "$COUNTRY" = "All" ]; then
	sed 's/#Server/Server/g' "$file" > "$TMP_FILE"
else
	awk '/^## '"$COUNTRY"'/{f=1}f==0{next}/^$/{exit}{print substr($0, 2)}' "$file" > "$TMP_FILE"
fi

if [ -s "$TMP_FILE" ]; then
	echo "==> rankmirrors $file"
	rankmirrors "$TMP_FILE" | tee "$MIRRORLIST" && rm "$TMP_FILE"
	if [ -f "$MIRRORLIST.pacnew" ]; then
		rm "$MIRRORLIST.pacnew"
	fi
	count=$(grep -c -v "#" "$MIRRORLIST")
	echo "==> Finished: $count mirrors ranked"
else
	echo "error: there is no mirror for country $COUNTRY."
	exit 4
fi