#!/bin/sh
# deroff -- TeX/LaTex version
# removes TeX and LaTeX commands and write remainder of file on standard output
#
# BUGS: this doesn't really parse TeX input
# 
# AUTHOR:	Gary T. Leavens

[ -n "$echo" ] && set -x

USAGE='Usage: '"$0"' [-w] file
      '"$0"' [-m] file'

single=false	# output words one per line (for spell)?
# the following sed code to take out mathematical constructs (the default)
math='/\\begin{equation}/{:eqloop
				n
				/\\end{equation}/!b eqloop
				d
				}
	/\\begin{eqnarray}/{:ealoop
			     n
			     /\\end{eqnarray}/!b ealoop
			     d
			     }
	/\\begin{eqnarray\\*}/{:ea2loop
			     n
			     /\\end{eqnarray\\*}/!b ea2loop
			     d
			     }
	/\\begin{displaymath}/{:dmloop
			     n
			     /\\end{displaymath}/!b dmloop
			     d
			     }
	/\\begin{math}/{:mloop
			     n
			     /\\end{math}/!b mloop
			     d
			     }
	/\\begin{picture}/{:picloop
			     n
			     /\\end{picture}/!b picloop
			     d
			     }
	s/\$\$[^\$]*\$\$/ /g
	s/\\\[^]]*\\\]/ /g
	s/\$[^\$]*\$/ /g
	s/\\(.*\\)/ /'

case "$1" in
-w) single=true
    shift
    ;;
-m) math=''		
    shift
    ;;
-?) echo "$USAGE" >&2
    exit 2
    ;;
esac

if $single
then
	sed -n -e 's/\\[a-zA-Z][a-zA-Z]*//g
	's/\'\'//g'
	s/^[^a-zA-Z'\''][^a-zA-Z'\'']*//
	s/[^a-zA-Z'\''][^a-zA-Z'\'']*/\
/g
	s/\n$//
	/^$/d
	p
	' $* | sed -e '/^[a-zA-Z]$/d' # take out one letter words for spell
else
	sed -n -e ':start
	/^%/d
	s/\([^\\]\)%.*/\1/
	/\\begin{verbatim}/{:vbloop
			     /^\\end{verbatim}/!{n
						 b start
						 }
			     p
			     n
			     b vbloop
			     }
	'"$math"'
	/\\newcommand/d
	/\\setlength/d
	/^%/d
	s/[^\\]%.*//
	s/{\\em \([^}][^}]*\)}/\1/g
	s/{\\tt \([^}][^}]*\)}/\1/g
	s/{\\bf \([^}][^}]*\)}/\1/g
	s/{\\sf \([^}][^}]*\)}/\1/g
	s/{\\it \([^}][^}]*\)}/\1/g
	s/{\\rm \([^}][^}]*\)}/\1/g
	s/\\cite\[\([^]]*\)]{\([^}]*\)}/[\2, \1]/g
	s/\\cite{\([^}]*\)}/[\1]/g
	s/\\ref{\([^}]*\)}/[\1]/g
	s/\\begin{[a-zA-Z]*}/ /g
	s/\\end{[a-zA-Z]*}/ /g
	s/\\label{\([^}]*\)}/*\1*/g
	s/\\[a-zA-Z][a-zA-Z]*{\([^}]*\)}/\1/g
	s/\\[a-zA-Z][a-zA-Z]*/ /g
	s/~/ /g
	s/\\\///g
	s/\\\([\$&%#{}_]\)/\1/g
	s/\\[^a-zA-Z]/ /g
	/^[ 	][ 	]*$/d
	p
	' $*
fi
