;
; path_util.s
;
; A path manipulation utility
;
;  1994-1998 Straylight
;

;----- Licensing note -------------------------------------------------------
;
; This file is part of Straylight's core utilities (coreutils).
;
; Coreutils is free software; you can redistribute it and/or modify
; it under the terms of the GNU General Public License as published by
; the Free Software Foundation; either version 2, or (at your option)
; any later version.
;
; Coreutils is distributed in the hope that it will be useful,
; but WITHOUT ANY WARRANTY; without even the implied warranty of
; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
; GNU General Public License for more details.
;
; You should have received a copy of the GNU General Public License
; along with Coreutils.  If not, write to the Free Software Foundation,
; 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

;----- Standard header ------------------------------------------------------

		GET	libs:header
		GET	libs:swis

;----- External dependencies ------------------------------------------------

		GET	sh.pathUtil

		IMPORT	version

;----- Main code ------------------------------------------------------------

		AREA	|!!!Utility$$Header|,CODE,READONLY

; --- Main program ---
;
; Arguments:	[-add]|-remove[-path] <pathvar> [-dir] <element>
;
; Use:		Adds an element to a path, or removes one.

main		ROUT

		STMFD	R13!,{R14}		;Keep the return address safe
		ADR	R0,pu__commDef		;Point to command syntax def
		MOV	R2,R12			;Point to a workspace buffer
		MOV	R3,#256			;Size of my buffer thingy
		SWI	XOS_ReadArgs		;Try to understand the string
		BVS	%90main			;If it failed, return error

		; --- Make sure we got sensible arguments ---

		LDMIA	R12,{R10,R11}		;Load the two names

		LDR	R14,[R12,#8]		;Load the help flag value
		CMP	R14,#0			;Is help specified?
		BNE	pu__help		;Yes -- give help then

		LDR	R14,[R12,#24]		;Load the fontinstall value
		CMP	R14,#0			;Does he want to use it?
		ADRNE	R10,pu__fontPath	;Yes -- point to var name

		CMP	R11,#0			;Is there no directory?
		CMPNE	R10,#0			;Or no path variable?
		BEQ	pu__noDir		;None -- report an error

		; --- See if user wants GSTransing ---

 		MOV	R0,R11			;Point to the name string
 		LDR	R14,[R12,#20]		;Load GSTrans flag out
 		CMP	R14,#0			;Does this want doing?
		BEQ	%20main			;No -- skip onwards then
		ADD	R1,R12,#512		;Point to my GSTrans buffer
		MOV	R2,#256			;Set the buffer size
		ORR	R2,R2,#&E0000000	;Set *all* the GSTrans flags
		SWI	XOS_GSTrans		;Translate it nicely
		ADD	R0,R12,#512		;Point to translated string

		; --- Do the main bit of processing ---

20main		ADD	R11,R12,#256		;Point to a spare buffer
		MOV	R1,R0			;Point to directory in R1
		MOV	R0,R10			;Point to path var in R0
		MOV	R2,#2			;Path variables are macros
		LDR	R14,[R12,#36]		;Load the create flag
		CMP	R14,#0			;Do we create the variable?
		ORRNE	R2,R2,#&100		;Yes -- set the bit then
		LDR	R14,[R12,#16]		;Load the remove flag
		CMP	R14,#0			;Are we meant to remove it?
		BLEQ	path_addDir		;No -- then add it in
		BLNE	path_removeDir		;Yes -- remove it then
		BVS	%90main			;If it failed, report error
		BCC	%50main			;If nothing done, return

		; --- If we did fontPath things, update FontManager ---

		LDR	R14,[R12,#24]		;Load the fontinstall value
		CMP	R14,#0			;Does he want to use it?
		BEQ	%50main			;No -- do nothing then

		SWI	XHourglass_On		;This could take some time
		ADR	R0,pu__finstall		;Point to command thingy
		SWI	XOS_CLI			;Try to update the font list
		SWI	XHourglass_Off		;And disable hourglass again

		; --- All done, all sweetness and light ---

50main		LDMFD	R13!,{PC}^		;Return to caller nicely

pu__fontPath	DCB	"Font$Path",0
pu__finstall	DCB	"FontInstall",0

		; --- Someone threw a spanner in the works ---

90main		LDMFD	R13!,{R14}		;Restore the link register
		ORRS	PC,R14,#V_flag		;And return the error

		; --- The command definition table ---

pu__commDef	DCB	"path,"			;Offset 0     p
		DCB	"dir,"			;Offset 4     d

		DCB	"help/S,"		;Offset 8     h
		DCB	"add/S,"		;Offset 12    a
		DCB	"remove/S,"		;Offset 16    r
		DCB	"GS=GSTrans/S,"		;Offset 20    g
		DCB	"fInstall=fontPath/S,"	;Offset 24    f
		DCB	"create/S,"		;Offset 28    c
		DCB	0

		; --- Our arguments weren't done right ---

pu__noDir	ADR	R0,pu__errNoDir		;Point to the error message
		B	%90main			;And report it as usual

pu__errNoDir	DCD	1
		DCB	"Syntax: PathUtil [<options>] "
		DCB	"[-path] <pathvar> [-dir] <directory>",0

		; --- User wanted a helping hand ---

pu__help	ADR	R0,pu__helpText		;Point to the help text
		MOV	R1,#0			;Don't use a dictionary
		LDR	R2,=version		;Point to version/cright
		ADR	R14,main		;Point to code base
		ADD	R2,R14,R2		;And point to the help text
		SWI	XOS_PrettyPrint		;Print it on the screen
		LDMFD	R13!,{PC}^		;And return to caller

pu__helpText	DCB	"PathUtil ",27,0,13
		DCB	13
		DCB	"Syntax: PathUtil [<options>] "
		DCB	"[-path] <pathvar> [-dir] <directory>",13
		DCB	13
		DCB	"By default, adds a path element to the given path "
		DCB	"variable.  If the element is already included, "
		DCB	"PathUtil does nothing.",13
		DCB	13
		DCB	"Options (which may be abbreviated) are as "
		DCB	"follows:",13
		DCB	13
		DCB	"-help",9,9,"Display this help text",13
		DCB	"-add",9,9,"Add directory to path (default)",13
		DCB	"-remove",9,9,"Remove directory from path",13
		DCB	"-gsTrans",9,"Translate directory before adding",13
		DCB	"-fontPath",9,"Use Font$Path as path variable",13
		DCB	"-create",9,9,"Force creation of path variable",13
		DCB	0

;----- That's all, folks ----------------------------------------------------

		END
