:
# make a Bourne Shell procedure that generates a new release:
# compare all files *.[chx] of directories $1 (old) and $2 (new);
# with "to_$2" you can change the old files into new ones;
# if $3 is given, it replaces the pattern *.[chx] in the find-command
#
# R.Wobst, May 27/Jul 25-26, Oct 14-16, 1991

[ $# -lt 2 ] && \
	{ echo "usage: ediff dirold dirnew [pattern ...]\07"; exit 1; }
[ ! -d "$1"  -o  ! -d "$2" ] && \
	{ echo "ediff: $1 and $2 must be directories\07"; exit 1; }

OLD=$1
NEW=$2
TMP=`pwd`/tmp$$
DIFF=`pwd`/to_$NEW
END="This_string_is_complete_nonsens"

trap "/bin/rm -f $TMP* $DIFF; exit 1" 1 2 3

: > $TMP.0		# collects shell commands: /bin/rm ...
: > $TMP.1		# stores the "diff -e" output
: > $TMP.2		# compiles the ed script

# built arguments for "find"

shift; shift
case $# in
  0) FINDARG="-name \\*.[chx]";;
  1) FINDARG="-name \"$1\"";;
  *) FINDARG="\\( -name \"$1\""
     while [ $# -gt 1 ]
     do
       shift; FINDARG="$FINDARG -o -name \"$1\""
     done
     FINDARG="$FINDARG \\)"
esac

echo "\ndifferences appeared in the following files:\n"

# search for files to be deleted

(
 cd $OLD
 eval find . $FINDARG ! -type d -print
) |
while :
do
   read word || break
   if [ ! -f $NEW/$word ]
   then
     echo "/bin/rm $word" >> $TMP.0
     echo ">>>" $word - deleted
   fi
done

# search for different or new files

(
 cd $NEW
 eval find . $FINDARG ! -type d -print
) |
while :
do
   read word || break
   if [ -f $OLD/$word ]
   then
     diff -e $OLD/$word $NEW/$word >$TMP.1
     if [ -s $TMP.1 ]
     then
       echo $word
       echo "e $word"	>>$TMP.2
       cat $TMP.1	>>$TMP.2
       echo w 		>>$TMP.2
     fi
   else
     echo ">>>" $word - created
     echo "1,\$d\n0a"	>>$TMP.2
     cat $NEW/$word	>>$TMP.2
     echo ".\nw $word"	>>$TMP.2
   fi
done

if [ -s $TMP.0 -o -s $TMP.2 ]
  then
    echo ":"		>$DIFF
    cat $TMP.0		>>$DIFF

    if [ -s $TMP.2 ]
    then
      echo "ed - <<\\$END" >>$DIFF
      cat $TMP.2	>>$DIFF
      echo "q\n$END"	>>$DIFF
    fi

    echo "\"*** to_$NEW is done ***\"" >>$DIFF
    chmod 744 $DIFF
  else
    echo "no differences found\07!"
    /bin/rm -f $TMP*
    exit 0
fi

/bin/rm -f $TMP*

echo "\n*** ediff: all work is done - generated shell procedure: to_$NEW ***\07"
exit 0
