;*******************************************************************************
; Title:	IfThereIs
; Author:	Dave Lawrence
;		Converted to ObjAsm source code by Philip Ludlam
; Copyright:	(C) Dave Lawrence 2002
; Version:	1.01, 7 May 2006
;
; Changes:	Updated for the A9home by Philip Ludlam
;
;*******************************************************************************
; This program 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 of the License, or (at your option)
; any later version.
;
; This program is distributed in the hope that it will be useful, but WITHOUT
; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
; FITNESS FOR APARTICULAR PURPOSE. See the GNU General Public License for more
; details.
;
; You should have received a copy of the GNU General Public License along with
; this program; if not, write to the Free Software Foundation, Inc., 59 Temple
; Place - Suite 330, Boston, MA 02111-1307, USA
;
;*******************************************************************************


		TTL	> IfThereIs

		GET	OSLib:oslib.hdr.OS
		GET	OSLib:oslib.hdr.OSFile
		GET	AsmLib2:hdr.RegsBoth

arg_name	*	&0
arg_command	*	&4
arg_yes		*	&8
arg_no		*	&C
arg_not		*	&10
arg_file	*	&14
arg_dir		*	&18
arg_app		*	&1C
arg_image	*	&20
arg_x		*	&24

		AREA	|main|, CODE, READONLY

		ENTRY

;-------------------------------------------------------------------------------

Start		B	Entry			; Entry instruction
		DCD	&79766748		; Magic value 1
		DCD	&216C6776		; Magic value 2
		DCD	End-Start		; Read only size
		DCD	0			; Read/write size
		DCD	32			; 26 or 32 bit build

Entry		MOV	r11, lr			; Store lr in r11

		ADR	r0, args
		MOV	r2, wp
		MOV	r3, #1024
		SWI	XOS_ReadArgs
		BVS	error
		RSB	r3, r3, #1024
		ADD	r8, wp, r3

		LDR	r0, [wp, #arg_name]
		CMP	r0, #0
		LDRNE	r0, [wp, #arg_command]
		CMPNE	r0, #0
		ADREQ	r0, syntax
		BEQ	error

						; r7 = object type or 0 = any

		MOV	r7, #0
		LDR	r0, [wp, #arg_file]
		CMP	r0, #0
		MOVNE	r7, #1
		LDR	r0, [wp, #arg_dir]
		CMP	r0, #0
		MOVNE	r7, #2
		MOVNE	r9, #&1000
		LDR	r0, [wp, #arg_app]
		CMP	r0, #0
		MOVNE	r7, #2
		MOVNE	r9, #&2000
		LDR	r0, [wp, #arg_image]
		CMP	r0, #0
		MOVNE	r7, #3

		MOV	r0, #20
		LDR	r1, [wp, #arg_name]
		SWI	XOS_File
		BVC	no_error

		LDR	r1, [wp, #arg_x]
		CMP	r1, #0
		BNE	do_no
		B	error

no_error
		CMP	r0, #0
		BEQ	do_no
		CMP	r7, #0
		BEQ	do_yes
		CMP	r0, r7
		BNE	do_no
		CMP	r0, #2
		BNE	do_yes
		CMP	r6, r9
		BNE	do_no

do_yes
		LDR	r0, [wp, #arg_not]
		CMP	r0, #0
		BNE	really_no

really_yes
		ADD	r0, wp, #arg_command
		LDMIA	r0, {r5, r6, r7}
		TEQ	r7, #0
		MOVEQ	r6, #0
		B	do_command

do_no
		LDR	r0, [wp, #arg_not]
		CMP	r0, #0
		BNE	really_yes

really_no
		ADD	r0, wp, #arg_command
		LDMIA	r0, {r5, r6, r7}
		TEQ	r6, #0
		TEQNE	r7, #0
		MOVNE	r6, r7
		BNE	do_command
		TEQ	r6, #0
		BEQ	exit
		MOV	r5, r6
		MOV	r6, #0

do_command
		MOV	r1, r8
		MOV	r0, r5
		MOV	r2, #32
		BL	copy
		MOVS	r0, r6
		MOV	r2, #0
		BLNE	copy
		STREQB	r2, [r1, #-1]

os_cli
		MOV	r0, r8
		SWI	XOS_CLI
		BVS	error

exit
		MOV	pc, r11

error
		SetV
		MOV	pc, r11

copy
		LDRB	r3, [r0], #1
		STRB	r3, [r1], #1
		CMP	r3, #32
		BGE	copy
		STRB	r2, [r1, #-1]
		MOV	pc, lr

syntax
		DCD	220
		DCB	"Syntax: *IfThereIs [-Not] [ -File | -Dir | -App | -Image ] <object>             <yes command> [-x] [<no command>] | <command> <yes param> <no param>"
		DCB	0
		ALIGN
args
		DCB	",,,,not/s,file/s,dir/s,app/s,image/s,x/s"
		DCB	0

licence		DCB	" dL, 1993. Licence: GPL."
		ALIGN

End
		END
