#! /bin/bash

# Typesets standard input (a plain ISO Latin-1 or UTF-8 file)
# creating TMPNAME.pdf. Outputs errors to TMPNAME.err.
# Designed to be used by iso-to-pdf-hack

usage="$0 TMPNAME [ -landscape | -portrait ] [-iso | -iso-latin-1 | -utf8 ] [ -8pt|-9pt|-10pt|-11pt|-12pt|-14pt|-17pt|-20pt ] [ -twocolumn ] < INFILE.txt > OUTFILE.pdf"

latexaspect="\textwidth=7.5in \textheight=9in"
dvipdfaspect=""
margin="-0.5in"
ptsize=""
clops=""
clsep=""
dclass="article"
enc="utf8"

if [[ $# -lt 1 ]]; then
  echo "usage: ${usage}" 1>&2
  exit 1
fi

echo "args: $*" 1>&2

tmpname="$1"; shift
err="${tmpname}.err"
/bin/rm -f ${err} "${tmpname}.pdf"

while [[ $# -gt 0 ]]; do
  if [[ "/$1" == "/-landscape" ]]; then
    latexaspect="\textwidth=10in \textheight=6.5in"
    dvipdfaspect="-t landscape"
    margin="+0.5in"
    shift
  elif [[ "/$1" == "/-portrait" ]]; then
    latexaspect="\textwidth=7.5in \textheight=9in"
    dvipdfaspect=""
    margin="-0.5in"
    shift
  elif [[ ( "/$1" == "/-iso" ) || ( "/$1" == "/-iso-latin-1" ) ]]; then
    enc="latin1"
    shift
  elif [[ "/$1" == "/-utf8" ]]; then
    enc="utf8"
    shift
  elif [[ "/$1" == "/-8pt" ]]; then
    ptsize="8pt"; dclass="extarticle"; shift;
  elif [[ "/$1" == "/-9pt" ]]; then
    ptsize="9pt"; dclass="extarticle"; shift;
  elif [[ "/$1" == "/-10pt" ]]; then
    ptsize=""; shift
  elif [[ "/$1" == "/-11pt" ]]; then
    ptsize="11pt"; shift
  elif [[ "/$1" == "/-12pt" ]]; then
    ptsize="12pt"; shift;
  elif [[ "/$1" == "/-14pt" ]]; then
    ptsize="14pt"; dclass="extarticle"; shift;
  elif [[ "/$1" == "/-17pt" ]]; then
    ptsize="17pt"; dclass="extarticle"; shift;
  elif [[ "/$1" == "/-20pt" ]]; then
    ptsize="20pt"; dclass="extarticle"; shift;
  elif [[ ( "/$1" == "/-twocolumn" ) || ( "/$1" == "/-twocol" ) ]]; then
    clops="${clops}${clsep}twocolumn"; clsep=","; shift
  else
    echo "unrecognized option: $1" >> ${err}
    echo "usage: ${usage}" >> ${err}
    exit 1
  fi
done

echo "parsed options" 1>&2

if [[ "/${ptsize}" != "/" ]]; then 
  clops="${clops}${clsep}${ptsize}"; clsep=","
fi

if [[ $# -ne 0 ]]; then
  echo "usage: ${usage}" >> ${err}
fi

fname="${tmpname##*/}" # File name minus directory
dname="${tmpname%/*}" # Directory name (or ${tmpname} if there is no directory)
if [[ "/${tmpname}" != "/${fname}" ]]; then
  cd ${dname}
fi

echo "now at `pwd`" 1>&2

/bin/cat  <<EOF > ${fname}.tex
\nonstopmode
\documentclass[${clops}]{${dclass}}

\usepackage[portuges,brazil]{babel}
\usepackage[${enc}]{inputenc}

\topmargin  -0.5in
\oddsidemargin  ${margin}
\evensidemargin ${margin}
\marginparwidth 10pt
\marginparsep 0pt
${latexaspect}

\begin{document}

\small\baselineskip ${ptsize}
% The character '¤' maps to \textcurrency, undefined in OT1:
\def\textcurrency{\hbox to 1 em{o\hss x}}
\begin{verbatim}
EOF

/bin/cat \
  | sed \
      -e '1,$s//\\end{verbatim}'"\n"'\\newpage'"\n"'\\begin{verbatim}'"\n"'/g' \
      -e 's/º/o/g' \
      -e 's/ª/a/g' \
      -e 's/×/x/g' \
  >> ${fname}.tex

/bin/cat <<EOF >> ${fname}.tex 
\end{verbatim}
\end{document}
EOF

echo "running latex" 1>&2

export TEXINPUTS=".:${STOLFIHOME}/tex/inputs::"

/bin/touch ${fname}.aux
/bin/rm -f ${fname}.pdf
pdflatex -file-line-error ${fname} >> ${fname}.lg2
if [[ -s ${fname}.pdf ]]; then
  echo "PDF generated ${fname}.pdf" 1>&2
else
  cat ${fname}.lg2 >> ${err}
fi

/bin/rm -f ${fname}.{aux,log,lg2}
ls -l ${fname}.*

if [[ -s ${err} ]]; then
  exit 1
else
  exit 0
fi
