#!/bin/bash
# Author: A. Forti
# Creation date: 15/10/2015

usage(){

cat <<EOF

Usage: $0 <host:fs> <regexp> <space_token>

host:fs (mandatory) = data servers and file system to copy the files from.
regexp_string (mandatory) = to select a subset of data. For example atlas/2013 all atlas 2013 data or 2012-|2013- all 2012 and 2013 data. Mandatory.
                If you want all data use dpm-drain. 
space_token_desk (optional) = Replicate to space tokens. Works only for ATLAS on the assumption space token and directory name are the same. If you want to move data from another VO that doesn't use space tokens don't use this parameter. 

EOF

exit

}

if [ ! $(echo $1 | grep ":/") ] ; then
    echo "Malformed host:fs $1"
    usage
    exit
fi

host=( $(echo $1 | cut -f1 -d':') ) 
fs=( $(echo $1 | cut -f2 -d':') ) 
regexp_string=$2

space_token_desc=$3

if [ -z "$regexp_string" ];then
    echo "Missing regexp string"
    usage
    exit

fi

host_short=$(echo $host|cut -f1 -d'.')
timestamp=$(date +%Y%m%d%H%M%S)
disk_list="/tmp/${host_short}-disklist-${timestamp}"
disk_list_dpns="${disk_list}_dpns"
disk_list_mon="${disk_list}_mon"

echo "You can follow the progress tail -f $disk_list_mon"
date >> $disk_list_mon

if [ -z $space_token_desc ] ; then 

    echo "Dumping the list of files selected for the machine $host_short with string $regexp_string" >> $disk_list_mon
    dpm-list-disk -s $host -f $fs | awk '{print $2}' | egrep "$regexp_string" > $disk_list
    echo "Number of files $(wc -l $disk_list) with $regexp_string"

    if [ -s $disk_list ] ; then 
	
	c=0 
	for a in $(cat $disk_list); do 
	    
	    c=$(($c+1))
	    echo "$c: $(date +%Y%m%d%H%M%S) $a"

	    error=$(dpm-replicate $a 2>&1)

	    if [ "$?" -ne "0" ] || [ -n "$error" ] ; then 
		echo "Error $? $error"
	    else
		dpm-delreplica $a
	    fi	
	    
	done     
	echo "$(date): End of copying $(wc -l $disk_list) files."
    else 
	
	echo "Nothing selected with these filters"
    fi

else

    echo "Dumping the list of files selected for the machine $host_short with string $regexp_string and $space_token_desc" >> $disk_list_mon
    dpm-sql-spacetoken-list-files --st="$space_token_desc" | egrep $host:$fs | egrep "$regexp_string" > $disk_list
    echo "Number of files $(wc -l $disk_list) with $regexp_string and $space_token_desc"

    if [ -s $disk_list ] ; then 
    
	c=0 
	space_token=$(dpm-listspaces | grep -i $space_token_desc| grep "ID="|cut -f2 -d'=')

	for a in $(cat $disk_list); do 
	    
	    c=$(($c+1))
	    echo "$c: $(date +%Y%m%d%H%M%S) $a"
	    
	    error=$(dpm-replicate --space_token $space_token $a 2>&1)

	    if [ "$?" -ne "0" ] || [ -n "$error" ] ; then 
		echo "Error $? $error $a"
	    else
		dpm-delreplica $a
	    fi	
	    
	done     
	echo "$(date): End of copying $(wc -l $disk_list) files."
    else 
	
	echo "Nothing selected with these filters"
    fi
    
fi  >> $disk_list_mon 2>&1

