#!/bin/sh


STATUS_FOLDER=/var/tmp/
FILE_COUNT=10
SCRIPT=$(basename $0)
if [ $SCRIPT == "ftps" ]; then
   TERSE=TRUE
else
   TERSE=FALSE
fi


function total ()
{
  # $1 is the filename
  if [ $TERSE == "FALSE" ]; then
    cat  $1 | tr '\015' '\012'| grep ETA | tail -1 | cut -c 45-53 | sed -e 's/^ *//'
  else
    cat  $1 | tr '\015' '\012'| grep ETA | tail -1 | cut -c 45-53 | sed -e 's/^ *//' | cut -f1 -d" " 
  fi
}

function percent ()
{
  # $1 is the filename
  if [ $TERSE == "FALSE" ]; then
    cat  $1 | tr '\015' '\012'| grep ETA | tail -1 | cut -c 0-4 | sed -e 's/^ *//' 
  else
    cat  $1 | tr '\015' '\012'| grep ETA | tail -1 | cut -c 0-3 | sed -e 's/^ *//' | cut -f1 -d" " 
  fi
}

function rate ()
{
  # $1 is the filename
  if [ $TERSE == "FALSE" ]; then
    cat  $1 | tr '\015' '\012'| grep ETA | tail -1 | cut -c 54-66 | sed -e 's/^ *//'
  else
    cat  $1 | tr '\015' '\012'| grep ETA | tail -1 | cut -c 54-66 | sed -e 's/^ *//' | cut -f1 -d" " 
  fi
}

function eta ()
{
  # $1 is the filename
  if [ $TERSE == "FALSE" ]; then
    cat  $1 | tr '\015' '\012'| grep ETA | tail -1 | cut -c 67-79 | sed -e 's/^ *//'
  else
    cat  $1 | tr '\015' '\012'| grep ETA | tail -1 | cut -c 67-79 | sed -e 's/^ *//' | cut -f1 -d" "
  fi
}


function pid ()
{
  local BN=$(basename $1)
  echo ${BN##*_}
}

function server ()
{
  local BN=$(basename $1)
  expr $BN : "ftp_\([0-9.]*\)"
  #echo $SRV
}

# file listing of status files
function ls_files ()
{
  local FILE
  for FILE in $(ls -t ${STATUS_FOLDER}ftp_* | head -n ${FILE_COUNT}); do echo $FILE; done
}

function local_file ()
{
  local LINE=$(cat $1 | grep "local:")
  #echo "1 $1"
  #echo "LINE $LINE"
  expr "$LINE" : "local: \(.*\) remote:" 
  #echo $LN
  }

function remote_file ()
{
  local LINE=$(cat $1 | grep "local:")
  expr "$LINE" : ".*remote: \(.*\)"
}

function status ()
{
  local STAT=$(grep -c "^226-" $1)
  if [ $TERSE == "TRUE" ]; then
    echo $STAT
  elif [ $STAT == "0" ]; then
    echo "Tranferring"
  else
    echo "Complete"
  fi
}

function cleanup () 
{
  
  local CNT=$(ls ${STATUS_FOLDER}ftp_* | wc -l)
  let "EXCESS=CNT-FILE_COUNT"
  #old_IFS=$IFS
  if [ $EXCESS -gt 0 ]; then
    TO_DELETE=$(ls -Qt ${STATUS_FOLDER}ftp_* | tail -n $EXCESS)
    #IFS=' '
    #echo $TO_DELETE
    for FILE in $TO_DELETE; do
       #echo "Del ${FILE}"
	   local NN=$(echo $FILE | tr -d '\"')
	   #echo "D $NN"
       rm -f "${NN}"
	   #echo $?
    done
    #IFS=$old_IFS
  fi
}


function do_terse () 
{
  for FILE in $(ls_files); do
    echo "$(pid ${FILE}) $(server ${FILE}) \"$(local_file $FILE)\" \"$(remote_file $FILE)\" $(percent $FILE) $(rate ${FILE}) $(total ${FILE}) $(eta ${FILE}) $(status ${FILE})"
  done
}

function do_verbose ()
{
  for FILE in $(ls_files); do
    echo "Process:       $(pid ${FILE})"
    echo "Server:        $(server ${FILE})"
    echo "Local Name:    $(local_file $FILE)"
    echo "Remote Name:   $(remote_file $FILE)"
    echo "Percent Comp:  $(percent $FILE)"
    echo "Rate:          $(rate ${FILE})" 
    echo "Total Trans:   $(total ${FILE})"
    echo "ETA:           $(eta ${FILE})"
    echo "Status:        $(status ${FILE})"
    echo "--------------------------------------"
  done
}

function do_status ()
{
  if [ ${SCRIPT} = "ftps" ]; then 
    do_terse
  else
    do_verbose
  fi
}


if [ "$#" -eq 0 ]; then
  do_status
else
  cleanup
fi

exit 0