#!/bin/sh

# Validate arguments
if [ "$#" -lt 2 ]; then
  echo "Usage: $0 SOURCE_DIR DEST_DIR" >&2
  /fs/etfs/config/current/CustomerExtensions/SendEvent "Snapshot.MoveError"
  exit 1
fi
if ! [ -e "$1" ]; then
  echo "$1 not found" >&2
  /fs/etfs/config/current/CustomerExtensions/SendEvent "Snapshot.MoveError"
  exit 1
fi
if ! [ -e "$2" ]; then
  echo "$2 not found" >&2
  /fs/etfs/config/current/CustomerExtensions/SendEvent "Snapshot.MoveError"
  exit 1
fi
if ! [ -d "$1" ]; then
  echo "$1 not a directory" >&2
  /fs/etfs/config/current/CustomerExtensions/SendEvent "Snapshot.MoveError"
  exit 1
fi
if ! [ -d "$2" ]; then
  echo "$2 not a directory" >&2
  /fs/etfs/config/current/CustomerExtensions/SendEvent "Snapshot.MoveError"
  exit 1
fi

# Remove done file
rm -rf $2/done

# Move snapshot folders to destination directory
cp -rcv "$1/snapshot"* "$2"

# Delete source files
rm -rf "$1/snapshot"*

# Sync and indicate done with a file and a PV event (if it exists)
sync
touch $2/done
/fs/etfs/config/current/CustomerExtensions/SendEvent "Snapshot.MoveSuccess"
echo "Snapshot files moved to $2"
