#! /bin/bash
# Last edited on 2020-12-15 09:50:52 by jstolfi

# Typesets standard input (a plain ISO Latin-1 file)
# creating NAME.ps. Outputs errors to NAME.err.
# Designed to be used by iso-to-ps-hack

usage="$0 NAME [ -landscape | -portrait ] [ -8pt|-9pt|-10pt|-11pt|-12pt|-14pt|-17pt|-20pt ] [ -twocolumn ] < INFILE.iso > OUTFILE.ps"

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

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

echo "args: $*" 1>&2

name="$1"; shift
err="${name}.err"
/bin/rm -f ${err} "${name}.ps"

while [[ $# -gt 0 ]]; do
  if [[ "/$1" == "/-landscape" ]]; then
    latexaspect="\textwidth=10in \textheight=6.5in"
    dvipsaspect="-t landscape"
    margin="-0.75in"
    shift
  elif [[ "/$1" == "/-portrait" ]]; then
    latexaspect="\textwidth=7.5in \textheight=9in"
    dvipsaspect=""
    margin="-0.5in"
    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="${name##*/}" # File name minus directory
dname="${name%/*}" # Directory name (or ${name} if there is no directory)
if [[ "/${name}" != "/${fname}" ]]; then
  cd ${dname}
fi

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

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

\usepackage[portuges,brazil]{babel}
\usepackage[latin1]{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 '1,$s/^ *$/   /g' \
      -e 's/º/o/g' \
      -e 's/ª/a/g' \
      -e 's/×/x/g' \
  >> ${fname}.iso

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

echo "running latex" 1>&2

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

/bin/touch ${fname}.aux
latex ${fname}.iso >> ${fname}.lg2
if [[ -s ${fname}.dvi ]]; then
  dvips -D600 -f ${dvipsaspect} -o ${fname}.ps < ${fname}.dvi >> ${fname}.lg3
  if [[ -s ${fname}.ps ]]; then
    echo "postscript generated ${fname}.ps" 1>&2
  else
    cat ${fname}.lg3 >> ${err}
  fi
else
  cat ${fname}.lg2 >> ${err}
fi

/bin/rm -f ${fname}.{dvi,aux,log,lg2,lg3}

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