#!/bin/bash
#
# archive.sh - copy any files listed in the argument list to the appropriate
#              directory in dcache, and remove them from local storage.
#
# author: richard.t.jones at uconn.edu
# version: may 23, 2019

function usage() {
	echo "archive.sh [options] <file1> [<file2> ...]"
	echo " where options include:"
	echo "  -r : remove the files as soon as they are successfully copied"
}

delete_after_copy=no
while [[ $# -gt 0 ]]; do
    if [[ "$1" = "-r" ]]; then
		delete_after_copy=yes
		shift
		continue
	elif [[ ! -r $1 ]]; then
		usage
		exit 1
	fi
	run=`echo $1 | awk -F'[_.]' '{print $3}'`
	uberftp nod29.phys.uconn.edu "cd /Gluex/rawdata/PSskims/Run0$run; put $1"
	if [[ $? = 0 && $delete_after_copy = yes ]]; then
		/bin/rm $1
	fi
	shift
done
