+initrepo_()
+{
+ if [ x"$3" != x"." ]; then
+ return
+ fi
+ case "$1" in
+ *$4)
+ base=${1%$4}
+ ;;
+ esac
+ case "$2" in
+ *$4)
+ pushbase=${2%$4}
+ ;;
+ esac
+}
+initrepo()
+{
+ base=
+ pushbase=
+ allrepos initrepo_ "`git config remote.origin.url`" "`git config remote.origin.pushurl`"
+ if [ -z "$base" ]; then
+ msg "The main repo is not xonotic.git, what have you done?"
+ exit 1
+ fi
+ msg "Found main repo = $base"
+ if [ -n "$pushbase" ]; then
+ msg "Found push repo = $pushbase"
+ fi
+}
+matchrepoflag()
+{
+ case ",$2," in
+ *",$1,"*)
+ return 0
+ ;;
+ *)
+ return 1
+ ;;
+ esac
+}
+testrepoflag_()
+{
+ [ x"$1" = x"$3" ] || return
+ if matchrepoflag "$6" "$2"; then
+ echo 0
+ fi
+}
+testrepoflag()
+{
+ allrepos testrepoflag_ "$1" "$2" | grep ^0 >/dev/null
+}
+
+mirrorspeed()
+{
+ # 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
+ # if we can't time, we only check availability
+ if ! $have_time; then
+ echo 0
+ return
+ fi
+ # now actually time it
+ (
+ set +x
+ export REPO="$1" # so that the sh -c subshell can use it
+ { measure_time sh -c 'git ls-remote "$REPO" refs/heads/master >/dev/null 2>&1'; } 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
+
+ if [ -z "$newprotocol" ]; then
+ newprotocol=$oldprotocol
+ fi
+ if [ -z "$newlocation" ]; then
+ newlocation=$oldlocation
+ fi
+
+ besturl=
+ bestlocation=
+ besttime=
+ bestcount=
+ bestmirror_benchmark()
+ {
+ bmb_curloc=$1
+ bmb_proto=$2
+ bmb_loc=$3
+ bmb_url=$4
+ bmb_fudge=$5
+
+ if [ -z "$bmb_loc" ]; then
+ # empty location is not allowed
+ return
+ fi
+ case " $newprotocol " in
+ *" "*)
+ # no protocol requested? all match
+ ;;
+ *" $bmb_proto "*)
+ ;;
+ *)
+ return
+ ;;
+ esac
+
+ # prefer location match
+ case " $newlocation " in
+ *" $bmb_loc "*)
+ # bmb_curloc is true in first run, false in second
+ # so first run gets all matching locations
+ # so second run gets all non-matching locations
+ if ! $bmb_curloc; then
+ return
+ fi
+ ;;
+ *)
+ if $bmb_curloc; then
+ return
+ fi
+ case " $newlocation " in
+ *" $bestlocation "*)
+ # worse
+ return
+ ;;
+ esac
+ ;;
+ esac
+
+ case " $newlocation " in
+ *" $bmb_loc "*)
+ # see below
+ ;;
+ *)
+ case " $newlocation " in
+ *" $bestlocation "*)
+ # worse
+ return
+ ;;
+ esac
+ ;;
+ esac
+ msg "Testing speed of $bmb_url..."
+
+ # only working mirrors
+ if ! thistime=`mirrorspeed "$bmb_url$testrepo"`; then
+ msg "-> FAILED"
+ return
+ fi
+ thistime=$(($thistime $bmb_fudge))
+ msg "-> $thistime"
+
+ # anything is better than nothing
+ if [ -z "$besttime" ]; then
+ besturl=$bmb_url
+ bestlocation=$bmb_loc
+ besttime=$thistime
+ bestcount=1
+ return
+ fi
+
+ # prefer location match
+ case " $newlocation " in
+ *" $bmb_loc "*)
+ case " $newlocation " in
+ *" $bestlocation "*)
+ # equality
+ ;;
+ *)
+ # better
+ besturl=$bmb_url
+ bestlocation=$bmb_loc
+ besttime=$thistime
+ bestcount=1
+ return
+ ;;
+ esac
+ ;;
+ *)
+ # if newlocation matches bestlocation, then we already discarded it above
+ ;;
+ esac
+
+ # 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=$bmb_url
+ bestlocation=$bmb_loc
+ besttime=$thistime
+ bestcount=1
+ return
+ fi
+ # both location and time match. Random decision.
+ bestcount=$(($bestcount + 1))
+ if [ $((($RANDOM + 0) % $bestcount)) -eq 0 ]; then
+ besturl=$bmb_url
+ bestlocation=$bmb_loc
+ fi
+ }
+ allmirrors bestmirror_benchmark true
+ allmirrors bestmirror_benchmark false
+ echo "$besturl"
+}
+
+testrepoflag_()
+{
+ [ x"$1" = x"$3" ] || return
+ case ",$6," in
+ *",$2,"*)
+ echo 0
+ ;;
+ *)
+ ;;
+ esac
+}
+testrepoflag()
+{
+ allrepos testrepoflag_ "$1" "$2" | grep ^0 >/dev/null
+}
+listrepos_()
+{
+ d=$1
+ f=$4
+ p="${d%dir}"
+ # if we have .no file, skip
+ if [ -f "$d.no" ]; then
+ msg "Repository $d disabled by a .no file, delete $d.no to enable"
+ return
+ fi
+ # if .yes file exists, always keep it
+ if [ -f "$d.yes" ]; then
+ msg "Repository $d enabled by a .yes file"
+ $ECHO "$d"
+ return
+ fi
+ # remove broken clones so they don't mess up stuff
+ if [ x"$d" != x"." ] && [ -d "$d" ] && ! [ -d "$d/.git" ]; then
+ msg "$d exists but has no .git subdir. Probably a broken clone. Deleting."
+ verbose rm -rf "$d"
+ return
+ fi
+ # if we have the dir, always keep it
+ if [ -d "$d" ]; then
+ msg "Repository $d enabled because it already exists"
+ $ECHO "$d"
+ return
+ fi
+ # if we have matching pk3, skip
+ if [ x"$p" != x"$d" ] && [ -f "$p" ]; then
+ msg "Repository $d disabled by matching .pk3 file, delete $p or create $d.yes to enable"
+ return
+ fi
+ # if "no" flag is set, skip
+ if matchrepoflag "$f" no; then
+ msg "Repository $d disabled by default, create $d.yes to enable"
+ return
+ fi
+ # default: enable
+ msg "Repository $d enabled by default"
+ $ECHO "$d"
+}
+
+listrepos()
+{
+ $ECHO `allrepos listrepos_`
+}
+initrepo
+repos=`listrepos`
+
+ifrepoenabled()
+{
+ eval ire_test=\$$(($1 + 3))
+ shift
+ case " $repos " in
+ *" $ire_test "*)
+ "$@"
+ ;;
+ esac
+}