
/*	RX
 * 	AREXX	NAME:TeX-server.rexx	Version:1.42		DATE:30-Jul-91
 *
 	This script creates an ARex port, by the name of
 *
 *		Port Name = "Start_TeX"
 *
 * 		and waits there for the commands
 *
 *	- compile <format-file> <TeX-file including-absolute-path>
 *			- the filename *must* have an extension.
 *			- if <format-file>=='&', use tex'es default format.
 *	- quit
 *			exits the server.
 *
 * It may be called with one parameter: the name of the public screen
 * (OS 2.0) in which this server runs.
 *
	AUTHORS:
 *             April 91		J\"org H\"ohle
 *		before		Georg Hessmann
	NOTES:
 	An `absolute path' is given if you use a volume, device or assign'ed
	name like TeX_Disk1:, RAM: or TeX:.
 *
 * 	Make virtex resident (ARP or AmigaDOS, depending on your shell), or
	include it in your shell path. However the default of 'virtex' may be
	overridden at any time by the ARexx variable virtex (lowercase!) (see
	further below). This has been implemented to be able to switch
	between smallTeX and BigTeX on the fly.
 *
	BUGS:
 		Does not allow blanks in filenames and directories, because neither
		ARexx nor TeX are happy with them.
 *
	FILES:
	 	Rexx:namestruc		: namestruc is a function, and must stay in REXX:
							  for now. Sorry about that.
 		Libs:rexxsupport.library
 *
 */

/*
 Private Umgebung aus Clip  lesen (bitte dort ablegen):
*/

CALL GetENVclip


portname = StartPort    /* PortName */

/* Failat 10 */

OPTIONS RESULTS

PARSE ARG PubScreen
IF PubScreen = "" THEN PubScreen = "Workbench"

IF ~show('L','rexxsupport.library') THEN
	IF ~addlib('rexxsupport.library',0,-30,0) THEN DO
		say "Can't open 'rexxsupport.library'!"
        drop StartPort CEDport sDVIport comDIR Texts console TeXbin TeXcnfDIR Virtex cedDIR Format
		EXIT 20
		END

/* ARexx V1.10 sets RC to Result * 65536 + Result2
	 V1.14 and 1.15 set RC to Result	on a shell command call.
*/

parse version . rexxver .
filter2 = rexxver < "V1.14"

/*say 'ver'rexxver 'filter 'filter2*/

SIGNAL ON break_C
SIGNAL ON break_D

FALSE = 0
TRUE = 1

say
say "*******************************************************"
say "*                                                     *"
say "*    Welcome to the ARexx - Passauer TeX Interface    *"
say "*                                                     *"
say "*******************************************************"

say
oldedit = upper(mygetenv(TEXREXX))



/* default is no change, logic ?
AUSDOKUMENTIERT...

 IF "EDIT" = oldedit THEN DO
    OPTIONS PROMPT "Call editor automatically on first error (Y/n)? "
	PARSE UPPER PULL yn .
	IF LEFT(yn,1) = "N"	THEN newedit = "e"
    	ELSE newedit = "edit"
	END
 ELSE DO
	OPTIONS PROMPT "Call editor automatically on first error (y/N)? "
	PARSE UPPER PULL yn .
	IF LEFT(yn,1) = "Y"	THEN newedit = "edit"
				ELSE newedit = "e"
	END
...AUSDOKUMENTIERT */

newedit = "e"
IF UPPER(newedit) ~= oldedit THEN CALL mysetenv(TEXREXX,newedit)
/* if it were undefined, virtex would call ENV:EDITOR.
	 We want Rexx scripts to be used (TeXEdit.rexx).
*/

DROP newedit oldedit

say
/* AUSDOKUMENTIERT...

    OPTIONS PROMPT "Ask for format when default requested (y/N) ? "
    PARSE UPPER PULL yn .
...AUSDOKUMENTIERT */

yn = "N"
IF (LEFT(yn,1) = "Y") THEN
  CALL SETCLIP("TEXQUERY","TRUE")
ELSE
  CALL SETCLIP("TEXQUERY")

OPTIONS PROMPT "? >"
DROP yn

say
say "- Opening port "portname"."

IF ~openport(portname) THEN DO
  say
  say "- Can't open it !"
  drop StartPort CEDport sDVIport comDIR Texts console TeXbin TeXcnfDIR Virtex cedDIR Format
  EXIT 20
END


empty = FALSE

mainloop:
DO UNTIL com = 'quit'

  IF ~empty THEN DO
	CALL SETCLIP("TEXTIME")
	say "- Waiting for command (compile/quit) on port "portname"."
  END

  CALL waitpkt(portname)

  packet = getpkt(portname)
  IF (packet == NULL()) THEN empty = TRUE /* say "- Received NULL packet." */
  		/* ARexx 1.14 and 1.15 tend to send *lots* of NULL packets */
  ELSE DO

    empty = FALSE

    PARSE VALUE getarg(packet) WITH com format fullname

    CALL REPLY(packet, 0)

    IF (com = 'compile') THEN DO

	/* and now, Server-to-front */
	ADDRESS COMMAND 'EASys!_bin:Screentofront' '>NIL:' 'PubScreen'
	/* Result: 5: not OS 2.0, 10: PubScreen not found,
		15: no Intuition.library. */

	IF '&' ~= left(format,1) THEN format = '&'||format
	ELSE IF '&' = format THEN format = "" /* use virtex default format */

	fullname = STRIP(fullname)	/* beware of blanks (eg RAM DISK:) */

	/* make RAM DISK: a special case */
	IF "RAM DISK:" = UPPER(LEFT(fullname,9)) THEN
  	   fullname="RAM:"||SUBSTR(fullname,10)

/* KLUDGE: Do NOT accept names with blanks, I could do a better job here */
	IF 0 ~= INDEX(fullname," ") THEN
	   say "- Sorry, no spaces in filenames :"fullname
	ELSE DO

/**	 namestruc fullname
	 if 0 ~= RC THEN DO
	    say "- Function "namestruc" not found, aborted!"
	    SIGNAL finish
	    END
	 PARSE VALUE RESULT WITH ivol idir iname .
**/
	 PARSE VALUE namestruc(fullname) WITH ivol idir iname .

	 texdir = LEFT(fullname, ivol+idir)  /*(TeX:doc/sample.tex-> TeX:doc/)*/
	 nameonly = SUBSTR(fullname, 1+ivol+idir)		/*(sample.tex)*/
	 baseonly = SUBSTR(fullname, 1+ivol+idir, iname)	/*(sample)*/
	 fullbase = LEFT(fullname, ivol+idir+iname)	/*(TeX:doc/sample)*/

	 IF 0=ivol THEN DO
	   say "- An absolute path must be used (eg. <device|volumename>:...) !"
	   END
	 ELSE IF (SUBSTR(fullname, 1+ivol+idir+iname,1) ~= ".") THEN DO
	   say "- Sorry, filename must have a .<extension> :"fullname
	   END
	 ELSE DO

	  IF ~EXISTS(fullname) THEN DO
	   say "- Couldn't locate "fullname" ! Missing a path ?"
	   END
	  ELSE DO

	   CALL DELETE(fullbase||".log")
	   /* I feel there's no need to delete the .dvi file. */

	   virtex = GETCLIP("virtex")	/* get TeX or bigTeX */
	   IF "" = virtex THEN virtex = 'virtex'

	   say '- Start command: 'virtex format nameonly
	   say '   in directory 'texdir
	   CALL PRAGMA('directory',texdir)

	   CALL SETCLIP("TEXFILE",fullname)
	   now = TIME('s')
	   CALL SETCLIP("TEXTIME",now)

/* The "< *" is a hack, no ? */
	   ADDRESS COMMAND virtex' < * 'format nameonly

/* How can I get rid of the Rexx message "+++ Command returned >=65536"
   with versions of ARexx <= V1.10 ? */
	   IF filter2 THEN errorcode = RC % 65536 /* ARexx V1.10	*/
	   ELSE		   errorcode = RC	  /* ARexx V1.14, V1.15	*/
	   IF errorcode == 0 THEN DO
		say
		say "- Compiled file "fullname" without errors."
		IF (SHOW('P','showdvi')) THEN DO
		   ADDRESS "showdvi"
		   OPTIONS RESULTS
		   getfile
		   loadedfile = RESULT
		   getdir
		   loadeddir = RESULT
		   IF RIGHT(loadeddir,1)~=':' & RIGHT(loadeddir,1)~='/' THEN
		   	loadeddir = loadeddir||'/'
		   IF texdir = loadeddir & 1 = INDEX(UPPER(loadedfile||".dvi"),UPPER(baseonly||".dvi")) THEN DO
			say "- Updating file in ShowDVI."
			loadagain
			END
		   ELSE DO
			say "- Loading file "fullbase".dvi into ShowDVI."
			loadnew fullbase||".dvi"
			END
		   tofront
		   END
		ELSE DO
		   say "- ShowDVI is not running."
		   END
		END
	   ELSE DO
		say
		say "- Compilation not successful, error level "errorcode"."
		IF getclip("TEXTIME") == now & EXISTS(fullbase||".log") THEN DO
		  editscript = WORD(mygetenv("TEXREXXEDIT"),1)
		  IF "" = editscript THEN editscript = 'TeXedit.rexx'
		  say "- Calling editor for file "fullname" via "editscript"."
		  editscript fullname 0
		  END
		END
	   END	/* exists */
	  END	/* !ivol, "." */
	 END	/* !index */
	END	/* compile */
    END		/* !NULL() */
  END		/* do */

finish:

ADDRESS COMMAND 'EASys!_bin:Screentofront' '>NIL:' 'PubScreen'
/* Result: 5: not OS 2.0, 10: PubScreen not found, 15: no Intuition.library */

say "- Closing down port "portname
CALL CLOSEPORT portname

IF PubScreen ~= "Workbench" THEN
	say "  Type CTRL-\ or hit the CLOSE Gadget to close the window."
	/* das koennte man im Prinzip immer (unter 2.0) ausgeben */

SIGNAL cleanup

break_C:
break_D:
say
say "- Break received."

cleanup:
CALL SETCLIP("TEXFILE")
CALL SETCLIP("TEXTIME")
CALL SETCLIP("TEXQUERY")
EXIT


GetENVclip:
 StartPort=getclip("TeXStartPort")
 CEDport=getclip("TeXRexxCEDport")
 sDVIport=getclip("TeXRexxShowDVIport")
 comDIR=getclip("TeXcomDIR")
 Texts=getclip("TeXTextDIR")
 console=getclip("TeXmsgconsole")
 TeXbin=getclip("TeXbinDIR")
 TeXcnfDIR=getclip("TeXconfigDIR")
 Virtex=getclip("TeXvirtex")
 cedDIR=getclip("TeXcedDIR")
 Format=mygetenv(TEXFORMAT)

 IF "" = StartPort THEN StartPort='Start_TeX'
 IF "" = CEDport THEN CEDport='rexx_ced'
 IF "" = sDVIport THEN sDVIport='showdvi'
 IF "" = comDIR THEN comDIR='EASys!_bin:'
 IF "" = Texts THEN Texts='Texte:'
 IF "" = console THEN console=">CON:0/111/606/64/TeX-Start:_Message/CLOSE/WAIT"
 IF "" = TeXbin THEN TeXbin='TeX:bin/'
 IF "" = TeXcnfDIR THEN TeXcnfDIR='TeX:config/'
 IF "" = Virtex THEN Virtex='VirTeX'
 IF "" = cedDIR THEN cedDIR='c:'
 IF "GIVES" = Format THEN Format='glplain'

 RETURN 1

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 = "GIVES"

   RETURN gives

mysetenv: PROCEDURE
   PARSE ARG name,content

   ADDRESS COMMAND "SetEnv" name content

   RETURN

