#! /bin/csh -f
#
#		FTP throughput testing
#
#  2001-aug-1   first test with only one site
#  2001-aug-3   added 'site='


#------------------------------------------------------------------------------
#  set parameters:
set check_speed=1
set count=1
set sleep=0
set log=log1
set site=umd


#------------------------------------------------------------------------------
#  parse cmdline
foreach arg ($*)
   set $arg
end

#------------------------------------------------------------------------------

if ($site == umd) then
  set dir=ftp://ftp.astro.umd.edu/pub/astro
  #set file=saord_1.8_linux.tar.gz
  #set file=astromake.tar.gz
  set file=xgobi.tar.gz
else if ($site == ncsa) then
  set dir=ftp://ftp.ncsa.uiuc.edu/java/horizon/Latest/
  set file=Horizon-1.3.1beta.zip
else if ($site == tux) then
  set dir=ftp://ftp.tux.org
  set file=filelist.bz2
endif


#  work

set tmp=ftp-test.log
set url=$dir/$file
set rxd0=-1
set txd0=-1
set rxd1=-1
set txd1=-1

set myip=`/sbin/ifconfig eth0 | grep inet | awk '{print $2}'`

if (-e $file) then
   echo Cannot fetch an already existing file $file, since it would be deleted later
   exit 0
endif

again:



set rx0=`/sbin/ifconfig eth0 | grep 'RX pa' | awk '{print $2}' | awk -F: '{print $2}'`
set tx0=`/sbin/ifconfig eth0 | grep 'TX pa' | awk '{print $2}' | awk -F: '{print $2}'`


if ($check_speed) then
  sleep 2
  set rx1=`/sbin/ifconfig eth0 | grep 'RX pa' | awk '{print $2}' | awk -F: '{print $2}'`
  set tx1=`/sbin/ifconfig eth0 | grep 'TX pa' | awk '{print $2}' | awk -F: '{print $2}'`
  set rxd0=`expr $rx1 - $rx0`
  set txd0=`expr $tx1 - $tx0`
endif


wget -o $tmp.log $url
rm -f $file
set speed=`tail -2 $tmp.log | head -1 | awk '{print $2}' | sed s/\(//`

set rx1=`/sbin/ifconfig eth0 | grep 'RX pa' | awk '{print $2}' | awk -F: '{print $2}'`
set tx1=`/sbin/ifconfig eth0 | grep 'TX pa' | awk '{print $2}' | awk -F: '{print $2}'`

set rxd=`expr $rx1 - $rx0`
set txd=`expr $tx1 - $tx0`

if ($check_speed) then
  sleep 2
  set rx2=`/sbin/ifconfig eth0 | grep 'RX pa' | awk '{print $2}' | awk -F: '{print $2}'`
  set tx2=`/sbin/ifconfig eth0 | grep 'TX pa' | awk '{print $2}' | awk -F: '{print $2}'`
  set rxd1=`expr $rx2 - $rx1`
  set txd1=`expr $tx2 - $tx1`
endif


echo `date` $speed $rxd $txd $file $rxd0 $txd0  $rxd1 $txd1 $myip | tee -a $log


@ count--
if ($count > 0) then
  sleep $sleep
  goto again
endif


# rm $tmp
