export PATH=/home/henthorn/bin:$PATH
echo $PATH
export LD_LIBRARY_PATH=/home/henthorn/lib:/usr/local/lib
echo $LD_LIBRARY_PATH

UP=0
DOWN=1
PINGINTERVAL=5
UPTIME=1200   # 20 minutes
DOWNTIME=720  # 12 minutes
LOGS=/home/henthorn/logs
SESLOGS=$LOGS/ses
LOCALLOGS=$LOGS/local

date
echo "Lets wait for up to $UPTIME seconds for the SES to hibernate..."
echo ""
ses=$UP
TOTAL=0
ndown=0
while [ $TOTAL -lt $UPTIME ]
do
  ping -c 1 $1
  ses=$?
  if [ $ses -eq $DOWN ]; then
    if [ $ndown -lt 2 ]; then
      ndown=$((ndown+1))
    else
      break
    fi
  else
    sleep $PINGINTERVAL
    ndown=0
  fi
  echo "SES still up..."
  TOTAL=$((TOTAL+PINGINTERVAL))
  echo "time waiting is now $TOTAL..."
done
date

if [ $ses -ne $DOWN ]; then
  echo "SES must not executing a mission. Done."
  exit 1
fi

echo "SES is hibernating, lets catch it in $DOWNTIME seconds when it wakes up..."
echo ""
ses=$DOWN
TOTAL=0
nup=0
while [ $TOTAL -lt $DOWNTIME ]
do
  ping -c 1 -W 5 $1
  ses=$?
  if [ $ses -eq $UP ]; then
    if [ $nup -lt 2 ]; then
      nup=$((nup+1))
    else
      break
    fi
  else
    nup=0
  fi
  date
  echo "SES still down..."
  #sleep $PINGINTERVAL
  TOTAL=$((TOTAL+PINGINTERVAL))
  echo "Time waiting is now $TOTAL..."
done
date

if [ $ses -ne $UP ]; then
  echo "SES is hibernating an extra long time. We'll try later."
  exit 1
fi

echo "SES is up, wait for 30 seconds to allow instrument to start up..."
sleep 90

echo "Mounting ses:/media/data/logs on $SESLOGS..."
success=1
sshfs root@134.89.42.124:/media/data/logs $SESLOGS
success=$?
if [ $success -ne 0 ]; then
  echo "Failed to mount ses:/media/data/logs on $SESLOGS..."
  exit 1
fi

date
DEP=`ls -tr $SESLOGS | grep ^dep- | tail -1`
cd $SESLOGS/$DEP
pwd
NB16a=`find . -name *.b16 | wc -l`
echo "Watching $DEP for new image files, currently there are $NB16a..."
# for i in {0..45}
TOTAL=0
while [ $TOTAL -lt 360 ]
do
  sleep 30
  TOTAL=$((TOTAL+30))
  echo "Time waiting is now $TOTAL..."
  NB16b=`find . -name *.b16 | wc -l`
  if [ $NB16b -gt $NB16a ]; then
    break
  fi
  echo "Still $NB16b images..."
done

if [ $NB16b -eq $NB16a ]; then
  echo "Perhaps the SES is not executing a measuring mission. We'll try later."
  cd ~
  pwd
  umount $SESLOGS
  exit 1
fi

if [ $NB16b -eq 0 ]; then
  echo "SSH transport issue?. We'll try later."
  cd ~
  pwd
  umount $SESLOGS
  exit 1
fi

date
sleep 2
cd $LOGS
pwd
echo "Copying latest dep data from ses $DEP to local drive..."
echo "cp -vu --preserve=timestamps $SESLOGS/*.db $LOCALLOGS/."
cp -vu --preserve=timestamps $SESLOGS/*.db $LOCALLOGS/.
cp -vu --preserve=timestamps $SESLOGS/voltage.csv $LOCALLOGS/.

if [ -d "$LOCALLOGS/$DEP" ]; then
  # avoid copying extremely large syslog.csv files by simply copying the plan folders
  echo "cp -rvu --preserve=timestamps $SESLOGS/$DEP/plan* $LOCALLOGS/$DEP/."
  cp -rvu --preserve=timestamps $SESLOGS/$DEP/plan* $LOCALLOGS/$DEP/.
  cp -rvu --preserve=timestamps $SESLOGS/$DEP/*.csv $LOCALLOGS/$DEP/.
else
  # just getting started go ahead and copy the entire tree
  echo "cp -rvu --preserve=timestamps $SESLOGS/$DEP $LOCALLOGS/."
  cp -rvu --preserve=timestamps $SESLOGS/$DEP $LOCALLOGS/.
fi

pwd
echo "Unmount the remote media..."
umount $SESLOGS

date
echo "Now bake the resulting deployment data..."
sleep 5
cd $LOCALLOGS
pwd
echo "bake.sh -s $LOCALLOGS/$DEP..."
which bake.sh
bake.sh -s $LOCALLOGS/$DEP
cd /mbari/ProjectLibrary
cd /mbari/ProjectLibrary/902305_Event_Detection_with_SES/Deployments/MARS-230508-01/latest-data
pwd
cp -vn $LOCALLOGS/$DEP/images/*.jpg  .
rm -v  fluorometer.csv
cp -v  $LOCALLOGS/$DEP/fluorometer.csv .
cd /mbari/ProjectLibrary/902305_Event_Detection_with_SES/Deployments/MARS-230508-01
pwd
echo "cp -rvu $LOCALLOGS/$DEP ."
cp -rvu $LOCALLOGS/$DEP .


echo "Success!"
date
exit 0
