#!/bin/bash
####################################################################
#                  vsdiff                                          #
# This shell script compares two MCTDH_directories and lists the   #
# source files that differ.  vsdiff calls ddiff                    #
# HDM 04/00                                                        #
####################################################################


#-----------------------------------------------------------------------
#  Definitions :
#-----------------------------------------------------------------------
#set -x
mopt='-d'
purp="Purpose: Comparison of the fortran source code two MCTDH-directories."
usage="Usage  : vsdiff [ -h -d -i -s -l ] dir1 [task | [dir2 [task]]."
help1='-h : print this help text.'
help2='-d : indicate only files that differ (default).'
help3='-i : indicate only files that are identical.'
help4='-s : indicate whether files are identical or different.'
help5='-l : long form. Let diff show the differences.'
hlp5a='-L : similar to -l, but case and blank sensitive.'
help6='-n : No comparison of library routines.'
help7='     The comparison is between dir1 and dir2.'
help8='     If dir2 is not given, $MCTDH_DIR is assumed.'
help9='     If task is given, compare only files of task (i.e. mctdh, gendvr, etc).'

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

while getopts ":hdislLn" opt; do
    case $opt in
    h  ) dir1='-h' ;;
    d  ) mopt='-d' ;;
    i  ) mopt='-i' ;;
    s  ) mopt='-s' ;;
    l  ) mopt=''   ;;
    L  ) mopt='-c' ;;
    n  ) no=1      ;;
    \? ) echo ' Unkown option!'
         echo $usage
         echo ' -h provides a help text.'
         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 ' '$help5
    echo ' '$hlp5a
    echo ' '$help6
    echo ' '
    echo ' '$help7
    echo ' '$help8
    echo ' '$help9
    echo ' '
    exit
fi

if [ -z "$3" ] ; then
    for i in  mctdh gendvr geninwf genoper opfuncs propwf propdo \
              potfit fd_cmplx fd_real analyse include \
	      lib/linear lib/ode lib/eigen lib/utilities \
              lib/LAPACK lib/BLAS
    do
	if [ "$dir2" = "$i" ] ; then
	task=$dir2
	dir2=$MCTDH_DIR
	fi
    done
else
    task=$3
fi

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

if [ ! -d $dir2/source ] ; then
    echo "$dir2 is not an MCTDH directory!"
    exit
fi

cd $dir1
dir1=`pwd`

echo '-----------------------------------------------------------------'
echo ' Working    Directory: '$dir1
echo ' Comparison Directory: '$dir2
echo ' '

cd $dir1/source

#-----------------------------------------------------------------------
#  Compare the files of directory task.
#-----------------------------------------------------------------------

if [ -n "$task" ] ; then
    if [ -d $task ] ; then
	cd $task
	if [ "$task" = include ] ; then
	    ddiff $mopt -f $dir2/source/$task *.inc
	else
	    ddiff $mopt -f $dir2/source/$task *.[Ff]
	fi
    else
	echo "No directory $dir1/source/$task"
    fi
    exit
fi

#-----------------------------------------------------------------------
#  Compare the files of all directories.
#-----------------------------------------------------------------------

for i in  mctdh gendvr geninwf genoper propwf propdo \
          opfuncs surfaces potfit analyse fd_cmplx fd_real
do
    if [ -d $i ] ; then
	cd $i
	ddiff $mopt -f $dir2/source/$i *.[Ff]
	cd ..
    else
	echo "No directory $dir1/source/$i"
    fi
done
      if [ -d include ] ; then
	cd include
	ddiff $mopt -f $dir2/source/include \
          `ls *.inc | egrep -v '(cdate|paths|versions)'`
      else
	echo "No directory $dir1/source/include"
      fi

if [ -z "$no" ] ; then
   if [ -d $dir1/source/lib ] ; then
	cd $dir1/source/lib
	for i in BLAS LAPACK eigen linear utilities ode ode/odesim
	do
	    if [ -d $i ] ; then
		cd $i
		ddiff $mopt -f $dir2/source/lib/$i *.[Ffc]
		cd ..
	    else
		echo "No directory $dir1/source/lib/$i"
	    fi
	done
   else
	echo "No directory $dir1/source/lib"
   fi	
fi

echo '########################################################################'
exit














