#!/bin/bash
function kopyala(){
	
	dizi=1
	
	copy
	if [ "$Sz" != "$Size" -a "$dizi" -eq "$deneme_sayisi" ]
        then
        echo $file >> $kopyalanmayan_dosyalar
	else 
	
		number=`mdcli sequence_next $veritabani$number_name`
		mdcli addentry $veritabani$number " Name $file Path $Pth Format $Frmt Size $Sz Country $Cntry Organization $Orgnztn Station $Sttn Sensor $Snsr StartDateTime ' $i_time $i_time2 ' EndDateTime ' $e_time $e_time2 ' "
		
		echo "$file" >> $kopyalanan_dosyalar
    fi
	echo ""
	echo "----------------------------------------------------------------------------------------------------------------------"
	echo ""	
		
}
function copy () {
	echo "Try: " "$dizi"
    lcg-cr -v -d $Storage_element -l lfn:$Pth/$file --vo seegrid file://$bulundugu$file
    Size=`lfc-ls -l $Pth/$file | awk '{ print $5 }'`
        
    if [ "$Sz" != "$Size" -a "$dizi" -lt "$deneme_sayisi" ]
        then
        dizi=$(($dizi+1))
        copy
    fi
}
function dizin_yap(){
	lfc-ls $Pth > $var_mi
	sonuc_var_mi=`ls -la ./$var_mi | awk '{ print $5 }'`
	
	if [ $sonuc_var_mi -eq 0 ]
		then
		lfc-mkdir $Pth
	fi
	
}
if [ -z $2  ]
	then
	echo "USAGE:" $(basename $0)" <dir>  <Storage Element>  " 
	echo "EXAMPLE:" $(basename $0) "/home/lufer/waveformdata_directory/ se.ulakbim.gov.tr "
else
echo ""
echo "Copied files from " $1 " to " $2 
echo ""
export LFC_HOST=lfc.ulakbim.gov.tr
veritabani="/grid/seismo/test/waveformdata/"
ulke_veritabani="/grid/seismo/CountryCode/"
bulundugu=$1
number_name="fileid"
Storage_element=$2
TMP_FILE=`mktemp ./SCRIPT.XXXX`
var_mi=`mktemp ./SCRIPT.XXXX`
kopyalanmayan_dosyalar=`mktemp ./SCRIPT.XXXX`
kopyalanan_dosyalar=`mktemp ./SCRIPT.XXXX`
deneme_sayisi=20
for line in `ls -1 $bulundugu`
	do
	echo $line >> $TMP_FILE
done
cat $TMP_FILE | while read file
    do
	echo "$file"
	echo ""
	hedef="/grid/seismo.see-grid-sci.eu/data/"
	
    i_time=`echo $file | cut -d'-' -f1 | cut -c1-8`
    e_time=`echo $file | cut -d'-' -f2 | cut -c1-8`
	i_time2=`echo $file | cut -d'-' -f1 | cut -d'T' -f2`
	e_time2=`echo $file | cut -d'-' -f2 | cut -d'T' -f2`
	
	if [ $i_time = $i_time2 ]
		then
		echo $i_time
		echo $i_time2
		i_time2=000000
		echo $i_time2
	fi
	
	if [ $e_time = $e_time2 ]
		then
		echo $e_time2
		echo $e_time2
		e_time2=000000
		echo $e_time2
	fi
    Cntry=`echo $file | cut -d'-' -f3`
    Orgnztn=`echo $file | cut -d'-' -f4`
    Sttn=`echo $file | cut -d'-' -f5`
    Snsr=`echo $file | cut -d'-' -f6 | cut -d'.' -f1 `
    Frmt=`echo $file | cut -d'.' -f2`
	Sz=`ls -l $bulundugu$file | awk '{ print $5 }'`	
	if [ -z $Orgnztn ]
		then
		Orgnztn="-"
	fi
	
	sorgu='"'"$Cntry"'"'
	sorgu2='"'"$Orgnztn"'"'
		
	sonuc=`mdcli "selectattr $ulke_veritabani:Path 'A2= $sorgu or A3= $sorgu and Organization= $sorgu2  ' "`
	hedef=$hedef$sonuc
	
		
    if [[ "$i_time" =~ "([0-9]{4})([0-9]{2})([0-9]{2})" ]]
        then
        i_year="${BASH_REMATCH[1]}"
        i_month="${BASH_REMATCH[2]}"
        i_day="${BASH_REMATCH[3]}"
		e_year=`echo $e_time | cut -c1-4`
		e_month=`echo $e_time | cut -c5-6`
		e_day=`echo $e_time | cut -c7-8`
        
		Pth="$hedef$i_year"
		
        if [ ! -d $hedef$i_year ]
			then			
        	dizin_yap
        fi
   
		if [ $i_month = $e_month ]
			then			
			Pth="$hedef$i_year/$i_month"
			if [ ! -d $hedef$i_year/$i_month ]
				then
				dizin_yap
			fi
			
			if [ $e_time = $i_time ]
				then
				Pth="$hedef$i_year/$i_month/$i_day"
				if [ ! -d $hedef$i_year/$i_month/$i_day ]
                	then
                	dizin_yap
				fi				
                kopyala
            else
                kopyala
            fi			
		
		else
			kopyala
		fi
	fi
done
echo "These files was coppied:"
echo ""
cat $kopyalanan_dosyalar
echo ""
echo "--------------------------------------------------"
echo ""
echo "These files can not be coppied:"
cat $kopyalanmayan_dosyalar
rm -rf ./SCRIPT*
fi

