#! /bin/bash
# Author: Chris Karakas <chris@karakas-online.de>
#
# /usr/local/bin/lyxtox
#
# Script for creating HTML, PDF, RTF, TXT and PS output 
# from a single SGML source.
# Needs: runsed, sedscr, jadetex.cfg, print.dsl
# (and of course TeX, LaTeX, openjade, pdfjadetex,
# DTD, stylesheets...)
# See http://www.karakas-online.de/mySGML/t1.html for a detailed 
# description.
#
# Usage: "lyxtox FILENAME_WITHOUT_.sgml_ENDING"

# Copyright (c) 2002, Chris Karakas <chris@karakas-online.de> 
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; see the file COPYING.  If not, write to
# the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
#

# Help function
#
function help() {
cat <<-EOF
Usage: `basename $0` [-h] [FILENAME_WITHOUT_.lyx_ENDING]
Creates HTML, PDF, RTF, TXT and PS output 
from a single LYX source.
Needs: lyx, runsed, sed, sedscr, jadetex.cfg, perl, openjade,
pdfjadetex, DocBook, TeX, LaTeX, thumbpdf, gzip, tar 
and all packages required from these.
See http://www.karakas-online.de/mySGML/ for a detailed 
description.

EXAMPLE
=======

If your file is myfile.lyx, then do

lyxtox myfile

go get a cup of coffee and be happy :-)


-h, --help    Display this help text
EOF
}

# Check arguments and issue a help statement, if wrong
#
if [ $# -eq 0 -o $# -gt 1 ]; then
  help
  exit 1
elif [ $1 = "-h" -o $1 = "--help" ]; then
  help
  exit 0
fi


# Program locations

# Adapt to your situation.
LYX="/usr/X11R6/bin/lyx"
LYNX="/usr/bin/lynx"
SED="/usr/bin/sed"
AWK="/usr/bin/awk"
RUNSED="/usr/local/bin/runsed"
SEDSCR="sedscr"
SEDSCRMATH="sedscr_math"
SEDSCRABI="sedscr_abi"
SEDSCRAPP="sedscr_app"
SEDSCRBIB="sedscr_bib"
SEDSCRTIDY="sedscr_tidy"
SEDSCRTIDY2="sedscr_tidy2"
SEDSCRIMA="sedscr_ima"
SEDSCRIMG="sedscr_img"
SEDSCRGRA="sedscr_gra"
SEDSCRAPA="sedscr_apa"
AWKSCRMATH="awkscr_math"
AWKSCR_REFDB_HTML="awkscr_refdb_html"
AWKSCR_REFDB_PRINT="awkscr_refdb_print"
PERL="/usr/bin/perl"
COLLATEINDEX="/usr/share/sgml/docbook/docbook-dsssl-stylesheets/bin/collateindex.pl"
UNESCAPEMATH="/usr/local/bin/unescape_math.pl"
TEXMATH2PNGBMP="/usr/local/bin/texmath2pngbmp.pl"
THUMB_PDF="/usr/bin/thumbpdf"
COOLTHUMBS="/usr/local/bin/coolthumbs"
OPENJADE="/usr/bin/openjade -E 1000"
PDFJADETEX="/usr/bin/pdfjadetex"
JADETEX="/usr/bin/jadetex"
DVIPS="/usr/bin/dvips"
GZIP="/usr/bin/gzip"
TAR="/bin/tar"
TIDY="/usr/bin/tidy"
SGMLNORM="/usr/bin/sgmlnorm"
HTMLSPLIT="/usr/local/bin/htmlsplit.awk"
REFDBXP="/usr/bin/refdbxp"
RUNBIB="/usr/bin/runbib"
DATE="/bin/date"
DATADIR="../"
DOMAIN="www.karakas-online.de"
TODAY=`LANG="en_US" $DATE`

# Shall we use the coolthumbs script to create the PDF thumbnails?
# You can get coolthumbs from
# http://www.ringlord.com/publications/latex-pdf-howto/
# Note that you will also need to have GIMP installed
# and that you will have to edit some lines in coolthumbs too.
use_coolthumbs="1"

# Put preliminary steps that are specific to the file being processed
# in $1.start. $1 is the argument you gave to this script.
# Thus, if you called this script with
#
# lyxtox myLyxfile
#
# then you can put any code to be executed at the start in
#
# myLyxfile.start
#
# The same holds for $1.end at the end of the script.
. $1.start

# DSSSL stylesheets locations.
if test "$process_RefDB" = "1"; then
  HTML_DSL=${PWD}"/refdb-html.dsl"
  HTML_CHUNKS_DSL="$HTML_DSL#html"
  HTML_NOCHUNKS_DSL="$HTML_DSL#onehtml"
  PRINT_DSL=${PWD}"/refdb-print.dsl"
  PRINT_PDF_DSL="$PRINT_DSL#print-pdf"
  PRINT_PS_DSL="$PRINT_DSL#print-ps"
  PRINT_RTF_DSL="$PRINT_DSL#print-rtf"
  PRINT_TXT_DSL="$PRINT_DSL#print-txt"
else
#   HTML_DSL="/usr/share/sgml/docbook/docbook-dsssl-stylesheets-1.72/html/ldp.dsl#html"
#   HTML_DSL="../newbiedoc-html-test.dsl"
#   HTML_NOCHUNKS_DSL="../newbiedoc-onehtml.dsl"
  HTML_CHUNKS_DSL=${PWD}"/lyxtox-html.dsl"
  HTML_NOCHUNKS_DSL=${PWD}"/lyxtox-onehtml.dsl"
  PRINT_PDF_DSL=${PWD}"/lyxtox-print-pdf.dsl"
  PRINT_PS_DSL=${PWD}"/lyxtox-print-ps.dsl"
  PRINT_RTF_DSL=${PWD}"/lyxtox-print-rtf.dsl"
  PRINT_TXT_DSL=${PWD}"/lyxtox-print-txt.dsl"
fi

# Environment variables

# openjade needs this!
# Both absolute and relative paths work!
# SGML_SEARCH_PATH="$PWD/images"
SGML_SEARCH_PATH="./images"
export SGML_SEARCH_PATH

# pdftex (and pdfjadetex) need this.
# For image files:
# For some reason which I don't fully understand, \includegraphics with
# pdftex uses the TEXPSHEADERS environment variable for the additional
# paths to search.
# Also: TEXPSHEADERS contains the search path where pdftex looks up 
# for font mapping file (pdftex.map) and encoding files (*.enc).
# In my system pdftex.map is located in /var/lib/texmf/dvips/config/,
# while the .enc files are under /usr/share/texmf/dvips/base/.
# TEXPSHEADERS=":${PWD}/images/:/var/lib/texmf/dvips/config/:/usr/share/texmf/dvips/base/"
TEXPSHEADERS=":${PWD}/images//"
export TEXPSHEADERS
#echo "TEXPSHEADERS=$TEXPSHEADERS"

# LaTeX & Co. need this!
# A relative path does NOT work!
# The double slash enables searching in subdirectories as well.
# TEXINPUTS="$PWD/images:$TEXINPUTS"
TEXINPUTS=":${PWD}/images//"
export TEXINPUTS
#echo "TEXINPUTS=$TEXINPUTS"

TEXMFCNF="/etc/texmf/"
export TEXMFCNF

# You can pass options to thumbpdf through this environment variable
THUMBPDF="--resolution 9.1"
export THUMBPDF

# openjade needs this! It is just the content of
# the /etc/sgml/catalog file. Please modify accordingly.
#
# NOTE: You must uncomment ONLY the ones you need!
# You most probably need CATALOG.iso_ent, CATALOG.docbook-dsssl-stylesheets.
# You certainly need openjade/catalog, refdb.cat.
# You probably need CATALOG.docbook_4, since newer versions of LyX
# export to DocBook 4.x, otherwise you probably need CATALOG.docbook_3.
# Choose wisely! 
# 
# This was taken from the catalog file of SuSE 7.3:
# SGML_CATALOG_FILES="/usr/share/sgml/CATALOG.iso_ent"
# SGML_CATALOG_FILES="$SGML_CATALOG_FILES:/usr/share/sgml/CATALOG.docbook-dsssl-stylesheets"
# SGML_CATALOG_FILES="$SGML_CATALOG_FILES:/usr/share/sgml/CATALOG.docbook_3"
# SGML_CATALOG_FILES="$SGML_CATALOG_FILES:/usr/share/sgml/CATALOG.docbook_4"
# SGML_CATALOG_FILES="$SGML_CATALOG_FILES:/usr/share/sgml/openjade/catalog"
# SGML_CATALOG_FILES="$SGML_CATALOG_FILES:/usr/share/refdb/refdb.cat"
#
# This is taken from the catalog file of SuSE 9.0:
#
# SGML_CATALOG_FILES="/usr/share/sgml/CATALOG.opensp"
SGML_CATALOG_FILES="$SGML_CATALOG_FILES:/usr/share/sgml/CATALOG.iso_ent"
SGML_CATALOG_FILES="$SGML_CATALOG_FILES:/usr/share/sgml/CATALOG.docbook-dsssl-stylesheets"
# SGML_CATALOG_FILES="$SGML_CATALOG_FILES:/usr/share/sgml/CATALOG.html"
# SGML_CATALOG_FILES="$SGML_CATALOG_FILES:/usr/share/sgml/CATALOG.html-3.2"
# SGML_CATALOG_FILES="$SGML_CATALOG_FILES:/usr/share/sgml/CATALOG.html-4.0"
# SGML_CATALOG_FILES="$SGML_CATALOG_FILES:/usr/share/sgml/CATALOG.html-4.01"
SGML_CATALOG_FILES="$SGML_CATALOG_FILES:/usr/share/sgml/CATALOG.mathml-2.0"
# SGML_CATALOG_FILES="$SGML_CATALOG_FILES:/usr/share/sgml/CATALOG.svg-1.0"
SGML_CATALOG_FILES="$SGML_CATALOG_FILES:/usr/share/sgml/CATALOG.svg-1.1"
# SGML_CATALOG_FILES="$SGML_CATALOG_FILES:/usr/share/sgml/CATALOG.tei_4"
# SGML_CATALOG_FILES="$SGML_CATALOG_FILES:/usr/share/sgml/CATALOG.tei_4sgml"
# SGML_CATALOG_FILES="$SGML_CATALOG_FILES:/usr/share/sgml/CATALOG.docbook-xml-website"
# SGML_CATALOG_FILES="$SGML_CATALOG_FILES:/usr/share/sgml/susedtd/CATALOG"
# SGML_CATALOG_FILES="$SGML_CATALOG_FILES:/usr/share/sgml/CATALOG.docbook_3"
# SGML_CATALOG_FILES="$SGML_CATALOG_FILES:/usr/share/sgml/CATALOG.xmlcharent"
SGML_CATALOG_FILES="$SGML_CATALOG_FILES:/usr/share/sgml/CATALOG.docbook_4"
SGML_CATALOG_FILES="$SGML_CATALOG_FILES:/usr/share/sgml/openjade/catalog"
# SGML_CATALOG_FILES="$SGML_CATALOG_FILES:/usr/share/sgml/CATALOG.db41xml"
# SGML_CATALOG_FILES="$SGML_CATALOG_FILES:/usr/share/sgml/CATALOG.db42xml"
SGML_CATALOG_FILES="$SGML_CATALOG_FILES:/usr/share/refdb/refdb.cat"

export SGML_CATALOG_FILES

SP_ENCODING="ISO-8859-1"
export SP_ENCODING

# Main part

# Export the lyx document to DocBook SGML.
${LYX} -e docbook $1.lyx

# Export the Appendix to DocBook SGML.
if test -e appendix.lyx; then
  ${LYX} -e docbook appendix.lyx
fi

# Export the Bibliography to DocBook SGML.
if test -e bibliography.lyx; then
  ${LYX} -e docbook bibliography.lyx
fi

# Transform LyX' SGML to a more suitable coding.
# See the sedscr file for the substitutions that take place here.
# runsed will (who could have guessed...) run sed, applying
# sedscr as input and doing some housekeeping, such as creating 
# a .bak backup file.
${RUNSED} ${SEDSCR} $1.sgml
# Add the Appendix, Bibliography and Index entities at the end.
${RUNSED} ${SEDSCRABI}  $1.sgml

# Transform the Appendix' SGML code as above.
if test -e appendix.lyx; then
  ${RUNSED} ${SEDSCR} appendix.sgml
  ${RUNSED} ${SEDSCRAPP} appendix.sgml
fi

# Do the same for bibliography.sgml, if it was supplied 
# by the user.
if test -e bibliography.sgml; then
  ${RUNSED} ${SEDSCRBIB} bibliography.sgml
fi

# Process math SGML elements.
if test "$process_math" = "1"; then
  ${RUNSED} ${SEDSCRMATH} $1.sgml
  ${AWK} -f ${AWKSCRMATH} $1.sgml > $1.awk.sgml
  mv $1.awk.sgml $1.sgml
  ${AWK} -f ${AWKSCRMATH} appendix.sgml > appendix.awk.sgml
  mv appendix.awk.sgml appendix.sgml
fi


# Tidy up the SGML file.
# ${RUNSED} ${SEDSCRTIDY} $1.sgml
# ${RUNSED} ${SEDSCRTIDY2} $1.sgml

#
# $SGMLNORM -c /usr/share/sgml/CATALOG.iso_ent -c /usr/share/sgml/CATALOG.docbook-dsssl-stylesheets -c /usr/share/sgml/CATALOG.mathml-2.0 -c /usr/share/sgml/stylesheets/sgmltools/sgmltools.cat -c /usr/share/sgml/CATALOG.svg-1.1 -c /usr/share/sgml/CATALOG.docbook_4 -c /usr/share/sgml/openjade/catalog -c /usr/share/refdb/refdb.cat  -d -n $1.sgml > $1-norm.sgml


# Clean previous HTML files.
rm $1/*.html

# Clean previous image files.
rm -rf $1/images

# Clean rsync backup copies.
rm -rf $1/*~

# Clean PDF, PS and TXT versions.
rm $1/$1.pdf $1/$1.ps.gz $1/$1.txt

# Bibliography SGML file creation through RefDB.
if test "$process_RefDB" = "1"; then
  ${REFDBXP} -t db31 < $1.sgml > $1.full.sgml
  mv $1.full.sgml $1.sgml
  ${RUNBIB} -d ${RefDB_db} -S ${REFDB_style} -t db31 $1.sgml
#  mv $1.bib.sgml bibliography.sgml
 
  # Automatic generation of the refdb-html.dsl and refdb-print.dsl 
  # DSSSL stylesheets from the just created $REFDB_style .dsl file.
  # The $REFDB_style stylesheet was just created through the 
  # above commands.
  # The refdb-html.dsl and refdb-print.dsl are the HTML and print
  # DSSSL stylesheets for the case we want to use RefDB.
  # See the definition of HTML_DSL and PRINT_DSL above.
  ${AWKSCR_REFDB_HTML}  ${REFDB_style}dsl > $HTML_DSL
  ${AWKSCR_REFDB_PRINT} ${REFDB_style}dsl > $PRINT_DSL
fi

# The SGML file, $1.sgml, is done at this point.
# We use it to compute a sed script, ${SEDSCRIMG},  on the fly.
# This is done using the sed script ${SEDSCRIMA}.
# Yup, we use a sed script to create another sed script...
${SED} -n -f ${SEDSCRIMA} $1.sgml > ${SEDSCRIMG}
# We create a second sed script, ${SEDSCRGRA}, which we will use 
# in order to substitute "graphXXXX" with the real name of the graphic file in ${SEDSCRIMG}
${SED} -n -e '/<\!ENTITY/s/.*graph\([^ ]*\) "\([^>]*\)".*>/s\/graph\1\/\2\/g/p' $1.sgml > ${SEDSCRGRA}
# Substitute "graphXXXX" with the real name of the graphic file in ${SEDSCRIMG}.
# Yup, we use a sed script to change another sed script...
${RUNSED} ${SEDSCRGRA} ${SEDSCRIMG}
# Erase <acronym>, <productname> and <application> tags from 
# the alt and title texts in the sed script ${SEDSCRIMG}.
# This is done using the sed script ${SEDSCRAPA}.
# Yup, we use a sed script to change another sed script...
${RUNSED} ${SEDSCRAPA} ${SEDSCRIMG}
# Add the necessary sed commands for the alt and title texts of smilies.
echo 's/"\.\/images\/icon_smile\.png">/".\/images\/icon_smile.png" alt="smile" title="smile">/g' >> ${SEDSCRIMG}
echo 's/"\.\/images\/icon_wink\.png">/".\/images\/icon_wink.png" alt="wink" title="wink">/g' >> ${SEDSCRIMG}
echo 's/"\.\/images\/icon_cool\.png">/".\/images\/icon_cool.png" alt="cool" title="cool">/g' >> ${SEDSCRIMG}
echo 's/"\.\/images\/icon_eek\.png">/".\/images\/icon_eek.png" alt="shock" title="shock">/g' >> ${SEDSCRIMG}
echo 's/"\.\/images\/icon_frown\.png">/".\/images\/icon_frown.png" alt="frown" title="frown">/g' >> ${SEDSCRIMG}
rm ${SEDSCRIMG}.bak 
# Now we have computed a sed script, ${SEDSCRIMG}, that adds alt and title tags to the images
# in every HTML file that is applied on. We will use it in a moment...


# One HTML file
# =============
#
# Generate HTML no chunks.
echo "#######################     HTML no chunks     #######################"
# Index creation, HTML links (for HTML, page numbers are not meaningful).
${PERL} ${COLLATEINDEX} -N -o index.sgml
# Pass "-V nochunks" to create one HTML file.
${OPENJADE} -t sgml -d $HTML_NOCHUNKS_DSL -i output.print.png -V nochunks -V html-index $1.sgml > $1.html
${PERL} ${COLLATEINDEX} -g -o index.sgml HTML.index 

# Generate HTML - one HTML file.
${OPENJADE} -t sgml -d $HTML_NOCHUNKS_DSL -i output.print.png -V nochunks $1.sgml > $1.html

# Process the equation list file.
# This will produce PNG and BMP images of the equations 
# in the images/math directory. The names will be randomly
# chosen numbers between 10000 and 20000. (see awkscr_math for this).
if test "$process_math" = "1"; then
  ${TEXMATH2PNGBMP} equation-list.sgml
fi

# Tidy the HTML code.
${TIDY} -ascii -c -wrap 200 -f /dev/null -m $1.html


# Generate TXT
echo "############################     TXT     ############################"
${LYNX} -dump -nolist $1.html > $1.txt


# Correct header and footer.
# First, split the HTML document in title and body parts.
# The title will be put in title.tmp, the body in body.tmp
# and the META tags in meta.tmp
${HTMLSPLIT} < $1.html

# Second, put the right header and footer in the file.
HTMLFILE=$1.html
BASENAME=`basename $HTMLFILE`
cat ${DATADIR}/part1 > ${HTMLFILE}
cat title.tmp >> ${HTMLFILE}
echo '</title>' >> ${HTMLFILE}
cat meta.tmp >> ${HTMLFILE}
# Substitute the placeholders DOMAIN, DIRNAME, FILENAME
# in the header (part2) and footer (part3) with the current values.
#
# Header
${SED} -e "s/_DOMAIN_/${DOMAIN}/g" ${DATADIR}/part2 > part2_1.tmp
${SED} -e "s/_DIRNAME_/$1/g" part2_1.tmp > part2_2.tmp
${SED} -e "s/_FILENAME_/${BASENAME}/g" part2_2.tmp > part2_3.tmp
${SED} -e "s/_TITLE_/${TITLE}/g" part2_3.tmp > part2_4.tmp
${SED} -e "s/_FORMATSFILE_/${FORMATSFILE}/g" part2_4.tmp > part2_5.tmp
${SED} -e "s/_COPYRIGHT_/${COPYRIGHT}/g" part2_5.tmp > part2_6.tmp
${SED} -e "s/_HOMEFILE_/${HOMEFILE}/g" part2_6.tmp > part2_7.tmp
${SED} -e "s/_DATE_/${TODAY}/g" part2_7.tmp > part2.tmp
cat part2.tmp >> ${HTMLFILE}

# Body
cat body.tmp >> ${HTMLFILE}

# Footer
${SED} -e "s/_DOMAIN_/${DOMAIN}/g" ${DATADIR}/part3 > part3_1.tmp
${SED} -e "s/_DIRNAME_/$1/g" part3_1.tmp > part3_2.tmp
${SED} -e "s/_FILENAME_/${HTMLFILE}/g" part3_2.tmp > part3_3.tmp
${SED} -e "s/_TITLE_/${TITLE}/g" part3_3.tmp > part3_4.tmp
${SED} -e "s/_FORMATSFILE_/${FORMATSFILE}/g" part3_4.tmp > part3_5.tmp
${SED} -e "s/_COPYRIGHT_/${COPYRIGHT}/g" part3_5.tmp > part3_6.tmp
${SED} -e "s/_HOMEFILE_/${HOMEFILE}/g" part3_6.tmp > part3_7.tmp
${SED} -e "s/_DATE_/${TODAY}/g" part3_7.tmp > part3.tmp
cat part3.tmp >> ${HTMLFILE}

# Housekeeping
rm -f body.tmp title.tmp meta.tmp part2*.tmp part3*.tmp

# Add alt and title tags to the images.
${RUNSED} ${SEDSCRIMG} ${HTMLFILE}

# Many HTML files (chunks)
# ========================
#
# Generate HTML chunks
echo "########################     HTML chunks     ########################"

mkdir -p $1
cp $1.sgml $1/
cp $1.bib.sgml $1/

if test -e appendix.sgml; then
  cp appendix.sgml $1/
fi

if test -e appendix.lyx; then
  cp appendix.lyx $1/
fi

if test -e bibliography.sgml; then
  cp bibliography.sgml $1/
fi

if test -e bibliography.lyx; then
  cp bibliography.lyx $1/
fi

cd $1

# Index creation, HTML links.
# We have to redo the index because this time we need links to various HTML files,
# not to various parts of one HTML file.
rm index.sgml
rm HTML.index

${PERL} ${COLLATEINDEX} -N -o index.sgml
# Notice that we use a different stylesheet.
# Notice also the absence of the "-V nochunks" option for openjade.
# This time, the HTML.index will be created under $1, i.e. a directory with the 
# name of the first parameter to this script, together with  all the
# HTML "chunks".
$OPENJADE -t sgml -d $HTML_CHUNKS_DSL -i output.print.png -V html-index $1.sgml
$PERL $COLLATEINDEX -g -o index.sgml HTML.index

# Generate HTML - many HTML files (chunks)
$OPENJADE -t sgml -d $HTML_CHUNKS_DSL -i output.print.png $1.sgml

cd ../

# For *every* HTML file just created in the $1 directory, 
# tidy the code and correct header and footer.
# 
for HTMLFILE in `ls $1/*.html`; do
    BASENAME=`basename $HTMLFILE`
#   Tidy the HTML code
    ${TIDY} -ascii -c -wrap 200 -f /dev/null -m ${HTMLFILE}

#   Correct header and footer.
#   First, split the HTML document in title and body parts.
#   The title will be put in title.tmp, the body in body.tmp
#   and the META tags in meta.tmp
    ${HTMLSPLIT} < ${HTMLFILE}

#   Second, put the right header and footer in the file.
    cat ${DATADIR}/part1 > ${HTMLFILE}
    cat title.tmp >> ${HTMLFILE}
    echo '</title>' >> ${HTMLFILE}
    cat meta.tmp >> ${HTMLFILE}
    # Substitute the placeholders DOMAIN, DIRNAME, FILENAME
    # in the header (part2) and footer (part3) with the current values.
    #
    # Header
    ${SED} -e "s/_DOMAIN_/${DOMAIN}/g" ${DATADIR}/part2 > part2_1.tmp
    ${SED} -e "s/_DIRNAME_/$1/g" part2_1.tmp > part2_2.tmp
    ${SED} -e "s/_FILENAME_/${BASENAME}/g" part2_2.tmp > part2_3.tmp
    ${SED} -e "s/_TITLE_/${TITLE}/g" part2_3.tmp > part2_4.tmp
    ${SED} -e "s/_FORMATSFILE_/${FORMATSFILE}/g" part2_4.tmp > part2_5.tmp
    ${SED} -e "s/_COPYRIGHT_/${COPYRIGHT}/g" part2_5.tmp > part2_6.tmp
    ${SED} -e "s/_HOMEFILE_/${HOMEFILE}/g" part2_6.tmp > part2_7.tmp
    ${SED} -e "s/_DATE_/${TODAY}/g" part2_7.tmp > part2.tmp
    cat part2.tmp >> ${HTMLFILE}

    # Body
    cat body.tmp >> ${HTMLFILE}

    # Footer
    ${SED} -e "s/_DOMAIN_/${DOMAIN}/g" ${DATADIR}/part3 > part3_1.tmp
    ${SED} -e "s/_DIRNAME_/$1/g" part3_1.tmp > part3_2.tmp
    ${SED} -e "s/_FILENAME_/${BASENAME}/g" part3_2.tmp > part3_3.tmp
    ${SED} -e "s/_TITLE_/${TITLE}/g" part3_3.tmp > part3_4.tmp
    ${SED} -e "s/_FORMATSFILE_/${FORMATSFILE}/g" part3_4.tmp > part3_5.tmp
    ${SED} -e "s/_COPYRIGHT_/${COPYRIGHT}/g" part3_5.tmp > part3_6.tmp
    ${SED} -e "s/_HOMEFILE_/${HOMEFILE}/g" part3_6.tmp > part3_7.tmp
    ${SED} -e "s/_DATE_/${TODAY}/g" part3_7.tmp > part3.tmp
    cat part3.tmp >> ${HTMLFILE}

    # Housekeeping
    rm -f body.tmp title.tmp meta.tmp part2*.tmp part3*.tmp

    # Add alt and title tags to the images.
    ${RUNSED} ${SEDSCRIMG} ${HTMLFILE}
done




# Print formats (PDF, RTF, TXT, PS)
# =================================
#
# Index creation, page numbers (notice the -p option to collateindex 
# and the use of the first, "no chunks", copy of HTML.index in the current directory).
rm index.sgml
${PERL} ${COLLATEINDEX} -p -g -o index.sgml HTML.index 

# We copy all our images under $1.
cp -Rv images $1/



echo "############################ SGML to TeX ############################"
# Generate PDF. 1st pass
${OPENJADE} -t tex -d ${PRINT_PDF_DSL} -o $1.tex -i "output.print.pdf" $1.sgml

# Unescape the math in the .tex file.
if test "$process_math" = "1"; then
  ${UNESCAPEMATH} $1.tex
fi

${PDFJADETEX} $1.tex

# The generated PDF in the 1st pass does not have thumbnails yet.
# 
# Generate thumbnails
# Do not confuse the script THUMB_PDF (thumbpdf) with the
# environment variable THUMBPDF, which passes additional
# options to the THUMB_PDF script.
if test "$use_coolthumbs" = "1"; then
  mkdir thumbnails
  cp $1.pdf thumbnails/
  cd thumbnails
  ${COOLTHUMBS} --v2 $1.pdf
  ${THUMB_PDF} --nomakepng $1
  mv $1.tpt ../
  cd ../
  rm -rf thumbnails
else
  ${THUMB_PDF} $1
fi

# Generate  PDF again (2nd pass), to incorporate the thumbnails:
#
# This will produce a tex file from the SGML source.
${OPENJADE} -t tex -d ${PRINT_PDF_DSL} -o $1.tex -i "output.print.pdf" $1.sgml

# Unescape the math in the .tex file.
if test "$process_math" = "1"; then
  ${UNESCAPEMATH} $1.tex
fi

# This will produce a PDF file from the tex file.
# This PDF file *will* have thumbnails!
echo "############################ PDF Pass 1  ############################"
${PDFJADETEX} $1.tex
# We must call pdfjadetex a second time, because the first time
# there was no .aux file and the bookmarks were not created.
echo "############################ PDF Pass 2  ############################"
${PDFJADETEX} $1.tex
# A third pass of pdflatex is needed, in order to get the page
# numbers in the Table of Contents computed. See the PRINT_PDF_DSL
# used above for the parameters that control printing and
# placement of ToC.
echo "############################ PDF Pass 3  ############################"
${PDFJADETEX} $1.tex


# Generate RTF
echo "############################     RTF     ############################"
${OPENJADE} -t rtf -d ${PRINT_RTF_DSL}  -i "output.print.bmp" $1.sgml


# Generate TXT
# echo "############################     TXT     ############################"
# ${OPENJADE} -t txt -d $PRINT_TXT_DSL -o $1.txt $1.sgml


# Generate PS
# Set the printer to "cmz", so that dvips 
# will search for the file config.cm, which
# contains the mappings for the "Computer-Modern" fonts.
# We use "cmz" instead of "cm" in order to embed the font in
# the PS file, thus making it portable.
PRINTER="cmz"
export PRINTER
echo "############################     PS      ############################"
#
# This will produce a tex file from the SGML source.
${OPENJADE} -t tex -d ${PRINT_PS_DSL} -o $1.tex -i "output.print.eps" $1.sgml

# Unescape the math in the .tex file.
if test "$process_math" = "1"; then
  ${UNESCAPEMATH} $1.tex 
fi

# As with PDFJADETEX above, again 3 passes are necessary:
${JADETEX} $1.tex
${JADETEX} $1.tex
${JADETEX} $1.tex

# A .dvi file was generated. Transform it to PS.
${DVIPS} -o $1.ps $1.dvi

# This is an alternative way to generate PS.
# Depending on the paper format, use one of 
# the two dvips invocations. The -d4100 switsch produces some
# interesting debugging messages.
# dvips  -t letter -Pcm -o $1.ps $1.dvi
# dvips -d4100 -t a4 -Pcm -o $1.ps $1.dvi

# Compress PS
${GZIP} $1.ps

# Move all documents in the HTML directory and remove the .tpt file.
# Remove all .pdf, .eps and .jpg images from ./images under $1
# (./images in the _current_ directory is not affected!)
# From what has been created, leave only $1.sgml and index.sgml 
# in the current directory.
mv $1.txt $1.rtf $1.pdf $1.ps.gz $1/
mv $1.html $1/
# rm $1.tpt $1.log $1.aux $1.out $1.tex
rm $1.tpt $1.aux $1.out $1.tex $1.bak $1/*.bak
rm $1/images/*.pdf $1/images/*.eps $1/images/*.jpg
rm $1/images/*/*.pdf $1/images/*/*.eps $1/images/*/*.jpg
if test "$process_math" = "1"; then
  rm images/math/*
fi

rm $1/HTML.index

# done! Huh...
# Share and enjoy! :-)

# ONLY for my site!
. $1.end
# http://www.crystalfaeries.net/linux/bin/lyxtox


syntax highlighted by Code2HTML, v. 0.9.1