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

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

RSYNC=/usr/bin/rsync 
RSYNC_OPT="-rvh --times --exclude=Config/ --exclude=*.parts --include=*/ --include=Priority* --include=Normal* --include=ESP* --exclude=* --timeout 10"

SRC=/mnt/mmc/LRAUV/Logs/*
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 ahi aku opah whoidhs galene brizo pontus triton
do
  lck=$LOCKDIR/handle-lrauv-cell-logs.$vehicle.lck
  dst=$DST/$vehicle/realtime/cell-logs # leave a flat directory structure for now -- don't want to waste time trying to rsync cell logs from 201403 when we really want logs from 201404
  log=$LOGDIR/pull-lrauv-cell-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
