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

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

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

HOTSPOTREPOSITORY=odss.mbari.org
SRC=/opt/hotspot/incoming
DST=/mbari/LRAUV
#DEBUG#DST=/tmp

# NOTE: If the atlas mount was on beach.mbari.org at /mbari/LRAUV and writable 
# by tethysadmin, the rsync could be performed locally (and probably faster).

# This script _assumes_ that public key authentication has been configured with
# no password for the hotspot repository, and that ~/.ssh/config has a host 
# entry for the hotspot repository specifying the correct user and identity 
# file. That host entry may also provide a host shortcut, e.g., `ssh beach` may 
# log in to tethysadmin@beach.shore.mbari.org

for vehicle in tethys daphne makai ahi aku opah whoidhs galene brizo pontus triton
do
  lck=$LOCKDIR/handle-lrauv-hotspot-logs.$vehicle.lck
  dst=$DST/$vehicle/realtime/hotspotlogs # leave a flat directory structure for now -- don't want to waste time trying to rsync hotspot logs from 201403 when we really want logs from 201404
  log=$LOGDIR/pull-lrauv-hotspot-logs.$vehicle.log
  mkdir -p $dst # make the directory if it isn't already there
  rcmd="$RSYNC $RSYNC_OPT tethysadmin@$HOTSPOTREPOSITORY:$SRC/$vehicle/* $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
