#!/bin/sh

# where you want to put the data
destdir=/net/data/4/Daq/pro2003/
tmpdir=/net/data/4/Daq/pro2003/tmp/

# where the data is
tgtdir=/scratch/dblast07/blast/data/pro2003/

# where the checksum will go - but only at the end !!
checksumdir=${destdir}/checksums


cd ${destdir}

for file in ${tgtdir}/*.dat ; do 
shfname=`echo ${file} | sed -e 's/\///g ' | sed -e 's/scratchdblast07blastdatapro2003//'  `;

if [ ! -f ${shfname} ]; then {

    #@ /bin/echo ${shfname} "does not exist - it will be copied over"
    # make file immediately available with a link
    /bin/ln -fs ${file} . 

    # define checksum fnames  - will check if copying is complete
    checkname=`echo ${shfname} | sed -e 's/dat/chcksum_spud4/ ' `;
    dbcheckname=`echo ${shfname} | sed -e 's/dat/chcksum_dblast07/ ' `;

    /usr/bin/md5sum  ${tgtdir}/${shfname}  | gawk '{print $1}' > ${tmpdir}/${dbcheckname}
    #/bin/cat  ${tmpdir}/${dbcheckname}


      until ( /usr/bin/diff ${tmpdir}/${checkname} ${tmpdir}/${checkname} ) ; do

	  # Checksums not equal - continue copying original data file to tmp dir
	  #@ /bin/echo "start copy"
	  /bin/cp -p ${file} ${tmpdir}/${shfname}
	  #@ /bin/echo "done copy"

	  #generate checksum of copied file
	  /usr/bin/md5sum  ${tmpdir}/${shfname} | gawk '{print $1}'  > ${tmpdir}/${checkname}
	  #@ /bin/cat  ${tmpdir}/${checkname}

	  sleep 180
	  # update checksum of original file - sleep so that it will really update..
	  # it takes less than 1 minute to update a coda file (even if in prestart mode)
	  /usr/bin/md5sum  ${tgtdir}/${shfname}  | gawk '{print $1}' > ${tmpdir}/${dbcheckname}
	  #@ /bin/cat  ${tmpdir}/${dbcheckname}

       done;


   #@ /bin/echo "yes I am done. Moving"
   # move file from tmpdir to destdir overwriting link
   /bin/mv -f ${tmpdir}/${shfname} .
   /bin/mv -f ${tmpdir}/${dbcheckname}  ${tmpdir}/${checkname} ${checksumdir}

}
fi;
done