#!/bin/bash

# Title:   download_speed.bash
# Puspose: Given the file name being downloaded,
#          chart the size difference every 2 seconds.
#          using dots..................
#
# Calculation:  delta kilobytes over delta seconds.
#
# Author:  john@stilen.com
#

##################################
#  Defign Functions
##################################

# Graphing the puppy
function delta_and_graph {

same="0"
last="1"
last_time=`date +%s`
while [ 1 ]; do			
  #Calculate the size of the file now
  cur=`du -sk $File_Name |awk '{print $1}'`
  cur_time=`date +%s`

  # if the file changed in size, calculate the difference
  if [[ "$cur" -ne "$last" ]]; then		
     let diff=$cur-$last    
     let diff_time=$cur_time-$last_time
     let rate=$diff/$diff_time
     
     # difference is too big to draw. devide by what ever makes sence
     let stars=$diff/1
     marker="." ;star=1
     while [ $star -lt $stars ]; do
       let star=$star+1
       marker="$marker."      
     done
     # draw the data
     echo "$marker = $diff/$diff_time =   $rate k/sec"
  fi
  sleep 2
  
  # assign the new size to the last size
  last="$cur"
done

}

#
# Did they call the script with a file name argument?
# ( i.e. /download_speed.bash <file name> )
#
function non_interactive {
  # if an argument was passed to the program
  # and that argument is a file that exists...
  echo "$test"
  if [ -n "$test" -a -f "$test" ]
  then      
      File_Name="$test" 
      
      # Graph the puppy
      delta_and_graph
      exit
  fi
}


function interactive {
 echo "Please enter the full path to the file."
 echo -e "FileName: \c"
 read file
  if [ -n "$file" -a -f "$file" ]
  then
      File_Name="$file"
      
      # Graph the puppy
      delta_and_graph
      exit   
  fi

}

#################################
#  Begin program
##################################
test="$1"
non_interactive
interactive
