#!/bin/csh -f

if( .$3 == . ) then
    set filetype = checkpoint
else
    set filetype = $3
endif

# Create List of Comparison Files
# -------------------------------
set dir = `pwd`
cd  $1
set chk_files = `/bin/ls -1 *${filetype}*`

set restarts = ''
foreach file ($chk_files)

# Compute number of "Dot" delimiters
# ----------------------------------
  @ n = 0
  @ b = 1
  set bit = `echo $file | cut -b $b`
      while( "${bit}" != '' )
         if( "${bit}" == '.' ) then
                  @ n = $n + 1
         endif
                  @ b = $b + 1
         set bit = `echo ${file} | cut -b $b`
      end
  #echo Number of Dots for ${file}: $n
 
# Find node matching "internal" or "import"
# -----------------------------------------
  if( $n != 0 ) then
     @ m = 1
     set node = `echo $file | cut -d. -f$m`
     while( $node != '' )
       set check_internal = `echo $node | grep internal`
       set check_import   = `echo $node | grep import`
       if( $check_internal == $node | \
           $check_import   == $node ) then
                set rstname = $node
                set    node = ''
       else
            @ m = $m + 1
            set node = `echo $file | cut -d. -f$m`
       endif
     end
  else
     set rstname = $file
  endif

# Add name to list of restarts
# ----------------------------
  set found = FALSE
  foreach rst ($restarts)
     if( $rst == $rstname ) set found = TRUE
  end
  #echo RESTART Name: $rstname  FOUND: $found
  if( $found == FALSE ) then
      set restarts = `echo $restarts $rstname`
  endif

end

echo ""
echo "Comparing Files ..."
foreach rst ($restarts)
echo "  $rst"
end
echo ""

cd $dir

# Compare Files
# -------------
foreach restart ( $restarts )
echo "Comparing $restart ..."
echo "---------------------------------"
             set rsts1 = `/bin/ls -1 $1/*${restart}*`
             set rsts2 = `/bin/ls -1 $2/*${restart}*`
             if( $#rsts1 != 0 & $#rsts2 != 0 ) then
               foreach rst1 ($rsts1)
                foreach rst2 ($rsts2)
                   set frmt1 = `file -ib $rst1 | cut -d/ -f2`
                   set frmt2 = `file -ib $rst2 | cut -d/ -f2`
                   if( "$frmt1" != "$frmt2" ) then
                        echo "File Formats do not agree!"
                        echo "$rst1  Format: $frmt1"
                        echo "$rst2  Format: $frmt2"
                   else
                        if( "$frmt1" == "x-hdf" ) then
                              set cmd = "cdo -s -diffn"
                        else
                              set cmd = cmp 
                        endif
 
                        if( -e diffs ) /bin/rm -rf diffs
                        echo "  $cmd $rst1 $rst2"
                                $cmd $rst1 $rst2 | tee diffs
                                 set RC = $status
                                if( "$cmd" == "cdo -s -diffn" ) set RC = `cat diffs | cut -do -f1`
                        if( -e diffs ) /bin/rm -rf diffs
                        if( $RC[1] == 0 ) then
                            echo "   Success!"
                            echo " "
                        else
                            echo "   Failed!"
                            echo " "
                            set size1 = `/bin/ls -l $rst1 | cut -d' ' -f5`
                            set size2 = `/bin/ls -l $rst2 | cut -d' ' -f5`
                            if( $size1 != $size2 ) then
                                                      set size = $size1
                                if( $size1 > $size2 ) set size = $size2
                                echo "   Comparing first $size bytes ..."
                                $cmd -n $size $rst1 $rst2
                                if( $status == 0 ) then
                                    echo "   Success!"
                                    echo " "
                                else
                                    echo "   Failed Again!"
                                    echo " "
                                endif
                            endif
                        endif
                   endif
                end
               end
             endif
end
