#!/bin/bsh
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# bos720 src/bos/usr/bin/dircmp/dircmp.sh 1.17 
#  
# Licensed Materials - Property of IBM 
#  
# COPYRIGHT International Business Machines Corp. 1985,1993 
# All Rights Reserved 
#  
# US Government Users Restricted Rights - Use, duplication or 
# disclosure restricted by GSA ADP Schedule Contract with IBM Corp. 
#  
# IBM_PROLOG_END_TAG 
# @(#)09        1.17  src/bos/usr/bin/dircmp/dircmp.sh, cmdfiles, bos720 4/26/02 00:18:55
#
# COMPONENT_NAME: (CMDFILES) commands that manipulate files
#
# FUNCTIONS: dircmp
#
# ORIGINS: 3, 18, 27
#
# (C) COPYRIGHT International Business Machines Corp. 1985, 1993
# All Rights Reserved
# Licensed Materials - Property of IBM
#
# US Government Users Restricted Rights - Use, duplication or
# disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
#
# (c) Copyright 1990, OPEN SOFTWARE FOUNDATION, INC.
# ALL RIGHTS RESERVED
#
# (No OSF RCS id string found)
#

# Portability: check for AIX dspmsg utility.
type dspmsg >/dev/null
DM=$?
# Portability: Handle all uses of dspmsg according to this code template
# so the code will work even if  dspmsg  is not defined:
#if [ $DM = 0 ] ; then
#dspmsg -s 1 dircmp.cat <msg_id> <default message>
#else echo <<default message> WITHOUT final newline and WITH substitutions>
#fi

PATH=/bin:/usr/bin 
umask 077
TMP=/usr/tmp
TMP=$TMP/bsh.$$
mkdir $TMP || {
	echo "bsh: Could not create temporary files" 1>&2
	exit 1
}
Wflag=0
trap "rm -fr $TMP;exit" 1 2 3 15
# Local subprogram BADUSAGE to display Usage message and exit.
BADUSAGE() {
if [ $DM = 0 ] ; then
dspmsg -s 1 dircmp.cat 14 "Usage: dircmp [-ds] [-w num] directory1 directory2\n" 1>&2
else echo "Usage: dircmp [-ds] [-w num] directory1 directory2" 1>&2
fi
rm -rf $TMP
exit 1
}
# End of local subprogram BADUSAGE
CLEANUP() {
rm -rf $TMP
exit 2
} 
# 
# Get options and Directory arguments.
set -- `getopt dsw: $* 2>/dev/null`
if [ $? != 0 ]
then BADUSAGE
fi 
while [ .$1 != . ]
do
  case $1 in
  -d )	Dflag="yes"
	shift
	;;
  -s )	Sflag="yes"
	shift
	;;
  -w )	Wflag=1
	width=`expr $2 + 0 2>/dev/null`
	if [ $? = 2 ]
	then
		if [ $DM = 0 ]
		then dspmsg -s 1 dircmp.cat 23 "Numeric argument is required for -w option\n" 1>&2
		else echo "dircmp: Numeric argument is required for -w option\n" 1>&2
		fi
	exit 2
	fi
	shift 2
	;;
  -- )	shift
	break
	;;
  * )	break
	;;
  esac
done
D1=$1
D2=$2
if [ -z "$D1" -o -z "$D2" -o -n "$3" ]
then BADUSAGE
fi
# Check that directory arguments really are directories.
NOTD=
if [ ! -d $D1 ]
then NOTD=$D1
elif [ ! -d $D2 ]
then NOTD=$D2
fi
if [ -n "$NOTD" ]
then
  if [ $DM = 0 ] ; then
  dspmsg -s 1 dircmp.cat 15 "dircmp: %s is not a directory.\n" $NOTD 1>&2
  else echo "dircmp: $NOTD is not a directory." 1>&2
  fi
  CLEANUP
fi 

D0=`pwd`
if cd $D1
then find . -print | sort > $TMP/dc$$a
     if [ $? != 0 ]
     then CLEANUP
     fi
else CLEANUP
fi
cd $D0
if cd $D2
then find . -print | sort > $TMP/dc$$b
     if [ $? != 0 ]
     then CLEANUP
     fi
else CLEANUP
fi
comm $TMP/dc$$a $TMP/dc$$b | sed -n \
	-e "/^		/w $TMP/dc$$c" \
	-e "/^	[^	]/w $TMP/dc$$d" \
	-e "/^[^	]/w $TMP/dc$$e"
rm -f $TMP/dc$$a $TMP/dc$$b
if [ $DM = 0 ] ; then
UNIQHDR=`dspmsg -s 1 dircmp.cat 16 '%1$s only and %2$s only' $D1 $D2`
else UNIQHDR='$D1 only and $D2 only'
fi
if [ $Wflag = 1 ]
then
pr -w $width -h "$UNIQHDR" -m $TMP/dc$$e $TMP/dc$$d
else
pr -h "$UNIQHDR" -m -s" " $TMP/dc$$e $TMP/dc$$d
fi
rm -f $TMP/dc$$e $TMP/dc$$d
sed -e s/..// < $TMP/dc$$c > $TMP/dc$$f
rm -f $TMP/dc$$c
cd $D0
if [ $DM = 0 ] ; then
COMPHDR=`dspmsg -s 1 dircmp.cat 17 'Comparison of %1$s and %2$s' $D1 $D2`
else COMPHDR='Comparison of $D1 and $D2'
fi
> $TMP/dc$$g
while read a
do
	if [ -d $D1/"$a" ]
	then if [ "$Sflag" != "yes" ]
	     then
		if [ $DM = 0 ]
	        then dspmsg -s 1 dircmp.cat 21 "directory\t%s\n" $a
		else echo "directory\t$a"
		fi
	     fi
	elif [ -f $D1/"$a" ]
	then cmp -s $D1/"$a" $D2/"$a"
	     if [ $? = 0 ]
	     then if [ "$Sflag" != "yes" ]
		  then
			if [ $DM = 0 ]
		 	then dspmsg -s 1 dircmp.cat 19 "same\t\t%s\n" $a
			else echo "same\t\t$a"
			fi
		  fi
	     else
		  if [ $DM = 0 ] ; then
		  dspmsg -s 1 dircmp.cat 20 "different\t%s\n" $a
		  else echo "different\t$a"
	 	  fi
		  if [ "$Dflag" = "yes" ]
		  then
		    if [ $DM = 0 ] ; then
	       	    DIFFHDR=`dspmsg -s 1 dircmp.cat 18 \
				'diff of %1$s in %2$s and %3$s' $a $D1 $D2`
		    else DIFFHDR= 'diff of $a in $D1 and $D2'
		    fi
	 	    diff $D1/"$a" $D2/"$a" | pr -h "$DIFFHDR" >> $TMP/dc$$g
		  fi
	     fi
	elif [ "$Sflag" != "yes" ]
	then
		if [ $DM = 0 ]
		then dspmsg -s 1 dircmp.cat 22 "special\t %s\n" $a
		else echo "special\t $a"
		fi
	fi
done < $TMP/dc$$f | pr -r -h "$COMPHDR"
if [ "$Dflag" = "yes" ]
then cat $TMP/dc$$g
fi
rm -rf $TMP
exit 0