#!/bin/sh -e
#
# author: Karol Dudek

######################################################

if [ 4 != $# ]
then
  echo "Usage $0 [operator] [location] [download in Mb/s] [upload in Mb/s] e.g:"
  echo "  $0 netia krk_szklanedomy2a 600 100"
  echo "  $0 netia krk_kniaznina1 600 100"
  echo "  $0 netia krk_saska12c 600 100"
  echo "  $0 netia krk_dekerta2 600 100"
  echo "  $0 t-mobile tm_zawada 300 50"
  echo "  $0 ovh waw_groch 500 500"
  echo "  $0 plus krosna_lte 300 100"
  exit 1
fi

######################################################

OPERATOR=$1
LOCATION=$2
DOWNLOAD=$3
UPLOAD=$4

PREFIX=$1-$2-$3-$4

######################################################

get_url()
{
  echo "Download $1.."
  wget --no-iri -a $PREFIX.wget -O /dev/null -4 $1
}

######################################################

ping_host()
{
  echo "Ping $1.."
  ping -c 10 -4 $1 >> $PREFIX.ping
}

######################################################

rm -rf $PREFIX*

echo "Start speed test"

get_url 'http://noc.pirx.pl/500mb.bin'
get_url 'http://test.leon.pl/files/500MB.bin'
get_url 'https://noc.sileman.pl/500mb.sil'
get_url 'https://ftp.atman.pl/1gb'
get_url 'http://ftp.agh.edu.pl/ubuntu-releases/releases/16.04.7/ubuntu-16.04.6-server-i386.iso'
get_url 'http://ftp.icm.edu.pl/pub/Linux/distributions/slackware/slackware-iso/slackware-14.2-iso/slackware-14.2-install-d5.iso'
get_url 'https://proof.ovh.net/files/1Gb.dat'
get_url 'http://sun.sli.pl/speedtest/500MB.bin'
get_url 'http://sli.pl/speedtest/500MB.bin'

ping_host wp.pl
ping_host onet.pl
ping_host icm.edu.pl
ping_host dns.tpnet.pl
ping_host dns.orange.pl
ping_host dns.atman.pl
ping_host drive.google.com
ping_host facebook.com
ping_host speedtest.ipartners.pl
ping_host proof.ovh.net
ping_host sun.sli.pl

grep -E '(http| - )' $PREFIX.wget >> $PREFIX.result
grep -E '(---|rtt)' $PREFIX.ping | cut -d/ -f5 >> $PREFIX.result

tar czf result-$PREFIX.tar.gz $PREFIX.*
cp $PREFIX.result result-$PREFIX
rm $PREFIX*

cat result-$PREFIX
