#!/bin/sh
mirrorlist=ftp://ftp.debian.org/debian/README.mirrors.txt
nonuslist=ftp://ftp.debian.org/debian/README.non-US

if [ -x /usr/bin/curl ]; then
    get="curl -s"
elif [ -x /usr/bin/wget ]; then
    get="wget -O - -q"
else
    echo "Can't find curl or wget"
    exit 1
fi

mynetselect () {
xargs netselect -vv \
    | fgrep -v ' 0% ok' \
    | sort -n -t\[ +1 \
    | head -20
}

if [ "$1" = "-n" ]; then
    nonus=yes
fi

if [ ! "$nonus" = yes ]; then
$get $mirrorlist \
    | awk '/^ [A-Z]/ && $2 ~ /\./ {print $2}
	   /^ [A-Z]/ && $2 ~ /^[A-Z][a-z]*/ && $3 ~ /\./ {print $3}
	   /^[a-z][a-z]*\./ {print $1}' \
    | mynetselect
else
$get $nonuslist \
    | awk '/^   *(ftp|http):\/\/.*non-US\// {split($1,a,/\//);print a[3]}' \
    | uniq \
    | mynetselect
fi
