#!/bin/tcsh
#
# Data Assimilation Research Testbed -- DART
# Copyright 2004-2007, Data Assimilation Research Section
# University Corporation for Atmospheric Research
# Licensed under the GPL -- www.gpl.org/licenses/gpl.html
#
# <next few lines under version control, do not edit>
# $URL: http://subversion.ucar.edu/DAReS/DART/trunk/shell_scripts/Workshop_Run.csh $
# $Id: Workshop_Run.csh 2691 2007-03-11 18:18:09Z thoar $
# $Revision: 2691 $
# $Date: 2007-03-11 12:18:09 -0600 (Sun, 11 Mar 2007) $

# set echo
set noclobber
set SNAME = $0

if ($#argv != 2) then

   echo
   echo "usage: $SNAME:t Directory1 Directory2"
   echo 'Recursively compares Directory1 with Directory2'
   echo 'If the same filename appears in both directories and'
   echo 'is different, $XDIFF will be used to display the differences.'
   echo
   echo 'This script attempts to compensate for different CVS/SVN'
   echo 'keywords and such by removing "obvious" lines and comparing'
   echo 'the remainder. If differences STILL exist, a graphical comparator'
   echo 'is used to view the differences of the ORIGINAL files.'
   echo
   echo 'It is best if the runtime output is logged, and you are HIGHLY'
   echo 'encouraged to run it both ways to catch files that exist in one'
   echo 'directory but not the other. i.e.'
   echo
   echo "$SNAME:t Directory1 Directory2 |& tee log12.txt"
   echo "$SNAME:t Directory2 Directory1 |& tee log21.txt"
   echo
   echo 'This script is, admittedly, slow but thorough.'
   echo
   @ MYSTATUS = 1  
   exit

else

   set ORIGDIR = $1
   set  NEWDIR = $2
   set TMPFILE = ${TMPDIR}/dir1files.$$
   set NEWFILE = ${TMPDIR}/dir2files.$$

   if ( ${?XDIFF} > 0 ) then
      echo "comparing with ${XDIFF}"
   else if (  -e  /contrib/bin/xdiff ) then
      set XDIFF = /contrib/bin/xdiff
   else if (  -e  /usr/local/bin/xdiff ) then
      set XDIFF = /usr/local/bin/xdiff
   else
      set XDIFF = xxdiff
   endif

   #--------------------------------------------------
   # Echo what we're doing
   #--------------------------------------------------

   echo
   echo "You have asked to compare $ORIGDIR with $NEWDIR ..."
   echo

   #--------------------------------------------------
   # Compile a list of files to check
   #--------------------------------------------------

   find $ORIGDIR -type f > $TMPFILE

   # exclude comparing backup files, etc.

   foreach EXTENSION ( "%" "\~" "#" '.o$' '.mod$' ) 

      grep -v $EXTENSION $TMPFILE > $NEWFILE
      mv $NEWFILE $TMPFILE

   end

   # exclude comparing CVS administration files
   grep -v CVS $TMPFILE >! $NEWFILE
   mv $NEWFILE $TMPFILE

   # exclude comparing SVN administration files
   grep -v "/.svn/" $TMPFILE >! $NEWFILE
   mv $NEWFILE $TMPFILE

   # exclude comparing bgrid_model source files
   grep -v "fms_src" $TMPFILE >! $NEWFILE
   mv $NEWFILE $TMPFILE

   # exclude comparing bufr source code
   grep -v "prep_bufr" $TMPFILE >! $NEWFILE
   mv $NEWFILE $TMPFILE

   # exclude comparing html files
   grep -v 'html$' $TMPFILE >! $NEWFILE
   mv $NEWFILE $TMPFILE

   # exclude comparing anything in the tutorial
   grep -v "tutorial" $TMPFILE >! $NEWFILE
   mv $NEWFILE $TMPFILE

   #--------------------------------------------------
   # generate the list of files expected to be in the
   # second directory and start traversing ...
   #--------------------------------------------------

   set STRING = "1,$ s#$ORIGDIR#$NEWDIR#"
   sed -e "$STRING" $TMPFILE >! $NEWFILE

   set fileatts = `wc $TMPFILE`
   @ nfiles = $fileatts[1]
   @ nf = 1
   while ( $nf <= $nfiles )    # loop through all the files

      set OrgFile = `head -$nf $TMPFILE | tail -1`
      set NewFile = `head -$nf $NEWFILE | tail -1`

      if ( -f $NewFile ) then   ;# file exists, compare

         set EXTENSION = $NewFile:e

         switch ( $EXTENSION )
            case fm:
               set numdiffs = `diff $OrgFile $NewFile | wc`
               breaksw
  
            case pdf:
               set numdiffs = `diff $OrgFile $NewFile | wc`
               breaksw

            case nc:
               set numdiffs = `diff $OrgFile $NewFile | wc`
               breaksw

            case eps:
               set numdiffs = `diff $OrgFile $NewFile | wc`
               breaksw

            case png:
               set numdiffs = `diff $OrgFile $NewFile | wc`
               breaksw

            case gif:
               set numdiffs = `diff $OrgFile $NewFile | wc`
               breaksw

            default:

               grep -v '$Source'      $OrgFile >! org.$$.1
               grep -v '$Revision'    org.$$.1 >! org.$$.2
               grep -v '$Date'        org.$$.2 >! org.$$.3
               grep -v '$Author'      org.$$.3 >! org.$$.4
               grep -v '$Name'        org.$$.4 >! org.$$.5
               grep -v '$Id'          org.$$.5 >! org.$$.6
               grep -v '$URL'         org.$$.6 >! org.$$.7
               grep -v 'do not edit>' org.$$.7 >! org.$$.8

               grep -v '$Source'      $NewFile >! new.$$.1
               grep -v '$Revision'    new.$$.1 >! new.$$.2
               grep -v '$Date'        new.$$.2 >! new.$$.3
               grep -v '$Author'      new.$$.3 >! new.$$.4
               grep -v '$Name'        new.$$.4 >! new.$$.5
               grep -v '$Id'          new.$$.5 >! new.$$.6
               grep -v '$URL'         new.$$.6 >! new.$$.7
               grep -v 'do not edit>' new.$$.7 >! new.$$.8

               set numdiffs = `diff -w org.$$.8 new.$$.8 | wc`
               breaksw
         endsw

         if ( $numdiffs[1] > 0 ) then
            echo "$OrgFile and $NewFile differ ... comparing ..."
            ${XDIFF} $OrgFile $NewFile   
         else
            echo "$OrgFile and $NewFile are identical ... nothing to do."
         endif

         \rm -f org.$$.? new.$$.?

      else
         echo "$NewFile does not exist ..."
      endif

      @ nf = $nf + 1
   end

   \rm -f $TMPFILE
   \rm -f $NEWFILE

   echo "$SNAME:t complete."

   @ MYSTATUS = 0  
endif
