#!/bin/csh -f

###################################################################
#                ddiff   (directory diff)                         #
# Tool for a convenient comparison of files of two directories.   #
# The directory to be compared with is set in DIFFNAME.           #
# H.-D. Meyer, March 1998                                         #
###################################################################

set pwd=`pwd`
set bpwd=$pwd:t
set pwd1=$pwd:h
set bpwd1=$pwd1:t
set pwd2=$pwd1:h
set bpwd2=$pwd2:t
set pwd3=$pwd2:h
set bpwd3=$pwd3:t
set pwd4=$pwd3:h
set bpwd4=$pwd4:t
if ( "$bpwd" == 'source' | "$bpwd" == 'doc' ) then
   set DIFFNAME = $MCTDH_DIR/$bpwd
else if ( "$bpwd1" == 'source' | "$bpwd1" == 'doc' ) then
   set DIFFNAME = $MCTDH_DIR/$bpwd1/$bpwd
else if ( "$bpwd2" == 'source' | "$bpwd2" == 'doc' ) then
   set DIFFNAME = $MCTDH_DIR/$bpwd2/$bpwd1/$bpwd
else if ( "$bpwd3" == 'source' | "$bpwd3" == 'doc' ) then
   set DIFFNAME = $MCTDH_DIR/$bpwd3/$bpwd2/$bpwd1/$bpwd
else if ( "$bpwd4" == 'source' | "$bpwd4" == 'doc' ) then
   set DIFFNAME = $MCTDH_DIR/$bpwd4/$bpwd3/$bpwd2/$bpwd1/$bpwd
else
   set DIFFNAME = ' '
endif
set EDIFF='cat'
which ediff > /dev/null && set EDIFF='ediff'

#-----------------------------------------------------------------------
# GET SYSTEM NAME
#-----------------------------------------------------------------------

set system=`uname -s`
set machine=`uname -n`

if ( "$system" == 'OSF1' ) then
   set OPTN = '-biwt'
else
   set OPTN = '-B -biwtd'
endif
set isw = 0
set i=1
if ( $#argv == 0 ) goto help


# Loop over options
while ($i <= $#argv)
  if ( "$argv[$i]" !~ "-"? ) goto start
    switch ($argv[$i])
    case '-f':
      @ i++
      set DIFFNAME=$argv[$i]
      breaksw
    case '-i':
      set isw = 1
      breaksw
    case '-d':
      set isw = 2
      breaksw
    case '-s':
      set isw = 3
      breaksw
    case '-c':
      set OPTN = '-d'
      breaksw
    case '-q':
      set isw = 4
      breaksw
    case '-h':
      goto help
      breaksw
    default:
      echo "Unknown parameter: $argv[$i]" ; echo ' '
      goto help
    endsw
  @ i++
end

start:
if ( "$DIFFNAME" == " " ) then
   echo "You are probably not below source of an MCTDH directory."
   echo "pwd = $pwd"
   echo "Cannot automatically determine DIFFNAME. Use option -f."
   exit(1)
endif
echo '************************************************************************'
echo 'compare with directory: ' $DIFFNAME
echo '------------------------------------------------------------------------'
if( -d  $DIFFNAME/ ) then
else
   echo 'directory '$DIFFNAME ' does not exist.'
   echo '  '
   exit(1)
endif

if( $isw == 1 ) goto identical
if( $isw == 2 ) goto differ
if( $isw == 3 ) goto short
if( $isw == 4 ) goto qdiff

while ($i <= $#argv)
if ( ( -f "$argv[$i]" ) == 0 )  goto fin
    cmp $argv[$i] $DIFFNAME/$argv[$i] -s && goto nothing
    echo '-----------------------------------------------------------------------'
    echo 'comparing file :' $argv[$i]
    diff $OPTN   $argv[$i] $DIFFNAME/$argv[$i] | $EDIFF
    echo '-----------------------------------------------------------------------'
    echo ' '
    goto fin
    nothing:
    echo 'identical file :' $argv[$i]
    fin:
  @ i++
end
exit(1)

qdiff:
while ($i <= $#argv)
   if ( ( -f "$argv[$i]" ) == 1 )  then
       diff $OPTN -q   $argv[$i] $DIFFNAME/$argv[$i]
   endif
  @ i++
end
exit(1)

short:
while ($i <= $#argv)
     if ( ( -f "$argv[$i]" ) == 0 )  goto fins
     if( -e  $DIFFNAME/$argv[$i] ) then
      set ret=0
      cmp -s $argv[$i] $DIFFNAME/$argv[$i] && set ret=1
      if( $ret == 0 ) then
	echo  $argv[$i] 'is different'
      else if( $ret == 1 ) then
	echo  $argv[$i] 'is identical'
      endif
    else
      echo  $argv[$i] 'does not exist'
    endif
    fins:
  @ i++
end
exit(1)

differ:
while ($i <= $#argv)
     if ( ( -f "$argv[$i]" ) == 0 )  goto find
    if( -e  $DIFFNAME/$argv[$i] ) then
      set ret=0
      cmp -s $argv[$i] $DIFFNAME/$argv[$i] && set ret=1
      if( $ret == 0 ) then
        echo  $argv[$i] 'is different'
      endif
    else
        echo  $argv[$i] 'does not exisit'
    endif
    find:
  @ i++
end
exit(1)

identical:
while ($i <= $#argv)
    if ( ( -f "$argv[$i]" ) == 1 )  then
       set ret=0
       cmp -s $argv[$i] $DIFFNAME/$argv[$i] && set ret=1
       if( $ret == 1 ) then
       echo  $argv[$i] 'is identical'
       endif
    endif
  @ i++
end
exit(1)


help:
    echo '  '
    echo 'Purpose: ddiff compares files of current directory with those of'
    echo '         the directory : '$DIFFNAME
    echo '  '
    echo 'Usage:   ddiff [-s || -d || -i] [-c] [-q] [-f name]  file'
    echo '   -q:   Let diff run quietly.(similar to -s, but using diff rather than cmp)'
    echo '   -c:   Let diff be case and blank sensitive.'
    echo '   -s:   indicate only whether files are identical or different.'
    echo '   -d:   indicate only if files are different.'
    echo '   -i:   indicate only if files are identical.'
    echo '   -f:   change the directory to be compared with to "name".'
    echo ' file:   file to be compared. One may use the shell shortcuts, e.g. *.f'
    echo '  '
    echo '         Note that -i, -d and -s use cmp and hence identical here'
    echo '         means really identical. Otherweise diff is used and blanks'
    echo '         and cases are ignored (except if -c is set).'
    echo '  '
    echo '         It is often useful to pipe the output to less, e.g. '
    echo '         ddiff *.f | less '
    echo '  '
    echo '         To change the directory to compare the files with,'
    echo '         use the -f option or edit the ddiff shell-script.'
    echo ' '
exit(1)







