/*
** AREXX $VER: Start_TeX.sd�V1.42 (24.7.92)
**
** This AREXX script saves and compiles the current view using the last
** format used, unless you supply a format name as argument. A '?'
** formatname will interactively ask for the format to use. If no format
** is supplied and the TeX-server has been set up to always ask for the
** default format, then it will do so here. Even under WB 1.3
**
** A command is send to the TeX server to compile the file. Hence a
** return value of 0 does not mean that the file compiled well, but only
** that the command was sent to the server and replied to.
**
** AUTHOR: J\"org H\"ohle, March 91
**         Revised: 24 July 1991
**
** BUGS: virtex doesn't like filenames with blanks (and ARexx parses them
**       hardly too), so avoid them in file, directory *and* device names.
**
** FILES: ENV:TEXFORMAT     default format used
**        REXX:NameStruc
**
** EXAMPLE: of ENV:TEXCONFIG(TeX:config/)ShowDVI.config (partial)
**
**   f1 TeX:rexx/Start_TeX.sd          ; use default=last used
**   f2 TeX:rexx/Start_TeX.sd plaine   ; use English format
**   f3 TeX:rexx/Start_TeX.sd plaind   ; use Deutsch format
**   f4 TeX:rexx/Start_TeX.sd latexgde ; use LaTeX special format
**   f5 TeX:rexx/Start_TeX.sd ?        ; always ask for format
*/

PORTNAME = 'Start_TeX'
SCRIPT   = 'TeX-server.rexx'

/*
** Ask interactively for format name
*/
If "" = GetClip("TEXQUERY") Then
  ASKFORMAT = 0
Else
  ASKFORMAT = 1

Options Results

Parse Arg FORMAT .
If "?" = FORMAT Then Do
  ASKFORMAT = 1
  FORMAT    = ""
End; Else If '&' = Left(FORMAT,1) Then
  FORMAT = SubStr(FORMAT,2)

GetFile
LOADEDFILE = RESULT

GetDir
LOADEDDIR = RESULT

If Right(LOADEDDIR,1) ~= '/' & Right(LOADEDDIR,1) ~= ':' Then
  LOADEDDIR = LOADEDDIR||'/'

FULLNAME = LOADEDDIR||LOADEDFILE

Parse Value NameStruc(FULLNAME) With IVOL IDIRS IBASE .

/*
** May be use GetClip("TEXFILE")?
*/
If ".DVI" = Upper(Right(LOADEDFILE,4)) Then
  FULLNAME = Overlay(".tex", FULLNAME,1+IVOL+IDIRS+IBASE)
Else
  FULLNAME = FULLNAME||".tex"

If 0 = IVOL Then Do
  DIREC = Pragma('D')
  If Right(DIREC,1) ~= '/' & Right(DIREC,1) ~= ':' Then
    DIREC = DIREC||'/'
  FULLNAME = DIREC||FULLNAME
End

If ~Exists(FULLNAME) Then Do
  Okay1 'Konnte die Datei 'FULLNAME' nicht finden!'
  Exit 10
End

If Show('Port',PORTNAME) Then Do
/*
** Set the default FORMAT, modify it to suit your needs
*/
  ENVFORMAT = MyGetEnv("TEXFORMAT")
  If "" = FORMAT Then Do
    FORMAT = ENVFORMAT
    If ASKFORMAT | "" = ENVFORMAT Then Do
      If "" = FORMAT Then
        FORMAT = 'plain'

      'GetString 'FORMAT '"Which format to use ?"'
      NFORMAT = RESULT
/*
** "RESULT" if cancelled
*/
      If "RESULT" ~= NFORMAT Then
        FORMAT = NFORMAT
    End /* ASKFORMAT */
  End   /* FORMAT    */

  If FORMAT ~= ENVFORMAT Then
    Call MySetEnv("TEXFORMAT",FORMAT)

  Message 'Calling TeX server with format 'FORMAT' and file 'FULLNAME'.'

  OLDADDR = Address()
  Address Value PORTNAME
  'compile' FORMAT FULLNAME
  Address Value OLDADDR
  Message 'TeX server called for file 'FULLNAME'.'
End; Else Do
  Okay1 'Der TeX-Server l�uft nicht!'
  Exit 10
End

Exit

/*
** When will ARexx supply GetEnv/SetEnv?
*/
MyGetEnv: Procedure
Parse Arg NAME

If Open(TEMPFILE,"ENV:"||NAME,'r') Then Do
  GIVES = Readln(TEMPFILE)
  Call Close TEMPFILE
End; Else
  GIVES = ""

Return GIVES

MySetEnv: Procedure
Parse Arg NAME,CONTENT

Address COMMAND "SetEnv" NAME CONTENT

Return
