#!/bin/sh -e

##########################################################################
#   Script description:
#       Quick and crude determination of fastest FreeBSD mirror site.
#       This method uses a single ping to approximate download speed
#       for each mirror.  This is unsophisticated since it uses only
#       a single ping and ping time does not necessarily correlate well
#       with transfer rate.  However, there is no way to accurately
#       predict future transfer rates, so we use the quickest method
#       possible to get a reasonable estimate, rather than waste time
#       trying to do the impossible.
#
#       Setting AUTO_PACKAGEROOT will enable auto-install-packages
#       to use the fastest mirror, without having PACKAGEROOT set
#       globally.
#
#       Bourne: export AUTO_PACKAGEROOT=`auto-fastest-mirror`
#       C:      setenv AUTO_PACKAGEROOT `auto-fastest-mirror`
#
#   Arguments:
#       -v      Verbose mode: echo ping times of each server.
#
#   Returns:
#       0
#       Echoes URL of fastest site
#
#   History:
#   Date        Name        Modification
#   2012-01-09  Jason Bacon Begin
##########################################################################

##########################################################################
#   Main
##########################################################################
    
case $(auto-ostype) in
FreeBSD)
    if [ x$1 = x'-v' ]; then
	verbose=1
    else
	verbose=0
    fi
    
    # Silence 'Alarm clock' message from shell when ping -t 1 times out
    # Doesn't work
    # trap SIGALRM
    
    # Start with primary server, then compare all mirrors
    fastest_mirror=''
    # ping sometimes outputs "Alarm clock"
    fastest_time=`ping -o -n -t 1 ftp.freebsd.org 2> /dev/null \
	    | fgrep -vi alarm \
	    | awk '$2 == "bytes" { print $7 }' | awk -F '=' ' { print $2 }' \
	    | awk -F '.' ' { print $1 }'`
    
    # Compensate if no response
    if [ $? != 0 ] || [ 0$fastest_time = 0 ]; then
	fastest_time=10000
    fi
    #printf "fastest_time = $fastest_time\n"
    
    # Check primary mirror sites only
    # https://www.freebsd.org/doc/handbook/mirrors-ftp.html
    # ftp5 is not a complete mirror
    # ftp14 is not a complete mirror
    
    for mirror in 1 2 3 4 6 7 8 9 10 11 12; do
	url=ftp$mirror.freebsd.org
	# ping sometimes outputs "Alarm clock"
	time=`ping -o -n -t 1 ftp$mirror.freebsd.org 2> /dev/null \
	    | fgrep -vi alarm \
	    | awk '$2 == "bytes" { print $7 }' | awk -F '=' ' { print $2 }' \
	    | awk -F '.' ' { print $1 }'`
	if [ $? != 0 ] || [ 0$time = 0 ]; then
	    time=10000
	    if [ $verbose = 1 ]; then
		printf "%s timed out.\n" $url
	    fi
	else
	    if [ $verbose = 1 ]; then
		printf "%s: %s\n" $url $time
	    fi
	fi
	#printf "ftp%s time = %s\n" $mirror $time
	
	if [ $time -le $fastest_time ]; then
	    fastest_time=$time
	    fastest_mirror=$mirror
	fi
	mirror=`expr $mirror + 1`
    done
    
    # Print name of fastest mirror
    printf "http://ftp%s.freebsd.org\n" $fastest_mirror
    ;;
    
*)
    auto-unsupported-os $0
    exit 1
    ;;

esac
