#!/bin/sh

# Read files as text, and write to stdout in PostScript.
# Need jverbatim.sty to be compiled by JLaTeX.

case "$0" in
t2pdf|*/t2pdf)
	TOFMT=pdf; HDR=f; DOKSUBST=p;;
*)
	TOFMT=ps; HDR=t; DOKSUBST=t;;
esac

usage(){
	echo "Usage: `basename $0` [-s PAPERSIZE] [-l LABEL] [-h] [-p] [-n]" \
		"[-r] [filename...]" >&2; exit 1
}
unsup(){
	echo 'Unsupported paper size' >&2; exit 1
}

LABEL=; PAPER=a4; LND=f
while :; do # ץ
	case "$1" in
	-s)	[ $# -lt 2 ] && usage; PAPER="$2"; shift 2;;
	-l)	[ $# -lt 2 ] && usage; LABEL="$2"; shift 2;;
	-h)	[ $HDR = t ] && HDR=f || HDR=t; shift;;
		# resizeboxȤܸlatex2egraphicsѥå
	-p)	[ $TOFMT = tex ] && TOFMT=ps || TOFMT=tex; shift;;
	-n)	[ $DOKSUBST = t ] && DOKSUBST=f || DOKSUBST=t; shift;;
	-r)	[ $LND = t ] && LND=f || LND=t; shift;;    # rotate x and y
	--)	shift; break;;
	-?*)	usage;;
	*)	break;;
	esac
done
[ $# -eq 0 ] && DEFARG=- || DEFARG=

exists(){
	[ -x "`which \"$1\" 2>/dev/null`" ]
	# FreeBSDsh`exec cmd`Ƚ񤯤ȿƥ뤬execƤޤ
}

# ޤܸбawkθƤӽФȼ̤Ƚ
JGAWK=; GAWK_NON_EUC_ORDER=
if exists jgawk; then
	JGAWK=jgawk
elif exists gawk; then
	for i in 'gawk --ctype=EUC' \
		    'env LANG=ja_JP.eucJP gawk' 'env LANG=ja_JP.ujis gawk'; do
		# i='A=B cmd'; $i arg... Τ褦ʤȤϤǤʤ
		# i(){A=B cmd "$@"}Ȥ C=D i arg...SolarisshǤC=D
		# ȿǤʤ OpenBSDshǤϿƥˤޤC=Dꤵ
		# κenv
		if $i 'BEGIN{exit("\xa1\xa1" !~ /^.$/)}' 2>/dev/null; then
			JGAWK="$i"
			# ǶgawkϤȤLANG=ja_JP.ujisǤʸϰϤ
			# utfνӤƤޤ
			if $i 'BEGIN{exit("" ~ /[-]/)}'; then
				GAWK_NON_EUC_ORDER=t
			fi
			break
		fi
	done
	# solarisshϡ롼ΤޤȤ 2>/dev/null ȥ֥
	# Ƥޤ(?)ѿJGAWK꤬롼׳˸ʤʤ
fi
if [ X"$JGAWK" = X ]; then
	echo 'Cannot find Japanized gawk' >&2; exit 1
fi
# 褿ʤ$JGAWKɽܸбgawkΤϤ
# ǤsubstrʤɤХñ̤ɤϤޤޤʤΤǼȽ

# substrʸñ̤ξ0(ξlengthRSTART, RLENGTHʤɤʸñ)
# substrХñ̤ԴʥޥХʸȤ1(gawk+mb)
# substr˥Хñ̤ʤ2(ݲʹ3.1.4ޤǤgawkΤserowjgawk)
$JGAWK 'BEGIN{
	a = substr(b = "\xa1\xa1", 1, 1); exit(a == b ? 0 : a == "" ? 1 : 2)
}' 2>/dev/null; SUBSTR_BYTEWISE=$?

# ǻ極ʤɤ˴ؤ׻򤷤Ƥ
case "$PAPER" in
[Aa]3)	PAPER=a3; PAPERX=29.7; PAPERY=42.0;;
[Bb]4)	PAPER=b4; PAPERX=25.7; PAPERY=36.4;;
[Aa]4)	PAPER=a4; PAPERX=21.0; PAPERY=29.7;;
[Bb]5)	PAPER=b5; PAPERX=18.2; PAPERY=25.7;;
[Aa]5)	PAPER=a5; PAPERX=14.8; PAPERY=21.0;;
letter)	PAPERX=21.59; PAPERY=27.94;;
*)	AWKSCR1='BEGIN{
		if(split(ARGV[1], xy, /,/) != 2) exit(1)
		for(i = 1; i <= 2; i++){
			if(sub(/cm$/, "", xy[i]) != 1 || # cm only
			   xy[i] !~ /^([0-9]+|[0-9]*\.[0-9]+)$/) exit(1)
		}
		print "PAPERX=" xy[1] "; PAPERY=" xy[2]
	}'
	eval `$JGAWK "$AWKSCR1" "$PAPER" || echo false` || unsup;;
esac
case "$LND" in
t)	PAPERTMP="$PAPERX"; PAPERX="$PAPERY"; PAPERY="$PAPERTMP"
	DVI2PSLND=landscape; DVIPDFMLND=-l;;
*)	DVI2PSLND=; DVIPDFMLND=;;
esac  # $DVI2PSLND$DVIPDFMLND$PAPERȰˤȤ

AWKSCR2='BEGIN{
	# 1ʸ5.25pt ⤵7.77pt 2.22pt򸫹
	print int((ARGV[1] - (2.54+1)) * 72/2.54 / 5.25)
}'
LEN=`$JGAWK "$AWKSCR2" $PAPERX` # 1ʸ

TABCOL=8
BASELINESKIP=3.8mm # ߤΤȤa4ʳǤ66Ԥˤʤʤ

# ¾ΥޥɤθƤӽФȽ
if exists platex; then
	JLATEX='platex -kanji=euc'
else
	JLATEX='jlatex -code=euc'
	if : | $JLATEX 2>&1 | fgrep -i 'unrecognized option' >/dev/null; then
		JLATEX='jlatex -kanji=euc'
	fi
fi
if exists dvips; then
	DVIPS="dvips -f -T ${PAPERX}cm,${PAPERY}cm"
else
	case "$PAPER" in *,*) unsup;; esac # dvi2psǤեˤ¿ʬб
	DVIPS="dvi2ps -o $PAPER$DVI2PSLND"
fi
if exists dvipdfmx; then
	DVIPDF="dvipdfmx -p $PAPER $DVIPDFMLND"; GRPOPT='[dvipdfmx]'
elif exists dvipdfm; then
	DVIPDF="dvipdfm -p $PAPER $DVIPDFMLND"; GRPOPT='[dvipdfm]'
else
	mydvipdf(){
		$DVIPS "$1" | ps2pdf - "$1.pdf"
	} # dvipdfˤdvipsʳǤλ極λʬäƤʤ
	DVIPDF=mydvipdf; GRPOPT=
fi
if exists yakc; then
	KCONV='yakc -k2,f2 --'
	KCONV_FNM='yakc -k2 --'
else
	KCONV='nkf -e --'
	KCONV_FNM='nkf -e --'
fi # 1Хȥʤ2ХȲKCONVʳǹԤ

if [ X`echo '\n'` = X ]; then
	echo(){ # solarisshecho\פᤷƤޤΤɤκ
		$JGAWK 'BEGIN{
			for(i = 1; i < ARGC; i++){
				printf "%s%s", ARGV[i], i==ARGC-1 ? "\n" : " "
			}
		}' "$@"
	}
fi

VALIDLIST=' -~---Т-----ڣ---'
VALIDLIST="$VALIDLIST"'--ا----С-'

AWKSCR_CNV='BEGIN{ # LANG=C
	for(c = 128; c < 256; c++) c2i[sprintf("%c", c)] = c
	str = ARGV[1]
	while(str != ""){
		ch = substr(str, 1, str ~ /^[\0-\177]/ ? 1 : 2)
		str = substr(str, 1 + length(ch))
		if(str !~ /^-/){
			out = out ch
		} else { # ϰϤ1ХƱΤ뤤2ХƱΤ
			ch2 = substr(str, 2, length(ch))
			str = substr(str, 2 + length(ch))
			if(length(ch) == 1){
				out = out ch "-" ch2; continue
			}
			c = c2i[substr(ch, 1, 1)]; d = c2i[substr(ch, 2)]
			c1 = c2i[substr(ch2, 1, 1)]; d1 = c2i[substr(ch2, 2)]
			while(c < c1 || c == c1 && d <= d1){
				out = out sprintf("%c%c", c, d)
				d++; if(d == 255){c++; d = 161}
			} 
		}
	}
	print out
}'
case "$GAWK_NON_EUC_ORDER" in
t)	# λgawkɬ¸ߤ
	VALIDLIST="`LC_ALL=C gawk \"$AWKSCR_CNV\" \"$VALIDLIST\"`";;
esac

gen_latinmac(){
	cat <<'EOF'
	\relax
	!`
	\tctt\char162
	\tctt\char163
	\tctt\char164
	\ooalign{\hfill{}Y\hfill\crcr\hfill=\hfill}
	\tctt\char166
	\tctt\char167
	\"{}
	\tctt\char169
	\tctt\char170
	\tctt\char60\kern-.35em\char60
	\tctt\char172
	\tctt\char22
	\tctt\char174
	\tctt\char175
	
	\tctt\char176
	\tctt\char177
	\tctt\char178
	\tctt\char179
	\'{}
	\tctt\char181
	\tctt\char182
	\tctt\char183
	\c{}
	\tctt\char185
	\tctt\char186
	\tctt\char62\kern-.35em\char62
	\tctt\char188
	\tctt\char189
	\tctt\char190
	?`
	
	\`A
	\'A
	\^A
	\~A
	\"A
	\AA
	\AE
	\c{C}
	\`E
	\'E
	\^E
	\"E
	\`I
	\'I
	\^I
	\"I
	
	\ectt\char208
	\~N
	\`O
	\'O
	\^O
	\~O
	\"O
	\tctt\char214
	\O
	\`U
	\'U
	\^U
	\"U
	\'Y
	\ectt\char222
	\ss
	
	\`a
	\'a
	\^a
	\~a
	\"a
	\aa
	\ae
	\c{c}
	\`e
	\'e
	\^e
	\"e
	\`\i
	\'\i
	\^\i
	\"\i
	
	\ectt\char240
	\~n
	\`o
	\'o
	\^o
	\~o
	\"o
	\tctt\char246
	\o
	\`u
	\'u
	\^u
	\"u
	\'y
	\ectt\char254
	\"y
EOF
}
LATINMAC="`gen_latinmac`"
# pre_proc()post_proc()ޤǤ˱ƶꤷʤܸʸ
KREPL=''; CREPL='\1' # gawk-3.1.5Ǥ\0ư

AWKSCR_SUB='
	BEGIN{
		krepl = "'"$KREPL"'"; crepl = "'"$CREPL"'"
		# ֤ʸȤƻ

		# ksubst: ʸ֤ޥɽ
		# csubst: ʸ֤ʸɽ
		# 餬Ǥ뤳Ȥݾڤ뤿Υߡ
		# $KREPLѴʤ褦ˤ뤿ɻߺ
		ksubst[""]; csubst[krepl] = krepl

		# DOKSUBST: tʤ֤ pʤ֡ݡפ֤
		if("'$DOKSUBST'" != "f"){
			# gsǤȾѤˤ뤳¿ PDFǤ
			ksubst[""] = "\\rm\\raisebox{0.104em}{$-$}"
		}
		if("'$DOKSUBST'" == "t"){
			# gsǤϽФʤȤ¿
			ksubst[""] = "\\rm\\raisebox{0.104em}{$\\sim$}"
			# IPAեȤǤ0x2026ΰ֤ˤΤǽФʤ
			ksubst[""] = "\\rm\\raisebox{.34zh}" \
				"{.\\kern.045zw.\\kern.045zw.}"
			# gsȶü˺˾ʤ뤳Ȥ¿
			ksubst[""] = "\\rm\\raisebox{0.364em}{$\\frown$}"
			# gs8ѤˤʤХ
			ksubst[""] = "\\rm\\raisebox{0.104em}" \
				"{$\\rightarrow$}"
			ksubst[""] = "\\rm\\raisebox{0.104em}{$\\leftarrow$}"
			ksubst[""] = "\\rm\\raisebox{0.104em}{$\\uparrow$}"
			ksubst[""] = "\\rm\\raisebox{0.104em}{$\\downarrow$}"
			makekeisens(ksubst)
		}
	}
	function makekeisens(ksubst,	tmpa, tmpb, i){
		# ޥksubst[]Ƥ
		split("5 10 9 12 6 3 11 13 14 7 15", tmpa)
		split("          ", tmpb)
		for(i = 1; i in tmpb; i++)
			ksubst[tmpb[i]] = makekeisen(tmpa[i], 0)
		split("          ", tmpb)
		for(i = 1; i in tmpb; i++)
			ksubst[tmpb[i]] = makekeisen(0, tmpa[i])
		split("1 10  8 5  4 10  2 5  10 5", tmpa)
		split("    ", tmpb)
		for(i = 1; i in tmpb; i++)
			ksubst[tmpb[i]] = makekeisen(tmpa[i*2-1], tmpa[i*2])
		split("    ", tmpb)
		for(i = 1; i in tmpb; i++)
			ksubst[tmpb[i]] = makekeisen(tmpa[i*2], tmpa[i*2-1])
	}
	function makekeisen(thin, thick){
		# makekeisen(, ǻ)ǥޥä֤
		# 濴鱦, , , ˱Ӥ1, 2, 4, 8Ȥƻ
		return sprintf("\\rm\\raisebox{-.14zh}{\\unitlength1zw" \
			"\\begin{picture}(1,1)(-.5,-.5)%s%s\\end{picture}}",
			makekeiparts(thin, .042), makekeiparts(thick, .1))
		# makekeiparts2ñzw
	}
	function makekeiparts(n, lwidth,	ret, i, x, y, llen, tmp){
		# makekeisen()β (-.5,-.5)(.5,.5)˷
		llen = .5; if(n+0 !~ /^[1248]$/) llen += lwidth/2
		# ҤʣפϥɤνΤĹȾʬФ
		x = .5; y = 0
		for(i = 1; i <= 4; i++){
			if(n % 2) ret = ret sprintf("\\put(%s,%s){" \
				"\\makebox(0,0)[%s]{\\rule{%szw}{%szw}}}",
				x, y, substr("rtlb", i, 1), llen, lwidth)
			tmp = x; x = -y; y = tmp
			tmp = llen; llen = lwidth; lwidth = tmp
			n = int(n/2)
		}
		return ret
	}
'
AWKSCR_AUX="$AWKSCR_SUB"'
	BEGIN{
		for(c in ksubst) printf "%s", c
		for(c in csubst) printf "%s", c
	}
'
KSUBSTLIST="`$JGAWK \"$AWKSCR_AUX\"`"
AWKSCR="$AWKSCR_SUB"'
	BEGIN{
		len = '$LEN'
		set_subst()

		if("'$HDR'" == "t"){
			centerhdr = ENVIRON["FNM"]
			if(centerhdr != ""){
				print "\\setbox\\fnmbox=\\hbox{%"
				centerhdr = dectrl(centerhdr)
				# dectrl() can be used only after set_subst()
				gsub(/[^'"$VALIDLIST"']/, "", centerhdr)
				gsub(/-/, "-{\\tt -}\\jverb-", centerhdr)
				print " \\jverb-" centerhdr "-}%"
			} else {
				print "\\setbox\\fnmbox=\\hbox{stdin}%"
			}
		}
	}
	function set_subst(	lmac){ # Хksubst[], csubst[]ɲ
		split(ENVIRON["LATINMAC"], lmac)
		for(i in lmac)
			ksubst[sprintf("\033N%c", i + 31)] = lmac[i]
		for(i = 0; i < 32; i++)
			csubst[sprintf("%c", i)] = sprintf("^%c", i + 64)
		csubst["\177"] = "^?"
	}
	function dectrl(s,	l, c){
		while(match(s, /[\0-\037\177]/)){
			l = l substr(s, 1, RSTART - 1) \
				csubst[substr(s, RSTART, 1)]
			s = substr(s, RSTART + 1)
		}
		return l s
	}

	NR == 1{
		print "\\begin{jverbatim}"
	}
	{
		gsub(/\f/, "") # ʬδ

		sub(/[ \t]+$/, "")
	}
	/^$/{
		emptylines++; next
	}
	{
		for(; emptylines; emptylines--) print ""

		# (ISO-2022-JP-2)ISO8859-1(ISO8859-7θ)
		# ȥʸδޡ֤
		$0 = pre_proc($0, lch_ary)

		$0 = detab($0); gsub(/[^'"$CREPL$VALIDLIST"']/, "")
		$0 = mid_proc($0, lch_ary)
		$0 = fold($0, len)

		gsub(/\\end{jverbatim}/, "\\end{jver\n" \
			"\\end{jverbatim}%\n\\begin{jverbatim}\nbatim}")

		# ޡ򤽤ʸɽ֤
		$0 = post_proc($0, lch_ary)

		print
	}

	END{
		if(NR) print "\\end{jverbatim}"
	}

	function pre_proc(s, r,		l, i, c){
		delete r
		while(match(s,
		/\033\.A|\033N[ -\177]|[\0-\010\012-\037\177'"$KSUBSTLIST"']/)){
			l = l substr(s, 1, RSTART - 1)
			c = substr(s, RSTART, RLENGTH)
			s = substr(s, RSTART + RLENGTH)
			if(c != "\033.A"){
				r[++i] = c
				l = l (c ~ /^[\0-\177]/ ? crepl : krepl)
				# Ⱦȹͤʸ(ƥʸ
				# ȥʸ)crepl
				# ʸ(δ)kreplִ
			}
		}
		return l s
	}
	function mid_proc(s, r,		l, i, c){
		while(match(s, /['"$CREPL"']/)){
			l = l substr(s, 1, RSTART - 1)
			c = r[++i]
			s = substr(s, RSTART + RLENGTH)

			l = l (length(c) == 1 ? krepl : crepl)
			# detabfoldȾʸΤȥʸ
			# ºݤΰˤʤΤǤkreplִ
		}
		return l s
	}
	function post_proc(s, r,	l, i, c, m){
		while(match(s, /['"$CREPL$KREPL"']/)){
			l = l substr(s, 1, RSTART - 1)
			c = r[++i]
			m = substr(s, RSTART, RLENGTH)
			s = substr(s, RSTART + RLENGTH)
			if(c in csubst){
				l = l csubst[c]
			} else {
				l = l "\0{\\tt\\makebox[" (m ~ /^[\0-\177]/ ? \
					.5 : 1) "em]{" ksubst[c] "}}%\n\1"
			}
		}
		l = l s # /\1$/֤sϢܤɬפ
		gsub(/\1\0/, "", l)
		gsub(/\n\0/, "\n\2", l); sub(/^\0/, "\2", l)
		gsub(/\1\n/, "\3\n", l); sub(/\1$/, "\3", l)

		gsub(/\0/, "\\end{jverbatim}%\n", l)
		gsub(/\2/, " \\end{jverbatim}{\\tt\\kern-.5em}%\n", l)
		gsub(/\1/, "%\n\\begin{jverbatim}\n", l)
		gsub(/\3/, "%\n\\mbox{}\\par\\begin{jverbatim}", l)
		return l
	}
	function detab(s,	l, m, n, p){
		while(match(s, /\t+/)){
			p = substr(s, 1, RSTART - 1); n += blength(p)
			# nϥľޤǤ Ȥ֤Ÿm׻
			m = RLENGTH * '$TABCOL' - n % '$TABCOL'
			l = l p sprintf("%" m "s", ""); n += m
			s = substr(s, RSTART + RLENGTH)
		}
		return l s
	}
	function fold(s, len,	    h, l){
		while(blength(s) > len){
			h = jbstrhead(s, len)
			l = l h "\n"; s = substr(s, length(h) + 1)
		}
		return l s
	}

	function blength(s,	l, i){ # ХȤǿlength
	 # substrʸñ̤ǤʤlengthѲ
		if('$SUBSTR_BYTEWISE') return length(s)
	 # substrʸñ̤ξ硢ʤΤ1ʸĿ
		for(i = l = length(s); i; i--)
			if(substr(s, i, 1) !~ /^[\0-\177]/) l++
		return l
	}
	function jbstrhead(s, l,	t, ss, l2){ # fold()ǤΤ߻
	 # sκlХȤ2Хʸ˸٤ʤ褦ڤФ
		if('$SUBSTR_BYTEWISE' == 0){ # substrʸñ̤ξ
			while(l > 1){ # 礦ʤ龯ڤФƤ
				l2 = int(l/2)
				ss = substr(s, 1, l2); s = substr(s, l2 + 1)
				l2 = blength(ss); if(!l2) return t
				t = t ss; l -= l2
			}
			return l && s ~ /^[\0-\177]/ ? t substr(s, 1, 1) : t
		} else if('$SUBSTR_BYTEWISE' == 1){ # gawk+mbξ
			return substr(s, 1, l) # ñʤƬsubstrѲ
		} else { # $SUBSTR_BYTEWISE == 2, substr˥Хñ
			t = substr(s, 1, ++l)
			if(length(t) == l)
			 # Ǹ夬Դ2Хʸξ serowgawkϤ
			 # subǼ뤬gawk3.1.4ޤǤϤǤʤ
				sub(/.$/, "", t) || t = substr(t, 1, --l)
			return t	# serowjgawkǤ⤳¿ʬOK
		}
	}
'

TMPDIR=/tmp/.t2ps$$_"`date '+%s' 2>/dev/null`"
trap '' 1 2 3 13 15
(umask 077 && exec mkdir "$TMPDIR") || exit $?
trap 'STATUS=$?; cd /tmp; rm -rf "$TMPDIR"; trap 0; exit $STATUS' 0 1 2 3 13 15

{
	echo '
		\ifx\LoadClass\undefined
		 \ifx\gtfam\undefined\ifx\gtfamily\undefined
		 \documentstyle[jverb]{j-article}
		 \else\documentstyle[jverb]{jarticle}\fi
		 \else\documentstyle[jverb]{jarticle}\fi
		\else
		 \ifx\gtfam\undefined\ifx\gtfamily\undefined
		 \documentclass{j-article}
		 \else\documentclass{jarticle}\fi
		 \else\documentclass{jarticle}\fi
		 \usepackage'"$GRPOPT"'{graphics}
		 \usepackage{jverb}
		\fi

		\paperwidth'$PAPERX'cm\paperheight'$PAPERY'cm
		\makeatletter
		% ޡ1in.2ܤξ
		\@tempdima'$PAPERX'cm
		\textwidth\@tempdima \advance\textwidth-2in
		\ifdim\textwidth<.6\@tempdima \textwidth.6\@tempdima\fi
		\oddsidemargin\@tempdima \advance\oddsidemargin-\textwidth
		\oddsidemargin.5\oddsidemargin \advance\oddsidemargin-1in
		% A43.8mm66(+1pt)줿ξ岼;׹⤵\@tempdimb
		% ׻(¾ѻξˤ⤽ͤȤ)
		\@tempdimb'$BASELINESKIP'\@tempdimb-66\@tempdimb
		\advance\@tempdimb-1pt\advance\@tempdimb29.7cm
		\@tempdima'$PAPERY'cm
		% ĥޡ\@tempdimbȾʬ⤵.2ܤξ
		\textheight\@tempdima\advance\textheight-\@tempdimb
		\ifdim\textheight<.6\@tempdima \textheight.6\@tempdima\fi
		\topmargin\@tempdima \advance\topmargin-\textheight
		\topmargin.5\topmargin \advance\topmargin-1in
		\makeatother

		\pagestyle{empty}
		\headheight12pt\headsep12pt
		\advance\topmargin-\headheight
		\advance\topmargin-\headsep
	'
	[ $HDR = t ] && echo '
		\advance\topmargin.5\headheight
		\advance\topmargin.5\headsep
		\newbox\fnmbox\newbox\hdrboxl\newbox\hdrboxr\newbox\hdrbox
		\setbox\hdrboxl=\hbox{'"`LC_ALL=C date '+%b %d %Y %H:%M'
			`"'\ \ }
		\makeatletter \def\@oddhead{%
			\setbox\hdrboxr=\hbox{\ \ Page \thepage}%
			\setbox\hdrbox=\hbox{\usebox\hdrboxl\usebox\fnmbox
				\usebox\hdrboxr}%
			\ifdim\the\wd\hdrbox>\textwidth
				\ifx\resizebox\undefined \hss\usebox\hdrbox\hss
				\else \rotatebox{0}{\resizebox{\textwidth}{!}{%
					\usebox\hdrbox}}\fi
			\else \usebox\hdrboxl\hfill \usebox\fnmbox\hfill
				\usebox\hdrboxr \fi}\makeatother
	' # dateμ¹Ԥ1
	echo '
		\def\tctt{\font\tentctt=tctt10\relax\tentctt}
		\def\ectt{\font\tenectt=ectt10\relax\tenectt}
		\begin{document}
		\baselineskip'$BASELINESKIP'
		\jvtabstop='$TABCOL'
	'
	FIRST=t
	for i in $DEFARG "$@"; do
		if [ $HDR = f ]; then
			FNM=	# $HDRfξϻȤʤ
		elif [ X"$LABEL" != X ]; then # LABELǤʤȤ
			FNM="$LABEL"
		elif [ X"$i" = X- ]; then
			FNM=	# ξޥΤΡstdinפ
		else
			FNM="`echo \"$i\" | $KCONV_FNM`"
		fi	

		[ $FIRST = f ] && echo '
			\newpage\setcounter{page}{1}%
		'
		FIRST=f
		($KCONV "$i" || touch "$TMPDIR"/tmp.err) |
			FNM="$FNM" LATINMAC="$LATINMAC" $JGAWK "$AWKSCR"
		# $LATINMAC-vϤawkˡ\פᤵƤޤ
	done
	echo '\end{document}'
} | { # trap򤳤ǹԤϡ{ }פǤʤ( )פǤʤȸʤ
	case "$TOFMT" in
	tex)
		cat;;
	*)
		cd "$TMPDIR" || exit $?
		{
			cat > tmp.tex && $JLATEX tmp </dev/null >&2 &&
			case "$TOFMT" in
			pdf)
				if [ -f tmp.dvi ]; then
					$DVIPDF tmp >&2 && cat tmp.pdf
				elif [ ! -f tmp.err ]; then
					# ʤΤ̵1ǤPDF
					{
						echo '%!PS'
						echo 'showpage'
					} | ps2pdf -
				fi
			;;
			*)
				if [ -f tmp.dvi ]; then
					$DVIPS tmp
				elif [ ! -f tmp.err ]; then
					echo '%!PS'
				fi # solarisshtestˤ-eʤ
			;;
			esac
		} 2> tmp.msg || {
			ST=$?; cat tmp.msg >&2; exit $ST
		}
	;;
	esac && [ ! -f "$TMPDIR"/tmp.err ]
}
