#!/bin/bash

HOST_TOOLS="$( cd "$( dirname "$0" )" && pwd )"

NAMES=$(sed -rn 's/^\s*Host\s+(.*)\s*/\1/ip' ~/.ssh/config)

#NAMES="tethys lrauv-tethys tethys-cell \
#daphne lrauv-daphne daphne-cell \
#makai lrauv-makai makai-cell \
#ahi lrauv-ahi ahi-cell \
#aku lrauv-aku aku-cell \
#opah lrauv-opah opah-cell \
#whoidhs lrauv-whoidhs whoidhs-cell \
#galene lrauv-galene galene-cell \
#triton lrauv-triton triton-cell"

if [ $# -ne 1 ]; then
  echo "sync-remove-data.sh requires a vehicle name as an argument";
  vehicle="_UNDEFINED_";
else
  vehicle=$1
fi

if echo "$NAMES" | grep -q "$vehicle"; then
  echo "syncing and removing data from $vehicle";
else
  echo "vehicle name $vehicle invalid";
  echo "valid vehicle names:";
  echo $NAMES;
  exit 0;
fi

# one last rsync
RSYNC=/usr/bin/rsync
RSYNC_OPT="-rvPhv --times --exclude 'latest' --exclude 'previous'" # no timeout 
SRC=/mnt/mmc/LRAUV/Logs
DST=/mbari/LRAUV/$vehicle/missionlogs/tmp # should be same path as in pull-unpack-lrauv-logs
echo "switching to user tethysadmin for rsync"
su tethysadmin -c "$RSYNC $RSYNC_OPT $vehicle:$SRC $DST"

# confirm deletion in Logs
echo "delete LRAUV/Logs/* on $vehicle? [yN]" && read response
if test "$response" = "Y" -o "$response" = "y"; then
  cmd="rm -rf /mnt/mmc/LRAUV/Logs/*"
  echo "executing $cmd on $vehicle"
  ssh $vehicle "$cmd" # wait for completion
else
  echo "received $response -- not Y, canceling."
fi
# confirm deletion in Data
echo "delete LRAUV/Data/persisted.cfg on $vehicle? (LRAUV/Data/navchart/ and LRAUV/Data/HFRadarModel will be left intact) [yN]" && read response
if test "$response" = "Y" -o "$response" = "y"; then
  cmd="rm -rf /mnt/mmc/LRAUV/Data/persisted.cfg"
  echo "executing $cmd on $vehicle"
  ssh $vehicle "$cmd" # wait for completion
else
  echo "received $response -- not Y, canceling."
fi
