#! /bin/csh -f

# @(#) $Header: genupd,v 1.31 94/07/22 13:18:10 deyke Exp $

set newdir=/users/funk/dk5sg/tcp
set olddir=/users/funk/dk5sg/tcp.$1
set patcher=$2

set nonomatch

cd $newdir
sort /usr/local/lib/users > users.$$
mv users.$$ users
set files=()
set sources=(`find \
	*.R \
	Makefile \
	README \
	aos/*.[ch] \
	aos/Makefile \
	bbs/*.[ch] \
	bbs/Makefile \
	bbs/help \
	cc \
	convers/*.[ch] \
	convers/Makefile \
	doc/*.mm \
	doc/*.ms \
	doc/*.txt \
	domain.txt \
	examples/*.* \
	hosts \
	lib/*.[ch] \
	lib/Makefile \
	lib/configure \
	net.rc.$1 \
	netrom_links \
	src/*.[ch] \
	src/Makefile \
	src/cc \
	src/linux_include/*.h \
	src/linux_include/*/*.h \
	users \
	util/*.[ch] \
	util/Makefile \
	util/genupd \
	! -name configure.h -print`)
if ($1 == dk0hu) set sources = ($sources domain.rev.txt)
foreach i ($sources)
	if ( ! { cmp -s $i $olddir/$i } ) then
		set files=($files $i)
	endif
end

cd $olddir
echo $sources | tr ' ' '\012' | sort > /tmp/src$$
find * -print | sort > /tmp/old$$
set remove=()
foreach i (`comm -13 /tmp/src$$ /tmp/old$$`)
	if ( ! -d $newdir/$i ) then
		set remove=($remove $i)
		if ($i:e == c) then
			set remove=($remove $i:r.o)
		endif
	endif
end
rm -f /tmp/src$$ /tmp/old$$

echo 'check() {'
echo '  test -d `dirname $1` || mkdir `dirname $1`'
echo '  if test $2 = d41d8cd98f00b204e9800998ecf8427e ; then'
echo '    : > $1'
echo '  else'
echo '    touch $1'
echo '  fi'
echo '  if test `md5 < $1` != $2 ; then'
echo '    echo ERROR: md5 shows wrong signature for $1 $3 updating.'
echo '    echo Update aborted.'
echo '    exit 1'
echo '  fi'
echo '}'

cd $olddir
foreach i ($files)
	if ( ! -d `dirname $i` ) mkdir `dirname $i`
	touch $i
	echo check $i `md5 < $i` before
end

foreach i ($files)
	echo echo Updating $i
	if ($patcher == patch) then
		echo patch -u -f -s $i "<<'@EOF'"
		gdiff -U1 $i $newdir/$i
		echo @EOF
	else
		echo ex - $i "<<'@EOF'"
		diff -e $i $newdir/$i
		echo x
		echo @EOF
	endif
	echo check $i `md5 < $newdir/$i` after
	if ($patcher == patch) then
		echo rm -f $i.orig $i~
	endif
end

foreach i ($remove)
	echo echo Removing $i
	echo rm -rf $i
end

echo rm -f net.rc
echo ln net.rc.$1 net.rc

echo echo Update complete, no errors.
echo exit 0
