#!/bin/sh
tmpfile=/tmp/diffdir.$$.tmp
baseline=$1
questionable=$2
logfile=$3
echo ""

while getopts h option
do
   case $option in
      *) help=true ;;
   esac
done

if [ "$help" = "true" -o $# -gt 3 ]
then
more << END_USAGE_TEXT

USAGE:     diffdir [-h] <baseline path> <questionable path> <logfile name>

PURPOSE:   Compares baseline against questionable directory contents.

OPTIONS:   -h      display help screen

SYNOPSIS:  All files contained in the baseline path are compared against
           files residing in the questionable path, results being displayed
           on screen and optionally recorded in the specified log file.
           Files contained only in the questionable path are not compared,
           therefore the path arguments are not commutative in general.
           Transient object files (core, .o, ...) are ignored.

NOTES:     Differences should be expected when files with the same name
           but different contents appear at multiple directory levels.
           Differences should also be expected for executable files which
           were compiled in different environments.

END_USAGE_TEXT
exit 9
fi

baseline=${baseline:=`pwd`}
printf "Enter BASELINE directory [$baseline]: "
read inbaseline
if [ "$inbaseline" ]
then
   baseline="$inbaseline"
fi
if [ -d "$baseline" -a -r "$baseline" -a -x "$baseline" ]
then
   :
else
   echo "ERROR: does not exist: $baseline"
   exit 1
fi

questionable=${questionable:=`pwd`}
printf "Enter QUESTIONABLE directory [$questionable]: "
read inquestionable
if [ "$inquestionable" ]
then
   questionable="$inquestionable"
fi
if [ -d "$questionable" -a -r "$questionable" -a -x "$questionable" ]
then
   :
else
   echo "ERROR: does not exist: $questionable"
   exit 1
fi

logfile=${logfile:=/dev/null}
printf "Enter LOG file [$logfile]: "
read inlogfile
if [ "$inlogfile" ]
then
   logfile="$inlogfile"
fi
touch $logfile 2>/dev/null
if [ $? != 0 ]
then
   echo "ERROR: touch: $logfile"
   exit 1
fi

trap "/usr/bin/rm -f $tmpfile 2>/dev/null; exit 1" 0 1 2 3 15
touch $tmpfile
if [ $? != 0 ]
then
   echo "ERROR: touch: $tmpfile"
   exit 1
fi

printf "\nComparison Started `date`\n\n" >> $logfile
basecount=0
samecount=0
diffcount=0
errcount=0

printf "\nScanning BASELINE directory...\n"
( cd $baseline; find . -type f -print 1>$tmpfile 2>/dev/null; exit $? )
if [ $? != 0 ]
then
   echo "ERROR: find: no files found"
   exit 1
fi

exec <$tmpfile
printf "Scanning QUESTIONABLE directory...\n\n"
while read file
do
   dirname=`dirname $file`
   basename=`basename $file`
   case $dirname in
      */dbxLib*) case $basename in
                    *\.c) continue ;;
                 esac ;;
      */dbpLib*) case $basename in
                    *\.c) continue ;;
                 esac ;;
        */chi/*) case $basename in
                    Makefile) continue ;;
                 esac ;;
      */ixxLib*) case $basename in
                    hold) continue ;;
                 esac ;;
   esac
   case $basename in
      \.make\.state) continue ;;
      *\.o) continue ;;
      core) continue ;;
   esac
   basecount=`expr $basecount + 1`
   target_list=`find $questionable/$dirname -name "$basename" -print 2>/dev/null`
   if [ $? != 0 ]
   then
      echo "    ERROR: find: $questionable/$dirname/.../$basename"
      echo "    ERROR: find: $questionable/$dirname/.../$basename" >> $logfile
      errcount=`expr $errcount + 1`
   else
      shift $#
      set -- $target_list
      if [ $# = 0 ]
      then
         echo "    ERROR: find: $basename not found"
         echo "    ERROR: find: $basename not found" >> $logfile
         errcount=`expr $errcount + 1`
      else
         while [ $# -gt 0 ]
         do
            diff $baseline/$file $1 1>/dev/null 2>/dev/null
            if [ $? != 0 ]
            then
               printf "DIFFERENT: "
               printf "DIFFERENT: " >> $logfile
               diffcount=`expr $diffcount + 1`
            else
               printf "     SAME: "
               printf "     SAME: " >> $logfile
               samecount=`expr $samecount + 1`
            fi
            echo "$1"
            echo "$1" >> $logfile
            shift 1
         done
      fi
   fi
done

printf "\nSummary of Comparison\n\n"
printf "\nSummary of Comparison\n\n" >> $logfile
printf "BASELINE $baseline\n"
printf "BASELINE $baseline\n" >> $logfile
printf "QUESTIONABLE $questionable\n\n"
printf "QUESTIONABLE $questionable\n\n" >> $logfile
printf "%9s: %9d\n" SAME $samecount
printf "%9s: %9d\n" SAME $samecount >> $logfile
printf "%9s: %9d\n" DIFFERENT $diffcount
printf "%9s: %9d\n" DIFFERENT $diffcount >> $logfile
printf "%9s: %9d\n\n" ERRORS $errcount
printf "%9s: %9d\n\n" ERRORS $errcount >> $logfile
printf "%9s: %9d\n" BASELINE $basecount
printf "%9s: %9d\n" BASELINE $basecount >> $logfile
printf "%9s: %9d\n" COMPARED `expr $samecount + $diffcount + $errcount`
printf "%9s: %9d\n" COMPARED `expr $samecount + $diffcount + $errcount` >> $logfile
printf "\nComparison Complete\n\n"
printf "\nComparison Complete\n\n" >> $logfile
exit 0
