#! /bin/bash
#
# HTMLforTeX by Anthony Phan.
# file: texscape (html browser based on HTML4TeX)
# definitively last modification: October 26, 2001.
#
# windows = 21cm x 29.7cm
# text    = 17cm x 25.7cm
# change the script to change settings...
#

#
# external programs
#

TEXSCAPEMAIL="pine"
# use your favorite program

#
# initialization
# BASEDIR default value is the path of the current directory
#

TEXSCAPEDIR="$HOME/.texscape/" # one could choose "/tmp"
COUNTER=1
BASEDIR=$PWD

#
# creation of TeXscape's directory if necessary
#

if [ ! -d $TEXSCAPEDIR ]; then
    mkdir $TEXSCAPEDIR
    echo "creating TeXscape directory"
fi

#
# strings which will be added to the TeX file
#

MYTEXINPUTS="% specific input files"
VERBOSE="\relax"
NOMEDIA="\relax"
HTMLINUX="\input htmlinux.tex"

#
# processing options
#

NONSTOPMODE="-interaction=nonstopmode"
ALLOWSHELL="-allowshell"

#
# extra variables
#

VISUAL=""
NOCLEAN=""
FORMAT=""
MAGSTEP=""

#
# help message
#

usage="\
Usage:
    texscape
	[-base <directory>] if not specified, current directory
	[-counter <number>] suffix number of the previous TeXscape job
	[-debug]            equivalent to [-stop -verbose]
	[-help]             prints this message and exit
	[-input <file.tex>] extra TeX control sequences if needed
	[-latex]            uses LaTeX format (and uses nfss...)
	[-magstep]          plain TeX magnification (0, 1, ...)
	[-noall]            even better than [-nocolors -nomedia -noshell]
	[-noclean]          doesn't clean TeXscape directory
	[-nocolors]         doesn't attempt to use colors
        [-nomedia]          doesn't attempt to use hyperrefs, images, ...
	[-noshell]          disallows shell for xdvi
	[-postscript]       displays postscript instead of dvi
	[-stop]             allows TeX to stop for debugging
	[-verbose]          displays maximum information while processing
	<file.html>         the very input, <file:file.html> also valid,
	                    things like <mailto:somebody@someplace?whatever>
                            may be one day also valid
    All flags can be abbreviated to their shortest unique prefix."

#
# processing arguments
#

WWWBROWSER="texscape"

while test $# -gt 0; do
case $1 in
    -b*) BASEDIR="$2"; shift; shift;;
    -c*) COUNTER=$[$2+1]; shift; shift;;
    -d*) NONSTOPMODE="";
        VERBOSE="\let\HTMLproofmode=!";
        WWWBROWSER="$WWWBROWSER -debug"; shift;;
    -h*) echo "$usage"; exit 0;;
    -i*) MYTEXINPUTS="$MYTEXINPUTS
	\input $2";
	WWWBROWSER="$WWWBROWSER -i $2"; shift; shift;;
    -l*) FORMAT="LaTeX";
	WWWBROWSER="$WWWBROWSER -latex"; shift;;
    -m*) MAGSTEP="\magnification=\magstep$2";
	WWWBROWSER="$WWWBROWSER -m $2"; shift; shift;;
    -noa*) NOMEDIA="\noHTMLcolors\noHTMLmultimedia";
	\HTMLINUX="\relax"; ALLOWSHELL="";
	WWWBROWSER="$WWWBROWSER -noall"; shift;;
    -nocl*) NOCLEAN="whatever";
	WWWBROWSER="$WWWBROWSER -noclean"; shift;;
    -noco*) NOMEDIA="$NOMEDIA\noHTMLcolors";
	WWWBROWSER="$WWWBROWSER -nocolors"; shift;;
    -nom*) NOMEDIA="$NOMEDIA\noHTMLmultimedia";
	WWWBROWSER="$WWWBROWSER -nomedia"; shift;;
    -nos*) ALLOWSHELL="";
	WWWBROWSER="$WWWBROWSER -noshell"; shift;;
    -p*) VISUAL="postscript";
	WWWBROWSER="$WWWBROWSER -postscript"; shift;;
    -s*) NONSTOPMODE="";
	WWWBROWSER="$WWWBROWSER -stop"; shift;;
    -v*) VERBOSE="\let\HTMLproofmode=!";
	WWWBROWSER="$WWWBROWSER -verbose"; shift;;
    file:*) FILENAME=`echo $1 | sed s/file://g`;
	break;;
    ftp:*) echo "$1 not yet supported";
	exit 1;;
    gopher:*) echo "$1 not yet supported";
	exit 1;;
    http:*) echo "$1 not yet supported";
	exit 1;;
#	FILENAME="${TEXSCAPEDIR}TeXscape-$COUNTER.tmp";
#	wget $1 -O $FILENAME;
#	BASEDIR=`echo $1 | cut -f 1-3 -d /`;
# ???????????????????????????
#	break;;
    mailto:*) ADDRESS=`echo $1 | sed s/mailto://g`;
	ADDRESS=`echo $ADDRESS ? | cut -f 1 -d ?`;
######	subject, etc. unsupported by now (I've got to learn the use of cut)
	$TEXSCAPEMAIL $ADDRESS;
	exit 0;;
    news:*) echo "$1 not yet supported";
	exit 1;;
    telnet:*) echo "$1 not yet supported";
	exit 1;;
    wais:*) echo "$1 not yet supported";
	exit 1;;
    *) FILENAME="$1";
	break;;
esac
done

WWWBROWSER="$WWWBROWSER -b $BASEDIR -counter $COUNTER"
export WWWBROWSER

echo "starting TeXscape on $FILENAME"

if [ -f $FILENAME ]; then

# create texfile $JOBNAME.tex in the TeXscape directory,
# TeX it and visualize it, then remove it
# with its auxiliary files ($JOBNAME.*)

JOBNAME="TeXscape-$COUNTER"

pushd $TEXSCAPEDIR

if [ -z "$FORMAT" ]
then
echo "% $JOBNAME.tex (temporary file)
$MAGSTEP
\hoffset=-0.56 true cm
\voffset=-0.53 true cm
\hsize=17 true cm
\vsize=25.7 true cm
\parindent=1.333em
$VERBOSE
\input html4tex.tex
$HTMLINUX
$NOMEDIA
$MYTEXINPUTS
\HTMLbase{$BASEDIR/}
\includeHTML{$FILENAME}
\bye">$JOBNAME.tex
tex $NONSTOPMODE $JOBNAME.tex
else
echo "% $JOBNAME.tex (temporary file)
\documentclass{article}
\parindent=1.333em
\paperheight=29.7 true cm
\paperwidth=21 true cm
\textheight=25.7 true cm
\textwidth=17 true cm
\oddsidemargin=\paperwidth
\advance\oddsidemargin by -\textwidth
\divide\oddsidemargin by 2
\advance\oddsidemargin by -1 in
\evensidemargin=\oddsidemargin
\topmargin=\paperheight
\advance\topmargin by -\headheight
\advance\topmargin by -\headsep
\advance\topmargin by -\textheight
\advance\topmargin by -\footskip
\divide\topmargin by 2
\advance\topmargin by -1 in
\pagestyle{empty}%
$VERBOSE
\input html4tex.tex
$HTMLINUX
$NOMEDIA
$MYTEXINPUTS
\HTMLbase{$BASEDIR/}
\begin{document}
\includeHTML{$FILENAME}
\end{document}">$JOBNAME.tex
latex $NONSTOPMODE $JOBNAME.tex
fi

if [ -z "$VISUAL" ]
then
    xdvi $ALLOWSHELL -paper a4 -thorough $JOBNAME.dvi
else
    dvips $JOBNAME -D600 -o; ghostview $JOBNAME.ps
fi

if [ -z "$NOCLEAN" ]
then
    rm -f $JOBNAME.*
fi

popd

else

echo "file $FILENAME not found"

fi

echo "quitting TeXscape"

exit 0


