#!/bin/sh
commit=
if type fetch >/dev/null 2>&1
then
	fetch=fetch
elif type curl >/dev/null 2>&1
then
	fetch="curl -L -O"
else
	exit 1
fi

for i
do
	z=
	case $i in
	http://tools.ietf.org/html/*|http://www.ietf.org/internet-drafts/*|http://www.ietf.org/id/*)
		z=`expr "$i" : 'http://.*/.*/\(.*\)'`
		;;
	*://*/*)
		echo -n "unknown uri $i"
		continue;
	esac
	if test -n "$z"
	then
		case $z in
		*-[0-9][0-9]) z="$z.txt"''
		esac
		i="$z"
	fi
	if test -f "$i"
	then
		continue
	fi
	pat=`echo "$i" | sed 's/...txt/??.txt/'`
	old=`echo $pat 2> /dev/null`
	if test "X$old" != "X$pat"
	then
		newer=0
		for j in $old
		do
			if test $j ">" $i
			then
				 newer=1
			fi
		done
		if test $newer = 1
		then
			continue;
		fi
	fi
	if $fetch "http://www.ietf.org/id/$i" 
	then
		git add "$i" 
		if test "X$old" != "X$pat"
		then
			rm $old
			git rm $old
			commit="$commit $old"
		fi
		commit="$commit $i"
	fi
done
if test -n "$commit"
then
	git commit -m "new draft"
	git push
fi
