#!/bin/bash
#
# tarballer.sh - script to build tarballs of the rocking curve image and log
#                files copied from CHESS into the named scan directories, and
#                archive to srm any that are not already stored there.
#
# author: richard.t.jones at uconn.edu
# version: may 28, 2014

scandir_list="\
J1a50_study1 \
J2a100_study1 \
JD70-2-study1 \
JD70-2_study2 \
JD70-3_study1 \
JD70-3_study2 \
SI45-S90_study1 \
"

pnfsbase=/pnfs/phys.uconn.edu/data/Gluex/beamline/diamonds/chess-2-2015
srmbase=srm://grinch.phys.uconn.edu/Gluex/beamline/diamonds/chess-2-2015

for scandir in $scandir_list; do
    if [[ -r $pnfsbase/$scandir.tgz ]]; then
        size_local=`ls -l $scandir.tgz | awk '{print $5}'`
        size_pnfs=`ls -l $pnfsbase/$scandir.tgz | awk '{print $5}'`
        if [[ $size_local = $size_pnfs ]]; then
            echo "$scandir.tgz already saved, skipping..."
            continue
        else
            echo "$scandir.tgz exists on pnfs but has a different size"
            echo "than the local copy. You must delete whichever copy"
            echo "is faulty and then try again."
            exit 9
        fi
    elif [[ ! -r $scandir.tgz ]]; then
        echo "$scandir.tgz not found, creating..."
        tar zcf $scandir.tgz $scandir || exit $?
    fi
    echo "pushing $scandir.tgz to srm..."
    srmcp file:///`pwd`/$scandir.tgz $srmbase/$scandir.tgz || exit $?
done
