#!/bin/csh

# This script takes a doc id, complete with protocol,
# tries to obtain the document, then runs converts HTML
# to LaTex, then tries to print it.  It is intended to
# be used from the Viola WWW browser but might he useful
# elsewhere too
# Jim Davis, Nov 4 1992

set noglob
set docid = $1

setenv WWW /usr/local/WWW/

# first separate the protocol from the doc id
set halves = (`echo $docid | tr : ' '`)
set protocol = $halves[1]
set doc = `echo $1 | sed -e s\?^${protocol}://\?\?`

# now separate host from PN
set pieces = ( `echo $doc | sed -e 's?//?/?' | tr / ' '`)
set host = $pieces[1]
set pn = `echo $doc | sed -e s/${host}//`

#echo PROTOCOL $protocol  HOST $host PN $pn
#exit 0

set LOG = /tmp/html.plog
if (-f $LOG) rm -f $LOG
touch $LOG

switch ($protocol)
 case file:
    if (-f $pn) then
      echo Printing local file $pn >> $LOG
    else
      echo "Can't print $pn, it is not a file" >> $LOG
      echo "Can't print $pn, it is not a file"
      exit 1
    endif
    breaksw
 case http:
    echo Getting $doc via HTTP >> $LOG
    $WWW/bin/get-http-document $doc > /tmp/html.html
    set pn =  /tmp/html.html
    breaksw
 default:
    echo No implementation for protocol $1 >>$LOG
    exit -1
    breaksw
endsw

# Now we have the document in file named by $pn

# If the document is not an HTML file, who knows what will happen?

# Convert it to LaTex.  
# If the document was mal-formed, this will yield bad Tex, sigh.
cd /tmp 

if (-f /tmp/html.tex) rm -f html.tex
$WWW/bin/html-to-latex $pn -o /tmp/html.tex

if (-z html.tex) then
 echo Conversion to LaTex failed for $pn >>$LOG
 exit -1
endif

# Now try to run LaTex.  But if we get an error that's bad.

if (-f html.dvi) rm -f html.dvi
latex /tmp/html.tex <<EOF >>$LOG
X
EOF

if (-f html.dvi) then
   if (-f html.ps) rm -f html.ps
   dvips html -o html.ps >>& $LOG
   if (-f html.ps) then
          echo Sending html.ps to printer >>$LOG
          lpr html.ps
   endif
 # rm html.*
   exit 0
endif




