#!/bin/bash
#
# slack-mirror-speedtest.sh
# Originally written for Ubuntu by Lance Rushing <lance_rushing@hotmail.com>
# Dated 9/1/2006
# Taken from http://ubuntuforums.org/showthread.php?t=251398
# This script is covered under the GNU Public License: http://www.gnu.org/licenses/gpl.txt
#
# Heavily modified for Slackware by Jeremy Brent Hansen <jebrhansen -at- gmail.com>
#
# NOTE: This won't work in Slackware 14.0 and earlier due to the OS lacking numfmt. It was
# introduced in the coreutils package in 14.1.
#
# Changelog
#
# 2016/07/07 - Updated MIRRORS variable to use EOF for easier copy/paste
# 2016/05/28 - Added additional comments for github upload
# 2015/11/06 - Initial release of slack-mirror-speedtest.sh

# Add or change mirrors from /etc/slackpkg/mirrors as desired (these are the US mirrors)
# Just place them between the read line and the EOF at the end. No quotes are necessary
read -r -d '' MIRRORS << 'EOF'
https://repository.plamolinux.org/pub/linux/Plamo/Plamo-7.x/x86_64/
ftp://plamo.linet.gr.jp/pub/Plamo-7.x/x86_64/
EOF

# Use any adequetly sized file to test the speed. This is ~7MB.
# The location should be based on the relative location within
# the slackware64-current tree. I originally tried a smaller 
# file (FILELIST.TXT ~1MB), but I was seeing slower speed results
# since it didn't have time to fully max my connection. Depending
# on your internet speed, you may want to try different sized files.
FILE="ChangeLog"

# Number of seconds before the test is considered a failure
TIMEOUT="5"

# Clear the string that results are stored in
RESULTS=""

# Set color variables to make results and echo statements cleaner
RED="\e[31m"
GREEN="\e[32m"
NC="\e[0m"  #No color

for MIRROR in $MIRRORS ; do
	
  echo -n "Testing ${MIRROR} "
	
  # Combine the mirror with the file to create the URL
  URL="${MIRROR}${FILE}"

  # Time the download of the file
  SPEED=$(curl -k --max-time $TIMEOUT --silent --output /dev/null --write-out %{speed_download} $URL)

  # If the speed is below ~10kb/s mark it as a failure
  if (( $(echo "$SPEED < 10000.000" | bc -l) )) ; then
    echo -e "${RED}Fail${NC}";
  else 
    # Use numfmt to convert to a pretty speed
    SPEED="$(numfmt --to=iec-i --suffix=B --padding=7 $SPEED)ps"
    echo -e "${GREEN}$SPEED${NC}"
    RESULTS="${RESULTS}\t${SPEED}\t${MIRROR}\n";
  fi

done;

# Display a sorted list to the prompt
echo -e "\nResults:"
echo -e $RESULTS | sort -hr

