#!/bin/bash
####################################################################
#                  MDISTRIBUTE                                     #
# This shell copies the files found and copied by mfind to the     #
# mctdh-directory.                                                 #
# HDM 05/00                                                        #
####################################################################


minus40='----------------------------------------'
day=`date '+%d' `

purp="Purpose: Copy the files found by mfind to the MCTDH directory."
usage="Usage: mdistribute [ -c -n -h ] dir1 [ dir2 ] "
help1='-h : print this help text.'
help2='-c : Copy. Without -c just display the files to be copied.'
help3="-n : No save. Do not save files to be updated to: file_md$day ."
help4="            (The number $day is todays date.)"
help7=' Copying from dir1 to dir2.'
help8=' dir1 and dir2 should be structured like MCTDH-directories.'
help9=' If dir2 is not given, $MCTDH_DIR is assumed.'


#-----------------------------------------------------------------------
#  Read the options (if any).
#-----------------------------------------------------------------------

while getopts ":hcn" opt; do
    case $opt in
    h  ) echo $minus40$minus40
         echo $purp
         echo $usage
         echo ' '$help1
         echo ' '$help2
         echo ' '$help3
         echo "  $help4"
         echo ' '
         echo ' '$help7
         echo ' '$help8
         echo ' '$help9
	 echo ' '
	 exit ;;
    c  ) copy=1 ;;
    n  ) nosave=1 ;;
    \? ) echo ' Unkown option!'
         echo ' -h provides a help text.'
	 echo $usage
	 exit 1 ;;
  esac
done

shift $(($OPTIND - 1))

if [ -z "$dir1" ] ; then dir1="${1:--h}" ; fi
dir2="${2:-$MCTDH_DIR}"

if [ "$dir1" = "-h" ] ; then
    echo $purp
    echo $usage
    echo ' '$help1
    echo ' '$help2
    echo ' '$help3
    echo "  $help4"
    echo ' '
    echo ' '$help7
    echo ' '$help8
    echo ' '$help9
    echo ' '
    exit
fi

if [ ! -d $dir1 ] ; then
   echo "$dir1 does not exist!"
   exit
fi
if [ ! -d $dir2 ] ; then
   echo "$dir2 does not exist!"
   exit
fi

if [ -n "${dir2##/*}" ] ; then
    present=`pwd`
    cd $dir2
    dir2=`pwd`
    cd $present
fi

cd $dir1
dir1=`pwd`

echo "$minus40$minus40"
echo ' Copying from Directory: '$dir1
echo ' Copying  to  Directory: '$dir2
echo ' '
count=0
dd2=0

filelist=`find .  -type f  -name '*[!~]' -print | egrep -v  \
'(\./object/.*|\./run.*/.*|\./test.*/.*|\./tutorial/.*|\./bin/.+/.*|.*/paths\.inc|.*\.ps|.*\.dvi|.*\.aux|.*/i686/.*|.*/alpha/.*|\./source/tags|\./source/TAGS|\./source/TYPE_DAT.*|\./source/SFILES|\./source/SUBR|.*\.tgz|\./install/compile.cnf$|\./install/platform.cnf$|\./install/platform\.cnf\.csh$|\./doc/.*\.log|\./doc/guide/guide\.i.*|\./doc/guide/guide\.l.*|\./doc/guide/guide\.toc|\./CompDir*|\./.*mctdhall.*|.*\.[0-9][0-9]?$|.*_bk[0-9][0-9]?$|.*_md[0-9][0-9]?$)' | sort `


for i in  $filelist ; do
    count=$(($count+1))
    f1=${i#./}
    f2=$dir2/$f1
    d2=`dirname $f2`
    if [ $d2 != $dd2 ] ; then
	echo ' '
	if [ ! -d $d2 ] ; then
	    if [ -n "$copy" ] ; then
		echo "I am creating the directory $d2 ."
		mkdir -p $d2
	    else	
		echo "The directory $d2 does not exist."
	    fi
	fi
    fi
    echo $f1
    if [ -n "$copy" ] ; then
       if [ -z "$nosave" ] ; then
           mv -f $f2 ${f2}_md$day 2>/dev/null
       fi
       cp -f $f1 $f2
    fi
    dd2=$d2
done


echo ' '
echo "There are $count files."
echo ' '
if [ -z "$copy" ] ; then
    echo 'Just listing the files. No copying performed.'
    echo 'Use the option -c to actually copy.'
else
    echo 'Files copied!'
    if [ -z "$nosave" ] ; then
	echo "Updated files backuped to: file_md$day"
    else
        echo "Updated files overwritten. No backup."
    fi
fi
echo "$minus40$minus40"
exit












