#!/bin/sh -e
#
# $dotat: scripts/rfc,v 1.48 2006/06/07 21:24:43 fanf2 Exp $

usage () {
	echo "usage: $0 [-dfltv] [-Pprinter] <number/index/draft-name>"
	echo "	-f	fetch and format"
	echo "	-l	print"
	echo "	-t	text preview"
	echo "	-u	show URL"
	echo "	-v	print preview"
	exit 1
}

view=view_txt

while getopts dfltuvP: opt
do
	case $opt in
	P)	lpr_opts="$lpr_opts -P$OPTARG"
		view=view_lpr
		;;
	f)	view=fmt_pdf
		;;
	l)	view=view_lpr
		;;
	t)	view=view_txt
		;;
	u)	view=view_url
		;;
	v)	view=view_ps
		;;
	*)	usage
		;;
	esac
done
shift $(($OPTIND - 1))

case $# in
1)	: ok
	;;
*)	usage
	;;
esac

setup () {
	dir=~/doc/rfc
	mkdir -p $dir
	cd $dir
	file=${2##*/}
	basename=${file%.*}
	fileps=$basename.ps
	filefmt=$basename.fmt
	filetxt=$basename.txt
	filehtml=$basename.html
	case $1 in
	rfc)	url=http://www.ietf.org/rfc/$file
		;;
	i-d)	url=http://www.watersprings.org/pub/id/$file
		url2=http://www.ietf.org/internet-drafts/$file
		;;
	jep)	url=http://www.jabber.org/jeps/$file
		;;
	-none-) url=$2
		;;
	esac
}

download () {
	[ -f $file ] || fetch $url ||
		case $url in
		*watersprings*)
			fetch $url2
		esac
}

update () {
	touch -t $(date -r $(($(date +%s) - 86400)) +%Y%m%d%H%M) .old
	if [ ! -f .mark -o ! -f $file ]
	then
		fetch -m $url &&
	    		touch .mark
	elif [ .mark -ot .old ]
	then
		cp -p $file $file.old
		if fetch -m $url
		then
			rm $file.old
			touch .mark
		else
			mv $file.old $file
		fi
        fi
	rm .old
}

case $1 in
http:* | ftp:* )
	setup -none- $1
	download
	;;
/*)	setup -none- $1
	[ $1 -ef ${1##*/} ] || cp $1 .
	;;
draft-*.txt)
	setup i-d $1
	download
	;;
draft-*-[0-9][0-9])
	setup i-d $1.txt
	download
	;;
draft-*)
	for id in `jot -w $1-%02d.txt 99 0`
	do
		setup i-d $id
		download || break
		okid=$id
	done
	case $okid in
	?*)	setup i-d $okid
	esac
	;;
jep-index)
	setup jep alljeps.shtml
	update
	;;
jep-*)	setup jep $1.html
	download
	;;
JEP-*)	setup jep jep${1#JEP}.html
	download
	;;
index)
	case $0 in
	*/rfc)	setup rfc rfc-index.txt
		update
		;;
	*/jep)	setup jep alljeps.shtml
		update
		;;
	esac
	;;
[0-9]*)
	case $0 in
	*/rfc)	setup rfc rfc$1.txt
		download
		;;
	*/jep)	jep=$1
		while [ ${#jep} -lt 4 ]
		do jep=0$jep
		done
		setup jep jep-$jep.html
		download
		;;
	esac
	;;
esac

fmt_html () {
	if [ -f $filetxt ] &&
	   [ $filetxt -nt $0 ] &&
	   [ $filetxt -nt $file ]
	then
		return
	fi
	case $file in
	*.txt)	return
		;;
	*.shtml)
		[ -f $file ] && mv $file $filehtml
		;;
	esac
	w3m -cols 72 -dump $filehtml > $filetxt
}

fmt_txt () {
	fmt_html
	if [ -f $filefmt ] &&
	   [ $filefmt -nt $0 ] &&
	   [ $filefmt -nt $file ]
	then
		return
	fi
	# XXX doesn't quite DTRT with page breaks
	case $file in
	alljeps*)
		sed '	/^ATOM/,/--------/!d
		' $filetxt |
		uniq > $filefmt
		;;
	jep-*)	ln -f $filetxt $filefmt
		;;
	*)	sed '	s/$//;s//\
/g' <$filetxt |	sed '	/^RFC [0-9]* /d
			/^[Ii][Nn][Tt][Ee][Rr][Nn][Ee][Tt].[Dd][Rr][Aa][Ff][Tt]/d
			/[[]Page [0-9]*[]]$/d
			/^/d
		' | uniq > $filefmt
		;;
	esac
}

fmt_ps () {
	if [ -f $fileps ] &&
	   [ $fileps -nt $0 ] &&
	   [ $fileps -nt $file ]
	then
		return
	fi
	fmt_txt
	case $basename in
	draft*|rfc-index)
		title="$basename"
		;;
	jep-*)	title=`head -1 $filefmt`
		;;
	*)	num=${basename#rfc}
		# pull the RFC title out of the index
		script="/^[0-9][0-9][0-9][0-9] /h
			/^ /H
			/^$/!d
			g
			/^0*$num /!d
			s///
			s/\n */ /g
			s/\..*//
			q"
		title=`sed "$script" $dir/rfc-index.txt`
		title="$title : $basename"
		;;
	esac
	rm -f $fileps
	a2ps -3B -stumble -t"$title" -b"$title : %s./%s#" -o$fileps < $filefmt
}

fmt_pdf () {
	fmt_ps
	ps2pdf -sPAPERSIZE=a4 $fileps
}

view_txt () {
	fmt_txt
	less $filefmt
}

view_ps () {
	fmt_ps
	gv $fileps
}

view_lpr () {
	fmt_ps
	lpr -s $lpr_opts $fileps
}

view_url () {
	echo $url
}

$view

exit 0
