--- /dev/null
+allmirrors()
+{
+ "$@" git '' git://git.xonotic.org/xonotic/ ''
+ "$@" http '' http://git.xonotic.org/xonotic/ ''
+ "$@" ssh '' ssh://xonotic@git.xonotic.org/ ''
+
+ "$@" ssh push ssh://xonotic@push.git.xonotic.org/ ''
+
+ "$@" git de git://de.git.xonotic.org/xonotic/ ''
+ "$@" http de http://de.git.xonotic.org/xonotic/ ''
+
+ "$@" git nl git://nl.git.xonotic.org/xonotic/ '*3/2'
+ "$@" http nl http://nl.git.xonotic.org/xonotic/ '*3/2'
+
+ "$@" git us git://us.git.xonotic.org/xonotic/ ''
+ "$@" http us http://us.git.xonotic.org/xonotic/ ''
+}
+
+allrepos()
+{
+ "$@" . xonotic.git master ""
+ "$@" data/xonotic-data.pk3dir xonotic-data.pk3dir.git master ""
+ "$@" data/xonotic-music.pk3dir xonotic-music.pk3dir.git master ""
+ "$@" data/xonotic-nexcompat.pk3dir xonotic-nexcompat.pk3dir.git master "no"
+ "$@" darkplaces darkplaces.git div0-stable "svn"
+ "$@" netradiant netradiant.git master ""
+ "$@" div0-gittools div0-gittools.git master "no"
+ "$@" d0_blind_id d0_blind_id.git master ""
+ "$@" data/xonotic-maps.pk3dir xonotic-maps.pk3dir.git master ""
+ "$@" mediasource mediasource.git master "no"
+ "$@" fteqcc fteqcc.git xonotic-stable "noautocrlf"
+}
+
+mirrorspeed()
+{
+ if ! { time -p true; } >/dev/null 2>&1; then
+ echo 0
+ return
+ fi
+ # first result is to be ignored, but we use it to check status
+ git ls-remote "$1" refs/heads/master >/dev/null 2>&1 || return 1
+ # now actually time it
+ (
+ set +x
+ { time -p git ls-remote "$1" refs/heads/master; } 2>&1 >/dev/null | head -n 1 | cut -d ' ' -f 2 | tr -d . | sed 's,^0*,,' | grep . || echo 0
+ # unit: clock ticks (depends on what "time" returns
+ )
+}
+bestmirror()
+{
+ oldurl="$1"
+ newprotocol="$2"
+ newlocation="$3"
+ oldprotocol=
+ oldlocation=
+ testrepo=
+ bestmirror_firstrepo()
+ {
+ if [ -z "$testrepo" ]; then
+ testrepo=$2
+ fi
+ }
+ allrepos bestmirror_firstrepo
+ bestmirror_findold()
+ {
+ if [ x"$oldurl" = x"$3" ]; then
+ oldprotocol=$1
+ oldlocation=$2
+ fi
+ }
+ allmirrors bestmirror_findold
+ besturl=
+ bestlocation=
+ besttime=
+ bestcount=
+ bestmirror_benchmark()
+ {
+ if [ -z "$2" ]; then
+ # empty location is not allowed
+ return
+ fi
+ if [ x"$1" != x"$newprotocol" ]; then
+ return
+ fi
+ # only working mirrors
+ if ! thistime=`mirrorspeed "$3$testrepo"`; then
+ return
+ fi
+ thistime=$(($thistime $4))
+ # anything is better than nothing
+ if [ -z "$besttime" ]; then
+ besturl=$3
+ bestlocation=$2
+ besttime=$thistime
+ bestcount=1
+ return
+ fi
+ # prefer location match
+ if [ x"$2" = x"$newlocation" ]; then
+ if [ x"$bestlocation" != x"$newlocation" ]; then
+ besturl=$3
+ bestlocation=$2
+ besttime=$thistime
+ bestcount=1
+ return
+ fi
+ else
+ if [ x"$bestlocation" = x"$newlocation" ]; then
+ return
+ fi
+ fi
+ # if we get here, we must compare mirror speed as we have more than one match
+ if [ $thistime -gt $besttime ]; then
+ return
+ elif [ $thistime -lt $besttime ]; then
+ besturl=$3
+ besttime=$thistime
+ bestcount=1
+ return
+ fi
+ # both location and time match. Random decision.
+ bestcount=$(($bestcount + 1))
+ if [ $((($RANDOM + 0) % $bestcount)) -eq 0 ]; then
+ besturl=$3
+ fi
+ }
+ allmirrors bestmirror_benchmark
+ echo "$besturl"
+}
+