if test "$*" = ""; then
 echo "$0 [-r] [-t dir3] [-z] [-y] [-n] dir1 dir2

			merge dir1 into dir2 (selectively)

	-r		recursive subdirectory operation
	-t dir3		store differences in dir3 (tree)
	-z		zap (remove) from dir1 if match with dir2
	-y		yes - force merge
	-n		no - never merge"
  exit 100
fi
set -- `getopt rt:zyn $*`
while test "$1" != "--"; do
 if test "$1" = "-r"; then rflag=y; fi
 if test "$1" = "-t"; then tflag=y; shift; tname=$1; fi
 if test "$1" = "-z"; then zflag=y; fi
 if test "$1" = "-y"; then yflag=y; fi
 if test "$1" = "-n"; then nflag=y; fi
 shift
done
shift
d1=$1; d2=$2; shift 2
case "$d2" in /* ) b2=/ ;; * ) b2=`pwd` ;; esac
tst(){ test "$nflag" || ((test "$yflag" ||
 (if test "$2"; then eval "/bin/ls -ld $2"; fi
  echo "$1cp $d1/$fn $d2/$fn ?\c"
  read ans < /dev/tty; test "$ans" = "y")) && eval "$3");}
(cd $d1; if test "$rflag"; then /bin/find ${*-"."} -type f -print
                           else /bin/ls -a ${*-"."}; fi) |
 while read fn; do
  if test "$fn" = "." -o "$fn" = ".."; then continue; fi
  if test -d $d1/$fn -a ! -d $d2/$fn; then
   tst "*** dir  add  *** " "$d1/$fn" \
       "(qmkdir $b2/$d2; cd $d1; /bin/find $fn -print |
                                 /bin/cpio -pvdum $b2/$d2)"
  elif test -f $d2/$fn; then
   if test "$tflag"; then
    if /bin/cmp $d2/$fn $d1/$fn > /dev/null
     then rm -f $tname/$fn
     else (qmkdir $tname; /bin/diff $d2/$fn $d1/$fn > $tname/$fn)
    fi
    continue
   fi
   if /bin/cmp $d2/$fn $d1/$fn > /dev/null; then
    echo '*** file same ***' $d1/$fn
    if test "$zflag"; then rm -f $d1/$fn; fi
   else
    test "$nflag" -o ! "$yflag" &&
     ((echo '*** file diff ***' $fn 'see diffs ?'"\c"; read ans < /dev/tty
      test "$ans" = "y") && /bin/diff $d2/$fn $d1/$fn | pg)
    tst "" "$d1/$fn $d2/$fn" "(cd $d1; /bin/ls $fn | /bin/cpio -pvdum $b2/$d2)"
   fi
  elif test ! -d $d2/$fn; then
   tst "*** file add  *** " "$d1/$fn" \
       "(qmkdir $b2/$d2; cd $d1; /bin/ls $fn | /bin/cpio -pvdum $b2/$d2)"
  fi
 done
