#!/bin/bash

# script to transfer local files at Cascina
# script to move files in the final folder once transfered at Cascina
# mabizoua@lal.in2p3.fr

webserver=cascina

if [[ $# != 1 ]]; then
    echo "usage: `basename $0` [gps] [outdir]" 1>&2
    echo "       [action] : transfer or move"
    exit 1
fi

action=$1
shift

if [ $action == 'move' ]; then
    if [ -d /virgoDev ]; then
        echo 'titi'
        eval `/users/mabizoua/home/ligotools/bin/use_ligotools`
    fi
    if [ -d /home/mat ]; then
        eval `/usr/local/ligotools/bin/use_ligotools`
    fi
    if [ -d /afs ]; then
        eval `/afs/in2p3.fr/throng/virgo/pro/ligotools/bin/use_ligotools`
    fi
    
    
    for gps in [8-9]*; do
# Translate it into year and month
        Year=`tconvert -f "%Y" ${gps}`
        Month=`tconvert -f "%b" ${gps}`
        FullMonth=`tconvert -f "%B" ${gps}`
        Monthnb=`tconvert -f "%m" ${gps}`
        Daynb=`tconvert -f "%d" ${gps}`
        Hour=`tconvert -f "%H" ${gps}`
        Minute=`tconvert -f "%M" ${gps}`
        Second=`tconvert -f "%S" ${gps}`
        now=`tconvert now`
        currentdate=`tconvert -f "%D %T" ${now}`

        echo $Year $Month
        outdir=$Year/$Month
        if [ ! -d $outdir ]; then
            mkdir -p $outdir
            echo        mv $gps $outdir/.
        else
            echo        mv $gps $outdir/.
        fi
    done
else
    echo 'transfer files'
    if [ $webserver = "cascina" ]; then
        remoteoutputdir=mabizoua@ctrl12.virgo.infn.it:/opt/w3/DataAnalysis/WOD/
    else
        if [ $webserver = "carleton" ]; then
            remoteoutputdir=mat@virgo.physics.carleton.edu:/home/mat/mab/wscan/
        else
            remoteoutputdir=""
        fi
    fi

    for gps in [8-9]*; do
        scp -i ~/.ssh/id_rsa -r ${gps} ${remoteoutputdir}
    done

fi

