#!/bin/sh
# pull-lrauv-logs.sh

FLOCK=/usr/bin/flock
LOCKDIR=/tmp
LOGDIR=/tmp

RSYNC=/usr/bin/rsync 
RSYNC_OPT="-rtvvh --exclude 'latest' --exclude 'previous' --timeout 600"

SRC=/mnt/mmc/LRAUV/Logs
LCMSRC=/home/user/lcm-logs
DST=/mbari/LRAUV
LRAUV=/home/tethysadmin/workspace/LRAUV

# This script _assumes_ that public key authentication has been configured with
# no password for each vehicle, and that ~/.ssh/config has host entries for 
# each vehicle specifying the correct user and identity file. Those host 
# entries must also provide a host shortcut, e.g., `ssh tethys` must log in to 
# root@lrauv-tethys.shore.mbari.org

for vehicle in tethys daphne makai ahi aku opah whoidhs galene brizo pontus triton
do
  lck=$LOCKDIR/handle-lrauv-logs.$vehicle.lck
  dst=$DST/$vehicle/missionlogs/tmp
  log=$LOGDIR/pull-lrauv-logs.$vehicle.log
  mkdir -p $dst # make the directory if it isn't already there
  rcmd="echo 'lock free' && $RSYNC $RSYNC_OPT $vehicle:$SRC $dst &>> $log"
  date -u --iso-8601=second &> $log # overwrite, don't append
  echo "*** running $rcmd under flock with lock $lck"
  $FLOCK --nonblock --timeout 0 --exclusive $lck -c "$rcmd"
done

for vehicle in tethys daphne makai ahi aku opah whoidhs galene brizo pontus triton
do
  lck=$LOCKDIR/handle-lrauv-logs.$vehicle.lck
  dst=$DST/$vehicle/missionlogs/tmp
  log=$LOGDIR/pull-lrauv-logs.$vehicle.log
  mkdir -p $dst # make the directory if it isn't already there
  rcmd="echo 'lock free' && $RSYNC $RSYNC_OPT $vehicle:$LCMSRC $dst &>> $log"
  #SKIP LCM PULL FOR NOW# echo "*** running $rcmd under flock with lock $lck"
  #SKIP LCM PULL FOR NOW# $FLOCK --nonblock --timeout 0 --exclusive $lck -c "$rcmd"
done


