
	SECTION	GadGridRunBack,CODE

rb_hunkstart:

	;Store away the command line parameter pointers
		MOVEM.L	A0/D0,-(A7)
		
	;Allocate some memory for the command
		MOVE.L	4,A6
		MOVE.L	#MEMF_PUBLIC,D1
		ADD.L	#8,D0	
		JSR	_LVOAllocMem(A6)
		TST.L	D0
		BEQ	ExitQuick			;No memory!
		
	;Store this pointer for our new process
		MOVE.L	D0,ParmPtr
		
	;Restore the pointer to the parameters
		MOVEM.L	(A7)+,A0/D0
		
	;Store the length of the allocated memory for new process
		MOVE.L	D0,ParmLen
		ADD.L	#8,ParmLen
		
	;Start the command with "EXECUTE "
		MOVE.L	ParmPtr,A1
		MOVE.L	#"EXEC",(A1)+
		MOVE.L	#"UTE ",(A1)+
		
	;Copy the parameter text into the allocated memory
CopyLoop:	MOVE.B	(A0)+,(A1)+
		DBEQ	D0,CopyLoop
			
	;Open the dos.library
		MOVE.L	4,A6
		LEA	DosName,A1
		CLR.L	D0
		JSR	_LVOOpenLibrary(A6)
		MOVE.L	D0,DosBase

	;First find pointer to our task
		MOVE.L	4,A6
		SUBA.L	A1,A1
		JSR	_LVOFindTask(A6)
		MOVE.L	D0,A4

	;Get pointer to the Lock on the Current Directory		
		MOVE.L	pr_CurrentDir(A4),D1
		
	;Create a duplicate copy of that lock
		MOVE.L	DosBase,A6
		JSR	_LVODupLock(A6)
		MOVE.L	D0,CurrentDir
		
	;Forbid task switching while we mess with the Segment List
		MOVE.L	4,A6
		JSR	_LVOForbid(A6)
		
	;Get a pointer to the next segment after this one
		LEA	rb_hunkstart-4(PC),A0
		MOVE.L	(A0),D3		
		
	;Break the link to the next segment, so it isn't unloaded
	;when this program exits.
		CLR.L	(A0)
		
	;Now create a new process.
		MOVE.L	DosBase,A6
		MOVE.L	#ProcessName,D1
		MOVE.L	#0,D2			;Priority
		MOVE.L	#4000,D4		;Stack Size
		JSR	_LVOCreateProc(A6)
		
	;Permit Task Switching Again
		MOVE.L	4,A6
		JSR	_LVOPermit(A6)
		
	;Exit Back to Dos
ExitQuick:	CLR.L	D0
		RTS

DosName:	DC.B	"dos.library",0

;Any sections after this point will be unlinked from the segment list
;and launched as a new process. This new process MUST use UnLoadSeg to
;remove itself from memory before exiting & free the Lock on the
;current directory.

		SECTION NewProcess,CODE

NewProcessStart:

;**** program code here

	;Get pointers to the CLI parameters
		MOVE.L	ParmPtr,A0
		MOVE.L	ParmLen,D0
		
	;Set the last character to NULL rather than LINEFEED
		SUB.L	#1,D0
		MOVE.B	#0,0(A0,D0)

	;Clear D3 (Used as Output() later if no ConsoleName given)
		CLR.L	D3

	;Search for a "©" character
SearchLoop:	TST.B	(A0)
		BEQ	NotFound
		CMP.B	#"©",(A0)+
		BNE	SearchLoop
	
	;We found a "©", change it to NULL
		MOVE.B	#0,-1(A0)
		
	;Open the default Output() channel as the console name
		MOVE.L	DosBase,A6
		MOVE.L	A0,D1
		MOVE.L	#MODE_NEWFILE,D2
		JSR	_LVOOpen(A6)
		MOVE.L	D0,D3

NotFound:

	;Store the Output() FileHandle for later on.
		MOVE.L	D3,-(A7)

	;Execute() the CLI parameter
		MOVE.L	DosBase,A6
		MOVE.L	ParmPtr,D1
		CLR.L	D2
		JSR	_LVOExecute(A6)

	;Get the Output() FileHandle back of the stack
		MOVE.L	(A7)+,D1
		
	;Was it NULL ?
		TST.L	D1
		BEQ	SkipCloseCon
		
	;No, so close the console window
		MOVE.L	DosBase,A6
		JSR	_LVOClose(A6)
		
	;Delete the Script file
SkipCloseCon:	MOVE.L	DosBase,A6
		MOVE.L	ParmPtr,D1
		ADD.L	#8,D1
		JSR	_LVODeleteFile(A6)
		

ExitNewProcess:

	;Free the memory containing the CLI parameters
		MOVE.L	4,A6
		MOVE.L	ParmPtr,A1
		MOVE.L	ParmLen,D0
		JSR	_LVOFreeMem(A6)

	;Forbid task switching while we unload our process
		MOVE.L	4,A6
		JSR	_LVOForbid(A6)
		
	;Free the Lock on the Current Directory
		MOVE.L	DosBase,A6
		MOVE.L	CurrentDir,D1
		JSR	_LVOUnLock(A6)
		
	;Get a BCPL pointer to the first segment of our NewProcess
		LEA	NewProcessStart-4(PC),A0
		MOVE.L	A0,D1
		LSR.L	#2,D1		;BCPL pointer = Address/4
				
	;Unload the Segment list from memory
		JSR	_LVOUnLoadSeg(A6)

	;Close the dos library
		MOVE.L	4,A6
		MOVE.L	DosBase,A1
		JSR	_LVOCloseLibrary(A6)
		
	;Exit process
		CLR.L	D0
		RTS
		
DosBase:	DC.L	0
CurrentDir:	DC.L	0
ParmPtr:	DC.L	0		;Command Line Parm Pointer
ParmLen:	DC.L	0		;Command Line Parm Length
ProcessName:	DC.B	"GadGridRunBackground",0
		even

