#!/bin/bash
# script to get entity data from various source via command line options.
# It assumes that ping, cat, wget and bax2csv are available on the computer
# this version for running in linux needs testing on msys
# 19 November 2015 version
 
# Command line options - ask for help
# ./entget.bash --help

# clear local variables
help=0
havedestination=0
havedestinationapp=0
isip=0
isftp=0
isfile=0
haveuser=0
havepassword=0
haveduration=0
havelast=0
havestart=0
haveend=0
test=0
silent=0
verbose=0
baxname=0
focus=0
run=0
DEST=./
DESTAPP=./bax2csv
ENAME=
TOPIC=
STARTDATE=
ENDDATE=
# LRS uses 2000 as the epoch so difference with Unix Epoch
# is -946684800 seconds. But date call can get it off by 3600 seconds
EPOCH=946684800
# EPOCH=946681200

# gather command line tokens
if [ $# -ne 0 ]; then
   for i do
      case "$i" in
      -h | --help)    help=1;      shift;;
      -u | -U | --user) shift;  USER=$1; haveuser=1;   shift;;
      -p | -P | --password) shift;  PWD=$1; havepassword=1;   shift;;
      -d | -D | --destination)  shift;   DEST=$1;  havedestination=1; shift;;
      -ad | -Ad | --appdestination)  shift;   DESTAPP=$1;  havedestinationapp=1; shift;;
      -test) shift;  ATRIB=$1; test=1;   shift;;
      -ip | -IP) shift;  ROUTER=$1; FILE=$2; isip=1; shift; shift;;
      -file) shift;     FILE=$1; isfile=1;  shift;;
      -ftp) shift;  ROUTER=$1; FILE=$2; isftp=1; shift; shift;;
      -env) shift;     ENAME=$1; baxname=1; shift;;
      -dur | -duration) shift;  T60=$1; haveduration=1; shift;;
      -last) shift;  T60=$1; haveduration=0; havelast=1; shift;;
      -start) shift;  STARTDATE=$1; havestart=1; haveduration=0; shift;;
      -end) shift;  ENDDATE=$1; haveend=1; haveduration=0; shift;;
      -focus) shift; TOPIC=$1; focus=1; shift;;
      -rf) shift; ROUTER=$1; run=1; shift;;
      esac
   done

# debug
   echo "got $help : $test : $isip : $run : $isip : $isftp : $haveuser : $havepassword : $baxname : $focus :$FILE : $ROUTER : $DEST : $ATRIB : $ATRIBA : $ATRIBB : $ATRIBC : $ATRIBD : $ENAME : $TOPIC."
   if [[ $test -eq 0 && $isfile -eq 0 && $isip -eq 0 && $isftp -eq 0  && $run -eq 0 ]]; then
     help=1
     echo " "
     echo " Did not understand command line tokens..."
   fi
fi

if [ $help -eq 1 ]; then
  echo " "
  echo " USAGE: entget.bash [OPTIONS]"
  echo "   where:"
  echo "   -h --help  (prints this message)"
  echo "   -u -U --user <user_name>  (for ftp access)"
  echo "   -p -P --password <user_password> (for ftp access)"
  echo "   -d -D --destination <folder>  (where to place files)"
  echo "   -ad -Ad -appdestination <executable or script>"
  echo "      (what to run locally to postprocess data)."
  echo "   -ip -IP <ip_address,filename> and if filename"
  echo "      is - the name is based on current time."
  echo "      note: must be preceeded by -u and -p commands and"
  echo "      -ad command if postprocessing also to be carried out."
  echo "   -ftp <ip_address,remote-file>"
  echo "      note: must be preceeded by -u and -p commands"
  echo "      and the remote-file should include the relative path"
  echo "      e.g. pub/myfile.txt. If preceeded by"
  echo "      -ad command postprocessing also carried out."
  echo "   -file <local_file> File on destination machine. If preceeded"
  echo "      by -ad command postprocessing also carried out."
  echo "   -dur -duration <seconds of data requested> is required for"
  echo "      -ip sources if -start -end not given"
  echo "   -last <instances of data requested> is required for"
  echo "      -ip sources if -start -end or -dur not given"
  echo "   -start <date at start> e.g. '2015-10-02 10:25' '2015/10/02 10:25'"
  echo "      or 'Fri Oct  2 10:35:26 BST 2015' in double quotes"
  echo "   -end <date at end> e.g. '2015-10-02 10:25' '2015/10/02 10:25'"
  echo "      or 'Fri Oct  2 10:35:26 BST 2015' in double quotes"
  echo "   -env <ENV sensor id>, (8 character name)"
  echo "   -f <dbt|rh|lux|pirc|sw|bat> (focus on a specific measurement)"
  echo " "
  echo "   -test [verbose|silent] reveal the underlying communications in chosen"
  echo "      method (see examples below)"
  echo "  "
  echo " Example of -test for an IP using wget to get newfetch and convert data:"
  echo " ./entget.bash -ad ./bax2csv -test verbose -ip 130.159.47.84 newfetch"
  echo " Example of -test for an IP using wget to get newfetch or test it is live:"
  echo " ./entget.bash -test silent -ip 130.159.47.84 newfetch"
  echo " ./entget.bash -test verbose -u user -p pass -ip 130.159.47.84"
  echo " Example of -test whether a local file exists"
  echo " ./entget.bash -test verbose -file TEST.txt"
  echo " Example of -test whether we can fetch a file TEST.txt in remote folder pub"
  echo " ./entget.bash -u user -p pass -test verbose -ftp 130.159.46.241 pub/TEST.txt"
  echo "  "
  echo " Example of using -ftp to get file TEST.txt in remote folder pub"
  echo " ./entget.bash -u esrusvn -p esru_2013 -ftp 130.159.46.241 pub/TEST.txt"
  echo " Example of using -ip to download 600 seconds of raw data into a file named 11h30"
  echo " ./entget.bash -u user -p pass -ip 130.159.47.84 11h30 -dur 600"
  echo " Example of using -ip to download data between two dates into a file named"
  echo "   oct10morning (please use double quotes for dates)"
  echo " ./entget.bash -u user -p pass -ip 130.159.47.84 oct10morning -start '2015-10-07 00:00:01' -end '2015-10-07 07:20:00'"
  echo " Example of using -ip to download 600 seconds and put into a file based on current time"
  echo " ./entget.bash -u user -p pass -ad ./bax2csv -ip 130.159.47.84 - -dur 600"
  echo " Example of using -ip to download 600 seconds of raw data into the file named 11h45 "
  echo " ./entget.bash -u user -p pass -ad ./bax2csv -ip 130.159.47.84 11h45 -dur 600"
  echo " as above but focused on a sensor 42D89A75 and focus on temperature. "
  echo " ./entget.bash -u user -p pass -ad ./bax2csv -ip 130.159.47.84 11h45 -dur 600 -env 42D89A75 -focus dbt"
  echo "  "
  echo " Example of using -file with -ad set to extract humidity data to a csv file and move to /tmp"
  echo " ./entget.bash -d /tmp -ad ./bax2rh -file 2014-12-22-15-00"
  echo "  "
  echo " Example test repeating acquisition of 60 seconds of data every 60 seconds and put csv files in /tmp"
  echo " ./entget.bash -ad ./bax2csv -d /tmp -rf 130.159.47.84 -dur 60"
  exit
fi

# BAX related
if [ $baxname -eq 1 ]; then
  echo "ENV name is...$ENAME"
fi
if [ $focus -eq 1 ]; then
  echo "focus is...$TOPIC"
fi
if [ $haveduration -eq 1 ]; then
  echo "duration is...$T60 seconds"
fi
if [ $havelast -eq 1 ]; then
  echo "request for last...$T60 instances"
fi
if [ $havestart -eq 1 ]; then
  echo "starting at...$STARTDATE until $ENDDATE"
fi

# Check if -ip, -ftp or -file decode the command line tokens.
if [ $isip -eq 1 ]; then
   echo "IP address is...$ROUTER"
   echo "File is...$FILE"
   if [ $test -eq 1 ]; then
     if [ $havelast -eq 1 ]; then
       echo "requesting last $T60 instances"
     else
       haveduration=1
       echo "using a duration..."
     fi
   fi
   if [[ $haveduration -eq 0 && $havestart -eq 0 && $havelast -eq 0 ]]; then
     echo " "
     echo " Need duration of data to be defined or the"
     echo " start and end of the period to be defined or"
     echo " last x entries to be defined."
     exit
   fi
fi
if [ $isftp -eq 1 ]; then
   echo "FTP address is...$ROUTER"
   echo "File is...$FILE"
   if [ $haveuser -eq 0 ]; then
     echo " "
     echo " Need user name for ftp."
     exit
   fi
   if [ $havepassword -eq 0 ]; then
     echo " "
     echo " Need user password for ftp."
     exit
   fi
fi
if [ $isfile -eq 1 ]; then
   echo "file is...$FILE"
fi
if [ $run -eq 1 ]; then
   echo "IP address is...$ROUTER"
   if [ $haveduration -eq 0 ]; then
     echo " "
     echo " Need duration of data to be defined. "
     exit
   fi
   echo "repeat every $T60 seconds"
fi

# Command line included -test
if [ $test -eq 1 ]; then

# If source is file
   if [ $isfile -eq 1 ]; then
     if [ -e $FILE ]; then
       case "$ATRIB" in
         silent | Silent) silent=1;;
         verbose | Verbose) verbose=1;;
       esac
       if [ $verbose -eq 1 ]; then
         echo "contents of file $FILE"
         cat $FILE
         exit
       else
         echo "file $FILE exists"
         exit
       fi
     else
       echo " File does not exist."
       exit
     fi
   fi

# if source if ftp
   if [ $isftp -eq 1 ]; then
     if [ $haveuser -eq 0 ]; then
       echo " "
       echo " Need user name for ftp."
       exit
     fi
     if [ $havepassword -eq 0 ]; then
       echo " "
       echo " Need user password for ftp."
       exit
     fi

     case "$ATRIB" in
       silent | Silent) silent=1;;
       verbose | Verbose) verbose=1;;
     esac
     if [ $verbose -eq 1 ]; then
# file name should include relative path from the default folder that
# the server starts on e.g. pub/the_file.txt  Set FN as the file w/o path
       /bin/rm -f $FILE
       FN=$(basename "$FILE")
       echo "test fetching file $FILE to $FN"
       wget --tries=1 --ftp-user=$USER --ftp-password=$PWD ftp://$ROUTER/$FILE
       if [ -e $FN ]; then
         echo "a passing $havedestinationapp $baxname $focus"
         if [ $havedestinationapp -eq 1 ]; then
           if [ $baxname -eq 1 ]; then
             if [ $focus -eq 1 ]; then
               $DESTAPP -file $FILE $FILE.csv -N $ENAME -f $TOPIC
             else
               $DESTAPP -file $FILE $FILE.csv -n $ENAME
             fi
           else
             if [ $focus -eq 1 ]; then
               $DESTAPP -file $FILE $FILE.csv -f $TOPIC
             else
               $DESTAPP -file $FILE $FILE.csv
             fi
           fi
           if [ -e $FILE.csv ]; then
             echo "converted $FILE to $FILE.csv and contents..."
             /bin/cat $FILE.csv
             echo "  "
           fi
         else
           echo "contents of $FN..."
           /bin/cat $FN
           echo "  "
         fi
       else
         echo " Failed attempt."
       fi
       exit
     else
# file name should include relative path from the default folder that
# the server starts on e.g. pub/the_file.txt  Set FN as the file w/o path
       /bin/rm -f $FILE
       FN=$(basename "$FILE")
       echo "test fetching file $FILE to $FN"
       wget --tries=1 --quiet --ftp-user=$USER --ftp-password=$PWD ftp://$ROUTER/$FILE
       if [ -e $FN ]; then
         echo "b passing $havedestinationapp $baxname $focus"
         if [ $havedestinationapp -eq 1 ]; then
           if [ $baxname -eq 1 ]; then
             if [ $focus -eq 1 ]; then
               $DESTAPP -file $FILE $FILE.csv -N $ENAME -focus $TOPIC
             else
               $DESTAPP -file $FILE $FILE.csv -N $ENAME
             fi
           else
             if [ $focus -eq 1 ]; then
               $DESTAPP $FILE $FILE.csv -f $TOPIC
             else
               $DESTAPP $FILE $FILE.csv
             fi
           fi
           if [ -e $FILE.csv ]; then
             echo "converted $FILE to $FILE.csv"
           fi
         else
           echo "ok."
         fi
       else
         echo " Failed attempt."
       fi
       exit
     fi
   fi

# test for case of source is an ip address
   if [ $isip -eq 1 ]; then
#      echo "IP address is...$ROUTER"
# Try a ping of the LRS, if that fails exit.
     ping -c 1 $ROUTER &> /dev/null
     if [ $? -ne 0 ]; then
       echo "LRS with IP: $ROUTER was not found."
       exit
     fi
     if [ $test -eq 1 ]; then
       echo "LRS with IP: $ROUTER was found."
       exit
     fi

     echo " "
     rm -f $FILE
     rm -f $FILE.csv

#   initial contact to setup session
     wget --quiet --tries=1 --save-cookies cookies.txt --delete-after --post-data 'user='${USER}'&pass='${PWD} --keep-session-cookies http://${ROUTER}/WWW/login.htm

     if [ $havelast -eq 1 ]; then
       echo "request for last...$T60 instances"
# second call to request last T60 data items
       wget --quiet --load-cookies cookies.txt -O $FILE --read-timeout=5 --no-http-keep-alive --content-disposition http://${ROUTER}/fetch?type=LS\&start=${T60}\&end=
     else
# second call to request last 20 data items
       wget --quiet --load-cookies cookies.txt -O $FILE --read-timeout=5 --no-http-keep-alive --content-disposition http://${ROUTER}/fetch?type=LS\&start=20\&end=
     fi
     $DESTAPP -file $FILE $FILE.csv
     if [ -e $FILE.csv ]; then
       case "$ATRIB" in
         silent | Silent) silent=1;;
         verbose | Verbose) verbose=1;;
       esac
       if [ $verbose -eq 1 ]; then
         echo "Success. Most recent data is..."
         cat $FILE.csv
       else
         echo "Success."
       fi
     else
       echo " "
       echo " Failed attempt."
    fi
    exit
  fi
fi

# user used -ftp and if we got this far then ok attempt fetch
# file name should include relative path from the default folder that
# the server starts on e.g. pub/the_file.txt
# NEED TO REMOVE the pub/ from $FILE 
if [ $isftp -eq 1 ]; then
  /bin/rm -f $FILE
  echo "fetching file $FILE"
  wget --tries=1 --quiet --ftp-user=$USER --ftp-password=$PWD ftp://$ROUTER/$FILE
  if [ -e $FILE ]; then
    echo "c passing $havedestinationapp $baxname $focus"
    if [ $havedestinationapp -eq 1 ]; then
      if [ $baxname -eq 1 ]; then
        if [ $focus -eq 1 ]; then
          $DESTAPP -file $FILE $FILE.csv -N $ENAME -f $TOPIC
        else
          $DESTAPP -file $FILE $FILE.csv -n $ENAME
        fi
      else
        if [ $focus -eq 1 ]; then
          $DESTAPP -file $FILE $FILE.csv -f $TOPIC
        else
          $DESTAPP -file $FILE $FILE.csv
        fi
      fi
      if [ -e $FILE.csv ]; then
        if [ $havedestination -eq 1 ]; then
          mv $FILE.csv $DEST/$FILE.csv
          echo "and moved to $DEST/$FILE.csv"
        else
          echo "converted $FILE to $FILE.csv"
        fi
      fi
    else
      echo "$FILE downloaded sucessfully."
      if [ $havedestination -eq 1 ]; then
        mv $FILE $DEST/$FILE
        echo "and moved to $DEST/$FILE"
      fi
    fi
  fi
  exit
fi

# user used -ip and if we got this far use wget to recover X seconds of data
# or the start-to-end period passed in the command line.
if [ $isip -eq 1 ]; then
  case "$FILE" in
    -) FILE=$(date +%Y-%m-%d-%H-%M -d now);;
  esac
  echo $FILE
  if [ $haveduration -eq 1 ]; then
    echo $T60

# In case computer time is different from LRS define offset
# This needs to be edited to fit the users computer
    THR=3600

# the current time for LRS
    NOW=$(($(date +%s -d now)-$EPOCH))
    echo $NOW
    TSTART=$(($NOW-$THR))
# Adjust start to subtract T60
    TSTARTADJ=$(($TSTART-$T60))
    echo $TSTART
    echo $TSTARTADJ
    TEND=`expr $TSTARTADJ + $T60`
    echo $TEND
  fi
# user has passed a start and end date adjust to the LRS epoch
  if [ $havestart -eq 1 ]; then
    echo "starting at...$STARTDATE until $ENDDATE"
    TSTART=$(($(date +%s -d "$STARTDATE")-$EPOCH))
    echo $TSTART
    TSTARTADJ=$(($(date +%s -d "$STARTDATE")-$EPOCH))
    echo $TSTARTADJ
    TEND=$(($(date +%s -d "$ENDDATE")-$EPOCH))
    echo $TEND
  fi
  rm -f $FILE
  rm -f $FILE.csv

# initial contact to setup session
#  wget --save-cookies cookies.txt --delete-after --post-data 'user=admin&pass=password' --keep-session-cookies http://${ROUTER}/WWW/login.htm
  wget --save-cookies cookies.txt --delete-after --post-data 'user='${USER}'&pass='${PWD} --keep-session-cookies http://${ROUTER}/WWW/login.htm
     if [ $havelast -eq 1 ]; then
# second call to request last T60 data items
       wget --load-cookies cookies.txt -O $FILE  --tries=10 --read-timeout=5 --no-http-keep-alive --content-disposition http://${ROUTER}/fetch?type=LS\&start=${T60}\&end=
     else

# second call to request data
       wget --load-cookies cookies.txt -O $FILE --tries=10 --read-timeout=5 --no-http-keep-alive --content-disposition http://${ROUTER}/fetch?type=BT\&start=${TSTARTADJ}\&end=${TEND}
     fi

# Post-processing if $havedestinationapp set. Typically DESTAPP will be
# a script which carries out the details or invokes other agents.
  echo "d passing $havedestinationapp $baxname $focus"
  if [ $havedestinationapp -eq 1 ]; then
    if [ $baxname -eq 1 ]; then
      if [ $focus -eq 1 ]; then
        if [ $havelast -eq 1 ]; then
          $DESTAPP -file $FILE $FILE.csv -N $ENAME -f $TOPIC -i
        else
          $DESTAPP -file $FILE $FILE.csv -N $ENAME -f $TOPIC
        fi
      else
        if [ $havelast -eq 1 ]; then
          $DESTAPP -file $FILE $FILE.csv -n $ENAME -i
        else
          $DESTAPP -file $FILE $FILE.csv -n $ENAME
        fi
      fi
    else
      if [ $focus -eq 1 ]; then
        if [ $havelast -eq 1 ]; then
          $DESTAPP -file $FILE $FILE.csv -f $TOPIC -i
        else
          $DESTAPP -file $FILE $FILE.csv -f $TOPIC
        fi
      else
        if [ $havelast -eq 1 ]; then
          $DESTAPP -file $FILE $FILE.csv -i
        else
          $DESTAPP -file $FILE $FILE.csv
        fi
      fi
    fi
    if [ -e $FILE.csv ]; then

# if we are asking for the last T60 data lines use wc
# to find number of lines A. If < T60 get ratio plus a
# bit and the re-issue wget with T60NEW.
      if [ $havelast -eq 1 ]; then
        A=`wc $FILE.csv`
        IFS=' ' read -r -a array <<< "$A"
        echo "number of data lines captured is ${array[0]}"
        B=${array[0]}
        if [ $B -lt ${T60} ]; then
	  C=$(($T60/$B))
	  T60NEW=$(($T60*$(($C+1))))
	  echo "requesting more...multipler is $C which is $T60NEW entries."
	  rm -f $FILE.csv
          echo "wget request for last...$T60NEW instances"
# second call to request last T60NEW data items
          wget --load-cookies cookies.txt -O $FILE  --tries=10 --read-timeout=5 --no-http-keep-alive --content-disposition http://${ROUTER}/fetch?type=LS\&start=${T60NEW}\&end=
          if [ $baxname -eq 1 ]; then
            if [ $focus -eq 1 ]; then
              $DESTAPP -file $FILE $FILE.csv -N $ENAME -f $TOPIC -i
            else
              $DESTAPP -file $FILE $FILE.csv -n $ENAME -i
            fi
          else
            if [ $focus -eq 1 ]; then
              $DESTAPP -file $FILE $FILE.csv -f $TOPIC -i
            else
              $DESTAPP -file $FILE $FILE.csv -i
            fi
          fi
          A=`wc $FILE.csv`
          IFS=' ' read -r -a array <<< "$A"
          echo "number of data lines is now ${array[0]}"
          B=${array[0]}
          if [ $B -gt ${T60} ]; then
            echo "trimming file...to $T60 lines."
	    tail -n $T60 $FILE.csv > $FILE-trimmed.csv
            mv $FILE-trimmed.csv $FILE.csv
          else

# still not enough so try a bigger C
	    T60NEW=$(($T60*$(($C+4))))
	    echo "requesting even more...multipler is $C which is $T60NEW entries."
	    rm -f $FILE.csv
            echo "wget request for last...$T60NEW instances"
# second call to request last T60NEW data items
            wget --load-cookies cookies.txt -O $FILE  --tries=10 --read-timeout=5 --no-http-keep-alive --content-disposition http://${ROUTER}/fetch?type=LS\&start=${T60NEW}\&end=
            if [ $baxname -eq 1 ]; then
              if [ $focus -eq 1 ]; then
                $DESTAPP -file $FILE $FILE.csv -N $ENAME -f $TOPIC -i
              else
                $DESTAPP -file $FILE $FILE.csv -n $ENAME -i
              fi
            else
              if [ $focus -eq 1 ]; then
                $DESTAPP -file $FILE $FILE.csv -f $TOPIC -i
              else
                $DESTAPP -file $FILE $FILE.csv -i
              fi
            fi
            A=`wc $FILE.csv`
            IFS=' ' read -r -a array <<< "$A"
            echo "number of data lines is now ${array[0]}"
            B=${array[0]}
            if [ $B -gt ${T60} ]; then
              echo "trimming file...to $T60 lines."
	      tail -n $T60 $FILE.csv > $FILE-trimmed.csv
              mv $FILE-trimmed.csv $FILE.csv
            fi
	  fi
          echo "output of wc is now $A lines"
	fi
      fi
      echo "$FILE downloaded sucessfully."
      echo "and converted to $FILE.csv"
      if [ $havedestination -eq 1 ]; then
        mv $FILE.csv $DEST/$FILE.csv
        echo "and moved to $DEST/$FILE.csv"
      fi
    fi
  else
    if [ $havedestination -eq 1 ]; then
      mv $FILE $DEST/$FILE
      echo "and moved to $DEST/$FILE"
    else
      echo "data $FILE downloaded."
    fi
  fi
  exit
fi

# user used -file in command line. If havedestinationapp 1 then run it
if [ $isfile -eq 1 ]; then
  if [ -e $FILE ]; then
    echo "$FILE exists"
    echo "e passing $havedestinationapp $baxname $focus"
    if [ $havedestinationapp -eq 1 ]; then
      if [ $baxname -eq 1 ]; then
        if [ $focus -eq 1 ]; then
          if [ $havelast -eq 1 ]; then
            $DESTAPP -file $FILE $FILE.csv -N $ENAME -f $TOPIC -i
          else
            $DESTAPP -file $FILE $FILE.csv -N $ENAME -f $TOPIC
          fi
        else
          if [ $havelast -eq 1 ]; then
            $DESTAPP -file $FILE $FILE.csv -n $ENAME -i
          else
            $DESTAPP -file $FILE $FILE.csv -n $ENAME
          fi
        fi
      else
        if [ $focus -eq 1 ]; then
          if [ $havelast -eq 1 ]; then
            $DESTAPP -file $FILE $FILE.csv -f $TOPIC -i
          else
            $DESTAPP -file $FILE $FILE.csv -f $TOPIC
          fi
        else
          if [ $havelast -eq 1 ]; then
            $DESTAPP -file $FILE $FILE.csv -i
          else
            $DESTAPP -file $FILE $FILE.csv
          fi
        fi
       fi
      if [ -e $FILE.csv ]; then
        if [ $havedestination -eq 1 ]; then
          mv $FILE.csv $DEST/$FILE.csv
          echo "and moved to $DEST/$FILE.csv"
        else
          echo "converted to $FILE.csv"
        fi
      fi
    fi
  fi
  exit
fi

# If run not set exit.
if [ $run -eq 0 ]; then
  echo " "
  echo " Not enough information for repeating fetch"
  exit
fi

# to test repeating wget fetches
while
# minimal pause before invoking logic
  sleep 1; do
  FILE=$(date +%Y-%m-%d-%H-%M -d now)
  echo "$FILE"

  THR=3600

  TSTART=$(($(date +%s -d now)-$EPOCH))
# Adjust start to subtract T60
  TSTARTADJ=$(($TSTART-$T60))
#  echo $TSTART
#  echo $TSTARTADJ
  TEND=`expr $TSTARTADJ + $T60`
#  echo $TEND

# initial contact to setup session
  wget --quiet --save-cookies cookies.txt --delete-after --post-data 'user='${USER}'&pass='${PWD} --keep-session-cookies http://${ROUTER}/WWW/login.htm

# second call to request data
  wget --quiet --load-cookies cookies.txt -O $FILE --tries=10 --read-timeout=5 --no-http-keep-alive --content-disposition http://${ROUTER}/fetch?type=BT\&start=${TSTARTADJ}\&end=${TEND}

  echo "f passing $havedestinationapp $baxname $focus"
  if [ $havedestinationapp -eq 1 ]; then
    if [ $baxname -eq 1 ]; then
      if [ $focus -eq 1 ]; then
        $DESTAPP -file $FILE $FILE.csv -N $ENAME -f $TOPIC
      else
        $DESTAPP -file $FILE $FILE.csv -n $ENAME
      fi
    else
      if [ $focus -eq 1 ]; then
        $DESTAPP -file $FILE $FILE.csv -f $TOPIC
      else
        $DESTAPP -file $FILE $FILE.csv
      fi
    fi
    if [ -e $FILE.csv ]; then
      if [ $havedestination -eq 1 ]; then
        mv $FILE.csv $DEST/$FILE.csv
        echo "moved to $DEST/$FILE.csv"
      else
        echo "added $FILE.csv"
      fi
    fi
    rm $FILE
  else
    echo "added $FILE"
  fi

# adjust the number of seconds as required
  sleep $T60
done

