#!/bin/rc

rfork en
path = /bin		# lose '.'
t1 = /tmp/rfcmirror.t1.^$pid^.^$sysname
t2 = /tmp/rfcmirror.t2.^$pid^.^$sysname

dest = /lib/doc/ietf/rfc

myname = $user
myhost = $sysname

if (~ $#user 0) {
	myname = unknown
}
if (~ $#sysname 0) {
	myhost = unknown
}
flagfmt='d,v'
if(! ifs=() eval `{aux/getflags $*}) {
	aux/usage
	exit usage
}
if (~ $flagv 1) {
	fn print { echo $* }
}
if not {
	fn print {}
}
if (~ $flagd 1) {
	opts = -d
}
if not {
	opts = -q
	fn sigexit {rm -f $t1 $t2; unmount /n/ftp; unmount /tmp}
	ramfs
}

test -d $dest || mkdir -p $dest || exit $status
ftpfs $opts -r /in-notes -a $myname^@^$myhost ftp.rfc-editor.org
cd /n/ftp
test -e rfc-index.txt || exit 'rfc-index not found'

ls | grep '^rfc[0-9]+.txt' | sed -e 's/^rfc//' -e 's;\.txt$;;' \
	| awk '{printf("%0.4d\n", $1)}' | sort -n > $t1
ls $dest | grep '[0-9]+' | sed 's;^.*/;;' | sort -n > $t2
for (i in `{comm -23 $t1 $t2}) {
	print '> ' $i
	cp rfc^`{echo $i | sed 's/^0+//'}^.txt $dest^/_tmp || exit $status
	mv $dest^/_tmp $dest^/^$i || exit $status
}
cp rfc-index.txt $dest^/_index
sed -n '1,/^=/d
/^=/,/^=/p' rfc-index-latest.txt | grep -v '^=' > $dest/_latest
cp std-index.txt $dest^/_std
cd $dest

if (! cmp -s _latest /lib/news/latest_rfcs)
	cp _latest /lib/news/latest_rfcs
