/*RX
 * AREXX	Name:Start_TeX.sd	Version:1.41	Date:27-Jul-91
 *
 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
f3		TeX:rexx/Start_TeX.sd plaind
f4		TeX:rexx/Start_TeX.sd latexgde
f5		TeX:rexx/Start_TeX.sd ?		; always ask for format
 */

portname = 'Start_TeX'
script	 = 'TeX-server.rexx'

IF "" = GETCLIP("TEXQUERY") THEN askformat = 0
ELSE askformat = 1		/* ask interactively for format name	*/

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 'Cannot find file 'fullname'!'
	EXIT 10
	END

IF SHOW('P',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 'The TeX server in 'script' is not running !'
	EXIT 10
	END
EXIT


mygetenv: procedure	/* when will ARexx supply GetEnv/SetEnv ? */
   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
