/*RX
 * AREXX	Name:Start_TeX.rexx	Version:1.41	Date:27-Jul-91
 *
    This AREXX script compiles the files given in argument, using an
    optional format argument. All in all, it handles as if you had called
    virtex directly (except if you want to pass virtex some options). As
    the server is called, any virtex error messages will go to the server
    window, not the CLI. A '?' formatname will interactively ask for the
    format to use.
 *
    AUTHOR:
 *	    Jörg Höhle, March 91
 *	    Revised: 21 April 1991
    BUGS:
        virtex doesn't like filenames with blanks (and ARexx parses them
        hardly too), so avoid them in file, directory *and* device names.
 *
        does not like names relative to the local root, like ":foo/bar"
 *
    FILES:
        ENV:TEXFORMAT: default format used
        REXX:namestruc
 *
 */

OPTIONS RESULTS

SIGNAL ON BREAK_C
SIGNAL ON BREAK_D

portname = 'Start_TeX'

/*
TeXRexx	 = 'TeX:rexx/'		        / * where our scripts stay	* /
namestruc= TeXRexx||'namestruc'
*/

script	 = 'TeX:rexx/TeX-server.rexx'	/* no path required, message only	*/

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

PARSE ARG format fullname
IF "?" = format THEN DO
	askformat= 1
	format	 = ""
	END
ELSE IF '&' = LEFT(format,1) THEN
	format = SUBSTR(format,2)
ELSE DO
	PARSE ARG fullname	/* no format given */
	format = ""
	END

fullname = STRIP(fullname)

/**
namestruc fullname
IF 0~= RC THEN DO
	say "Function "namestruc" not found!"
	EXIT 10
	END
ELSE /* PARSE VALUE RESULT WITH ivol idirs ibase . */
 PARSE VALUE namestruc(fullname) WITH ivol idirs ibase .

IF "" == SUBSTR(fullname, 1+ivol+idirs+ibase) THEN
	fullname = fullname||".tex"	/* supply a default extension	*/

IF 0 = ivol THEN DO
	direc = PRAGMA('d')
	IF RIGHT(direc,1)~='/' & RIGHT(direc,1)~=':' THEN direc=direc||'/'
	fullname = direc||fullname
	DROP direc
	END
DROP ivol idirs ibase
**/

IF ~EXISTS(fullname) THEN DO
	say 'Sorry, cannot find 'fullname'!'
	EXIT 10
	END

IF (SHOW('P', portname)) THEN DO
	envformat = mygetenv("TEXFORMAT")
	IF "" = format THEN DO
	    format = envformat
	    IF askformat | "" = envformat THEN DO
		IF "" = format THEN format = 'plain'

		OPTIONS PROMPT 'Which format to use (default 'format')? '
		PARSE PULL nformat .
		OPTIONS PROMPT
		IF "" ~= nformat THEN format = nformat

		END /* askformat */
	    END /* !format */

	/* If the server is already busy with some compilation, this will
	wait till compilation finishes, thus may take a long time. */

	say 'calling TeX server with format 'format' and file 'fullname'.'
	if format ~= envformat THEN CALL mysetenv("TEXFORMAT",format)
	ADDRESS VALUE portname
	'compile' format fullname
	say 'TeX server called for file 'fullname'.'
	END
ELSE DO
	/* The TeX server must be started first */
	say 'The TeX server 'script' is not running !'
	EXIT 5
	END

break_c:
break_d:
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
