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

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

RSYNC=/usr/bin/rsync 
RSYNC_OPT="-rzvh --times --timeout 30"

SRC=/mnt/mmc/LRAUV/ESPlogs/*
DST=/mbari/LRAUV
#DEBUG#DST=/tmp
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 brizo triton
do
  lck=$LOCKDIR/handle-lrauv-esp-logs.$vehicle.lck
  dst=$DST/$vehicle/realtime/ESPlogs # leave a flat directory structure for now
  log=$LOGDIR/pull-lrauv-esp-logs.$vehicle.log
  mkdir -p $dst # make the directory if it isn't already there
  rcmd="$RSYNC $RSYNC_OPT $vehicle-cell:$SRC $dst &> $log" # overwrite, don't append
  echo "*** [lock free] running: $rcmd under flock with lock $lck"
  $FLOCK --nonblock --timeout 0 --exclusive $lck -c "$rcmd"
done
