#!/bin/bash
####################################################################
#                  mkpatch                                         #
# This shell script compares two MCTDH_directories - old, new -    #
# and copies the files that differ to a third directory            #
# HDM 01/01                                                        #
####################################################################
# set locale, so that date etc will be in uniform format.
export LC_ALL=C

if [ -z "$1" ] || [ "$1" = "-h" ] || [ "$2" = "-h" ] ; then
   echo "---------------------------------------------------------------------"
   echo "Purpose: Generation of a Mpatch"
   echo "Usage  : mkMpatch [ -h ] old new"
   echo " -p" use the full directory name, rather than the version-numbers.
   echo " -h" print this help text.
   echo " "
   echo "  'old' and 'new' are version-numbers, e.g 82.1 "
   echo "  Takes the differences between mctdhold and mctdhnew and "
   echo "  produces the tared and g-zipped directory Mpatch-old-new.tgz"
   echo "  Example: mkMpatch 82.0 82.1"
   echo "  Example: mkMpatch -p mctdh84.1 /worka/dieter/mctdh_new"
   echo "---------------------------------------------------------------------"
   exit
fi
pwd=`pwd`

if [ "$1" = "-p" ] ; then
   dir1=$pwd/$2
   dir2=$pwd/$3
   if [ -z "${2##/*}" ] ; then dir1=$2 ; fi
   if [ -z "${3##/*}" ] ; then dir2=$3 ; fi
   dir4=Mpatch-$2-$3
   dir4=`echo $dir4 | tr '/' '.'`
   dir3=$pwd/$dir4
else
   dir1=$pwd/mctdh$1
   dir2=$pwd/mctdh$2
   dir3=$pwd/Mpatch-$1-$2
   dir4=Mpatch-$1-$2
fi

if [ ! -d $dir1 ] ; then
   echo "$dir1 does not exist!"
   exit
fi
if [ ! -d $dir2 ] ; then
   echo "$dir2 does not exist!"
   echo "taking $MCTDH_DIR"
   dir2=$MCTDH_DIR
fi
if [  -d $dir3 ] ; then
   echo "$dir3 already exist! STOP!"
   exit
fi

count=0
cd $dir2
filelist=`find .  -type f  -name '*[!~]' -print | egrep -v  \
'(\.pyc|\./addsu.*\.log|\./addsu.*\.prj|\./object/.*|\./run.*/.*|\./test.*/.*|\./tutorial/.*|\./bin/binary/.*|.*/paths\.inc|.*\.ps|.*\.dvi|.*\.aux|.*/\.svn/.*|\./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|/obsolete/.*|\./CompDir.*|\./install/MCTDH_client|\./.*mctdhall.*|.*\.[0-9][0-9]?$|.*_bk[0-9][0-9]?$|.*_md[0-9][0-9]?$)' | sort `

nfiles=`echo $filelist | wc -w`
echo "Total number of files:   $nfiles"
date > $pwd/difflist

for i in  $filelist ; do
    f1=$dir1/${i#./}
    f2=$dir2/${i#./}
    f3=$dir3/${i#./}
    d3=`dirname $f3`
    cmp  -s  $f1 $f2
    st=$?
    if [ $st -ne 0 ] ; then
	echo "$st $f2" >> $pwd/difflist
 	count=$(($count+1))
	if [ ! -d $d3 ] ; then mkdir -p $d3 ; fi
	cp -f $f2 $f3
    fi
done
echo "Total number of files:   $nfiles"
echo "Number of files that differ: $count"

cd $pwd
tar -czf ${dir4}.tgz $dir4
echo "gzip tar file generated: ${dir3}.tgz"

exit













