#! /bin/tcsh
#
# plain2html:	plain text to HTML file
#		by k-chinen@is.aist-nara.ac.jp , 1994
#
# NOTE:
# 	* This script will be running under csh or tcsh.
# 	* Handle with care for directory, because this script make
#	  a lot of temporary file and output file.
#
# HTML table extension by KOBAYASHI Kenichi, 2001
#
# NOTE:
#	* pass non plain2html options to plain2
# 	* pnmcrop becomes needless
# 	* pt2htcol.awk becomes needless
#	* avoid TBL and PIC filename confliction
#	* add -q, -c, -dpi, and -avoid options
#	* automatically select jlatex or platex

#
# require:
#
#    program:
#	csh or tcsh
#	plain2 ( have -html option verison )
#	awk, sed
#	groff ( gtbl, gpic )
#	latex or jlatex
#	dvi2ps
#	convert ( convertable PS to Any-format version )
#	ghostscript ( called by convert when convert PS format )
#
# input:
#	$1 (plain text file name.  e.g., README.j)
#
# output:
#	$1:r.html  (e.g., README.html)
#
# temporary file:
#	_src, _cut.sh , _out.html , pt2empty.sty, _tmp.pnm
#	TBL*.p TBL*.ps TBL*.src TBL*.tex TBL*.dvi TBL*.pnm
#	PIC*.p PIC*.ps PIC*.src PIC*.tex PIC*.dvi PIC*.pnm
#				


#
# Default values
#
set tex=1
set roff=0
set options=()
set quiet=0
set stdout=0
set dpi=144
set avoid=0
set ext="gif"

#
# Check args.
#
while ( $#argv > 0 )
	switch ($1)
	case -tex:
		set tex=1
		set roff=0
		shift
		breaksw
	case -roff:
		set tex=0
		set roff=1
		shift
		breaksw
	case -q:
		set quiet=1
		shift
		breaksw
	case -c:
		set quiet=1
		set stdout=1
		shift
		breaksw
	case -dpi:
		shift
		set dpi=$1
		shift
		breaksw
	case -jpeg:
	case -jpg:
		set ext=jpg
		shift
		breaksw
	case -png:
		set ext=png
		shift
		breaksw
	case -avoid:
		set avoid=1
		shift
		breaksw
	case -*:
		set options = ($options $1)
		shift
		breaksw
	default:
		break
	endsw
end

if ( $#argv > 0) then
	set target=$1
	if ($avoid) then
		set avoid_pre="$target:r_"
	else
		set avoid_pre=""
	endif
else
	cat << END_USAGE
plain2html: plain text to HTML file
	by k-chinen@is.aist-nara.ac.jp NAIST , 1994

usage: plain2html [option] input-filename

option:		-tex	TeX mode (use latex, dvi2ps) [defaults]
		-roff	roff mode (use groff)
		-q	quiet mode
		-c	use stdout (-q is implied)
		-dpi n	specify dpi (default value is 144)
		-avoid	avoid filename confliction
		-png	use PNG format
		-jpeg	use JPEG format
END_USAGE
	exit
endif


#
# search latex command
#
if ( { platex -v } ) then >& /dev/null
	set latex = "platex"
	set dvi2ps = "dvips"
else if ( { jlatex -v } ) then >& /dev/null
	set latex = "jlatex"
	set dvi2ps = "dvi2ps"
else
	set latex = "latex"
	set dvi2ps = "dvi2ps"
endif


#
# Start
#	convert document's main body.
#	collect table/picture location and cut these.
#

if (! $quiet ) echo "Copy Target $target ."
cp $target _src

if (! $quiet) echo "Process document main body"
if (! $avoid && $ext == "gif") then
	plain2 -html -here $options _src > _out.html
else if (! $avoid) then
	plain2 -html -here $options _src \
	| sed -e s/\\\(\<IMG\ SRC=\"\[PT]\[IB]\[CL]\[0-9]\[0-9]\[0-9]\[0-9]\[0-9]\\.\\\)gif/\\1$ext/ > _out.html
else
	plain2 -html -here $options _src \
	| sed -e s/\\\(\<IMG\ SRC=\"\[PT]\[IB]\[CL]\[0-9]\[0-9]\[0-9]\[0-9]\[0-9]\.\\\)gif/\\1$ext/ \
	| sed -e s/\\\(\<IMG\ SRC=\"\\\)\\\(\[PT]\[IB]\[CL]\[0-9]\\\)/\\1$avoid_pre\\2/ > _out.html
endif

if (! $quiet) echo "Collect Table/Picutre"
awk -f - _out.html > _cut.sh <<EOF
/^<!-- plain2:TABLE/{
	# print "# " \$0
	printf "# table %d th, start %d, end %d\n", \$3, \$4, \$5
	printf "echo '[[T' > TBL%05d.p\n", \$3
	printf "head -%d _src| tail -%d >> TBL%05d.p\n",\$5-1, \$5-\$4, \$3
	printf "echo ']]T' >> TBL%05d.p\n", \$3
}
/^<!-- plain2:PICTURE/{
	# print "# " \$0
	printf "# picture %d th, start %d, end %d\n", \$3, \$4, \$5
	printf "echo '[[P' > PIC%05d.p\n", \$3
	printf "head -%d _src| tail -%d >> PIC%05d.p\n",\$5-1, \$5-\$4, \$3
	printf "echo ']]P' >> PIC%05d.p\n", \$3
}
EOF

#
# Convert parts(table/picture) via LaTeX
#	If you don't have jlatex substitute to "latex".
#	( jlatex is Japanese LaTeX )
# 
if ( $tex ) then
cat << END_STYLEFILE > pt2empty.sty
\\thispagestyle{empty}
\\pagestyle{empty}
END_STYLEFILE
cat << END_TEX_CUT >> _cut.sh
set p = ()
set p = (TBL*.p PIC*.p) >& /dev/null
foreach i ( \$p ) 
	echo "Part \$i process"
        plain2 -tex -jis -tstyle=pt2empty \$i > \$i:r.tex
        $latex \$i:r.tex
        $dvi2ps \$i:r.dvi > \$i:r.ps
	convert -density "$dpi"x"$dpi" -crop 0x0 \$i:r.ps $avoid_pre\$i:r.$ext
	rm -f \$i:r.ps \$i:r.dvi \$i:r.aux \$i:r.log \$i:r.tex \$i:r.p \$i:r.pnm
end
END_TEX_CUT
endif

#
# Convert parts(table/picture) via GROFF
#	If you don't have gorff substitute to ROFF-like program.
#
if ( $roff ) then
cat << END_ROFF_CUT >> _cut.sh
set p = ()
set p = (TBL*.p PIC*.p) >& /dev/null
foreach i ( \$p ) 
	echo "Part \$i process"
	plain2 -roff -euc  \$i > \$i:r.src
	groff -me -t -p \$i:r.src > \$i:r.ps
	convert -density "$dpi"x"$dpi" -crop 0x0 \$i:r.ps $avoid_pre\$i:r.$ext
	rm -f \$i:r.ps \$i:r.src \$i:r.p \$i:r.pnm
end
END_ROFF_CUT
endif

#
# Run parts conversion script
#
if (! $quiet) echo "Cut Table/Picture"
if ($stdout) then
	$shell _cut.sh >& /dev/null
else
	$shell _cut.sh
endif

rm -f _cut.sh pt2empty.sty _src _tmp.pnm
if ($stdout) then
	cat _out.html
	rm -f _out.html
else
	rm -f $target:r.html
	mv _out.html $target:r.html
endif

if (! $quiet) echo "Complete"
