#!/bin/ksh93

progname=${.sh.file##*/}

[[ ${progname} == 'allec' ]] && encoding='T1' || encoding='OT1'
[[ $1 == '-r' ]] && { DVIPS='dvired'; shift ; } || DVIPS='dvips'

tmpdir=${ mktemp -t --directory ${progname}.XXXXXX ; }
[[ -z ${tmpdir} ]] && print -u2 'Unable to create temp dir - exiting.' && exit 1
trap "rm -rf ${tmpdir}" EXIT
cd ${tmpdir} || exit 1

function printBody {
	TXT='
	\pagestyle{empty}
	\parindent0in
	\hfuzz=\maxdimen
	\hbadness=10000
	\textheight9.5in
	\textwidth6.5in
	\newcommand{\myformula}{\sum a_{b_{c_d}} = c}
	\newcommand{\mytext}{text $\mathcal{\myformula}\mathrm{\myformula}
	\mathbf{\myformula}\mathsf{\myformula}\mathtt{\myformula}
	\mathnormal{\myformula}\mathit{\myformula}$}

	\newcommand{\TestSizes}{{%
	\tiny \mytext\scriptsize \mytext\footnotesize \mytext\small \mytext
	\normalsize \mytext
	\large \mytext\Large \mytext\LARGE \mytext\huge \mytext\Huge \mytext}}
	\newcommand{\TestRM}{rm-family: {\rmfamily\TestSizes}\newline}
	\newcommand{\TestSF}{sf-family: {\sffamily\TestSizes}\newline}
	\newcommand{\TestTT}{tt-family: {\ttfamily\TestSizes}\newline}
	\newcommand{\TestFamilies}{\TestRM\TestSF\TestTT\newline}
	\newcommand{\TestMD}{md-series: {\mdseries\TestFamilies}}
	\newcommand{\TestBF}{bf-series: {\bfseries\TestFamilies}}
	\newcommand{\TestSeries}{\TestBF\TestMD\par}
	\newcommand{\TestUP}{up-shape: {\upshape\TestSeries}\par}
	\newcommand{\TestIT}{it-shape: {\itshape\TestSeries}\par}
	\newcommand{\TestSL}{sl-shape: {\slshape\TestSeries}\par}
	\newcommand{\TestSC}{sc-shape: {\scshape\TestSeries}\par}
	\newcommand{\TestShapes}{\TestUP\TestIT\TestSL\TestSC}
	\begin{document}
	\TestShapes
	\end{document}
'
	print "${TXT//\\/\\\\}"
}

function printHead {
	print "\\documentclass['$1'pt]{article}" \
		"\n\\usepackage['${encoding}']{fontenc}"
}

print -u2 '
---------------------------------------------------------------------
>>>>>>>>>>>  Generating testfiles for 10pt, 11pt and 12pt. <<<<<<<<<<
---------------------------------------------------------------------'
printHead 10 > allcm10.tex
printHead 11 > allcm11.tex
printHead 12 > allcm12.tex
printBody >> allcm10.tex
printBody >> allcm11.tex
printBody >> allcm12.tex

print -u2 '
---------------------------------------------------------------------
>>>>>>>>>>>  Calling latex...                              <<<<<<<<<<
---------------------------------------------------------------------'
latex allcm10 >/dev/null
latex allcm11 >/dev/null
latex allcm12 >/dev/null

print -u2 '
---------------------------------------------------------------------
>>>>>>>>>>>  Now, calling $DVIPS to make missing fonts...  <<<<<<<<<<
---------------------------------------------------------------------'
${DVIPS} "$@" -V -f allcm10 > /dev/null
${DVIPS} "$@" -V -f allcm11 > /dev/null
${DVIPS} "$@" -V -f allcm12 > /dev/null

cd ${OLDPWD}
