 		incdir "includes:"


	INCLUDE exec/execbase.I
	IFND    EXEC_TYPES_I
	INCLUDE exec/types.i
	ENDC

	IFND EXEC_NODES_I		
	include "exec/nodes.i"		
	ENDC
	IFND EXEC_MEMORY_I
	include "exec/memory.i"
	ENDC

	IFND LIBRARIES_DOS_I
	include "libraries/dos.i"
	ENDC
	IFND LIBRARIES_DOSEXTENS_I
	include "libraries/dosextens.i"
	ENDC
	
	IFND LIBRARIES_DOS_LIB_I
	include	"libraries/dos_lib.i"
	ENDC
	
	IFND EXEC_EXEC_LIB_I
	include "exec/exec_lib.i"
	ENDC


	include	"asm:datamaster.library/datamaster.i"
	include	"asm:datamaster.library/datamaster_lib.i"
	include	"asm:datamaster.library/bundle.i"


CALLBASE:	MACRO
	jsr	_LVO\1(a6)
		ENDM

JMPBASE:	MACRO
	jmp	_LVO\1(a6)
		ENDM

NORMAL:		MACRO
	dc.b $9b,"0;31;40m"
	ENDM
		
NOIRGRAS:	MACRO
	dc.b $9b,"1;31;40m"
	ENDM
BLANCGRAS:	MACRO
	dc.b $9b,"1;32;40m"
	ENDM

NOIRSOUL:	MACRO
	dc.b $9b,"4;31;40m"
	ENDM
BLANCSOUL:	MACRO
	dc.b $9b,"4;32;40m"
	ENDM

NOIRITAL:	MACRO
	dc.b $9b,"3;31;40m"
	ENDM


PUSH:	MACRO
	movem.l	a0-a6/d0-d7,-(sp)
	ENDM

POP:	MACRO
	movem.l	(sp)+,a0-a6/d0-d7
	ENDM

;TESTING=1


	section	DMControl,code


run:	bsr.w	OpenLibs
	beq.w	_nolibs

	move.l	_DatamasterBase,a5
	move.l	dmb_DosBase(a5),_DOSBase

	CALLDOS Output
	move.l	d0,Output

	IFND	TESTING
	bsr	do_ReadArgs

	move.l	_S,d0
	cmp.l	#5000,d0
	beq.s	.nochg
	move.l	_S,a0
	move.l	(a0),_S
.nochg:
	move.l	_TI,d0		;Recognizer Info (opt:all)
	bne.w	do_TI

	move.l	_FI,d0		;File Info
	bne.w	do_FI

	move.l	_SD,d0		;Scan Dir (opt=*/name)
	bne.w	do_SD

	move.l	_AD,d0		;Add Dir (opt=*/name)
	bne.w	do_AD

	move.l	_RT,d0		;Remtype (opt=all)
	bne.w	do_RT

	move.l	_AT,d0		;Addtype=>possible multiple
	bne.w	do_AT

	move.l	_AF,d0		;Add RecoFile =>1 only
	bne.w	do_AF

	move.l	_TFS,d0		;Testfile (uses also XPK & SIZE as parameters)
	bne.w	do_TF

	move.l	_CB,d0		;CreateBundle
	bne.w	do_CB

	move.l	_L,d0		;LIST (last coz' can be an OPTION in fact
	bne.w	do_L

	lea	Intro,a0
	bsr	_Write

	ENDC

	IFD TESTING
	move.l	#.hehe,_TI
	bra	do_TI

.hehe:	dc.b	"music-ptkclones",0

	ENDC

OUT:	move.l	myRecoList,d0
	beq.s	.norl
	move.l	d0,a0
	CALLDM	dmFreeRecoList
.norl:	bsr	do_FreeArgs
	bsr	CloseLibs
_nolibs:moveq	#0,d0
	rts

PREOUT:	move.l	_L,d0
	bne.s	do_L
	bra	OUT

version_string:
	dc.b	'$VER: Datamaster_Control 1.9 (05.05.97) Release version  © Alexis "Cyb" Nasr',0
	even
;--------------------------------------------------------------------------
;				Sub branches
;--------------------------------------------------------------------------

;List
;********
do_L:
	CALLDM dmLockPrefs
	CALLDM dmQueryRecoNamesNew
	tst.l	d0
	beq	do_ERROR
	move.l	d0,myRecoList
	lea	Ftypes,a0
	lea	PC_Array,a1
	moveq	#0,d0
	move.l	myRecoList,a2
	move.w	rl_NumRecos(a2),d0
	move.l	d0,(a1)+
	bsr	WRITEC

	lea	bwhite,a0
	bsr	_Write

	lea	rl_RecoList(a2),a4
.loop:	move.l	(a4)+,d0
	bne.s	.ok

	lea	norm,a0
	bsr	_Write

	CALLDM dmUnLockPrefs
	BRA	OUT
.ok:	move.l	d0,a0
	bsr	_Write

	lea	return,a0
	bsr	_Write
	bra.s	.loop
	

;Reco info
;**********
do_TI:	clr.l	subtotal

	CALLDM dmLockPrefs
	move.l	_TI,a0
	lea	alltxt,a1
	CALLDM	dmCmpStringNC
	tst.l	d0
	beq.s	.all

	move.l	_TI,a0
	sub.l	a1,a1
	CALLDM	dmGetRecoAddr
	tst.l	d0
	beq	do_NOTFOUNDulock

.another:
	move.l	d0,a0
	bsr	PrintUnit

	move.l	_TI,a0
	move.l	d0,a1
	CALLDM	dmGetRecoAddr
	tst.l	d0
	bne.s	.another

	CALLDM dmUnLockPrefs
	bra	OUT

.all:	CALLDM dmQueryRecoListNew
	tst.l	d0
	beq	do_ERROR
	move.l	d0,myRecoList
	lea	reconbs,a0
	lea	PC_Array,a1
	moveq	#0,d0
	move.l	myRecoList,a2
	move.w	rl_NumRecos(a2),d0
	move.l	d0,(a1)+
	bsr	WRITEC

	lea	rl_RecoList(a2),a4
.loop:	move.l	(a4)+,d0
	bne.s	.ok

	lea	PC_Array,a1
	move.l	myRecoList,a2
	move.w	rl_NumRecos(a2),d0
	move.l	d0,(a1)+
	move.l	subtotal,(a1)
	lea	StatsTxt,a0
	bsr	WRITEC
	CALLDM dmUnLockPrefs
	BRA	OUT
.ok:	move.l	d0,a0
	bsr	PrintUnit
	move.l	nbsub,d0
	add.l	d0,subtotal
	bra.s	.loop

subtotal:
	dc.l	0
nbsub:	dc.l	0	;number of subrecos in current major-filetype
	

;File info
;**********
do_FI:	move.l	_FI,a0
	CALLDM	dmReadRecoFile
	tst.l	d0
	beq	do_NOTFOUND
	move.l	d0,-(sp)
	lea	FileOutput,a0
	lea	PC_Array,a1
	move.l	_FI,(a1)
	bsr	WRITEC
	move.l	(sp)+,a0
	bsr	PrintUnit

	CALLDM	dmFreeRecoFile
	bra	OUT


;scandir
;**********
do_SD:
	move.l	_SD,a0
	move.b	(a0),d0
	cmp.b	#"/",d0
	bne.s	.ok
	sub.l	a0,a0
.ok:	CALLDM	dmScanRecoDir
	bra	PREOUT

;AddDir
;**********
do_AD:
	move.l	_AD,a0
	move.b	(a0),d0
	cmp.b	#"/",d0
	bne.s	.ok
	sub.l	a0,a0
.ok:	CALLDM	dmAddRecoDir
	bra	PREOUT


;REMTYPE
;**********
do_RT:	move.l	_RT,a0
	CALLDM	dmRemRecoName
	bra	PREOUT

;AddRecoFile
;**********
do_AF:	move.l	_AF,a0
	CALLDM	dmAddRecoFile
	bra	PREOUT


;AddRecoName:
;**********
do_AT:	move.l	_AT,a0
	CALLDM	dmAddRecoName
	bra	PREOUT

;TestFile
;**********
do_TF:	move.l	_TFS,a0
.loo:	move.l	(a0)+,d0
	beq.w	OUT
	move.l	d0,_TF
	move.l	a0,-(sp)
	bsr.s	.do
	move.l	(sp)+,a0
	bra.s	.loo

.do:	move.l	_TF,a0							;name
	move.l	#RECOF_CheckPatt,d1
	move.l	_NOXPK,d0
	bne.s	.norm
.xpk:	add.l	#RECOF_XPKLoad,d1					;flags
.norm:	move.l	_NC,d0
	bne.s	.norm2
	add.l	#RECOF_CheckData,d1					;DATAcheck
.norm2:	move.l	_S,d0							;size
	CALLDM	dmTestFile
	move.l	d0,.handle
	move.l	d0,a0
	move.l	dmh_String(a0),d0
.ok:	move.l	_LN,d1
	bne.s	.long
	move.l	_TF,d1
	movem.l	d0/a0,-(sp)
	move.l	dmb_DosBase(a5),a6
	CALLBASE FilePart	
	lea	PC_Array,a1
	move.l	d0,(a1)+
	movem.l	(sp)+,d0/a0
	bra.s	.short
.long:	lea	PC_Array,a1
	move.l	_TF,(a1)+
.short:	tst.l	.handle
	beq.s	.retzero
	move.l	dmh_SubType(a0),d1
	beq.s	.nosub
.sub:	move.l	d1,(a1)+
	lea	Ftypetxt2,a0
	bra.s	.cont
.nosub:	lea	Ftypetxt,a0
.cont:	move.l	d0,(a1)+
	bsr	WRITEC

	move.l	.handle,a0
	JMPDM	dmFreeDMHandle
	
	
.retzero:
	lea	Ferrtxt,a0
	bra	WRITEC
	even
.handle:dc.l	0




;------------------- Some "heavy" routines :) -----------------------

;CreateBundle
;**********
do_CB:
	move.l	_CB,a0
	move.b	(a0),d0
	cmp.b	#"/",d0
	bne.s	.ok
	lea	recodirname,a0

.ok:	move.l	_DatamasterBase,a5
	bsr	DoCurrDir
	tst.l	d0
	bne.w	.lokok
	rts
.lokok:	move.l	d1,olddir
	move.l	d0,d4	

	move.l	a0,-(sp)
	lea	bundleinfo,a0
	bsr	_Write
	move.l	(sp)+,a0

	bsr	LoadFiles
	bsr	CreateBundle
	bsr	FreeFiles
	bsr	Extro
	lea	bundleinfo2,a0
	tst.l	bundle_header+bdl_nbrecos
	bne.s	.donet
	lea	bundleerrtxt,a0
.donet:	bsr	_Write
	bra	OUT

LoadFiles:
	bsr	DoGetFIB		;get info block
	tst.l	d0
	beq.s	.nofib
	move.l	d0,a4

	lea	loadseg_buff,a2
	lea	bundle_buff,a3

	bsr.s	.do_it			;do the job...

	move.l	a4,a0
	bsr	DoFreeFIB
.nofib:	rts

.do_it:	
.verify:move.l	fib_EntryType(a4),d0
	bmi.w	.file

.cont:	move.l	d4,d1
	move.l	a4,d2
	move.l	dmb_DosBase(a5),a6
	CALLBASE ExNext
	tst.l	d0
	bne.s	.verify
	lea	bundle_header,a0
	lea	4(a3),a3
	sub.l	a0,a3
	move.l	a3,bundle_header+bdl_offset
	rts
	
.file:	lea	fib_FileName(a4),a0	;test passed:Ok it's a file ;-)
	move.l	a0,d1
	move.l	dmb_DosBase(a5),a6
	CALLBASE LoadSeg		;loadseg the recognizer
	tst.l	d0
	beq.s	.cont
	bsr	TestReco		;verify if valid
	move.l	d0,-(sp)
	move.l	d2,d1
	bsr	MyUnloadSeg		;anyway, free the loadseg
	move.l	(sp)+,d0
	tst.l	d0
	beq.s	.cont

	lea	fib_FileName(a4),a0
	bsr	LoadFile		;if it was valid, LOAD file.
	tst.l	FileAdr
	beq.s	.cont
	add.l	#1,bundle_header+bdl_nbrecos
	move.l	FileAdr,(a2)+
	move.l	FileSize,(a3)+
	bra.s	.cont
	even
nbrecos:dc.l	0
copy_flag:
	dc.l	0


Extro:				;will be used a extro() of all reco ADD routines.
	move.l	d4,d1		;free current lock
	move.l	dmb_DosBase(a5),a6
	CALLBASE UnLock

	move.l	olddir,d1	;in SP:old currdir; in d0:results
	move.l	d0,-(sp)	;resultd0
	move.l	d1,d0
	bsr	DoCurrLock
	move.l	(sp)+,d0
	rts
olddir:	dc.l	0

FreeFiles:
	lea	loadseg_buff,a2
	lea	bundle_buff,a3

	move.l	(a2)+,d0
	beq.s	.end
	move.l	d0,a1
	move.l	(a3)+,d0
	CALLEXEC FreeMem
.end:	rts


CreateBundle:
	tst.l	bundle_header+bdl_nbrecos
	bne.s	.ok
	rts
.ok:	lea	bundle_header,a0
	move.l	#"RECO",bdl_id1(a0)
	move.l	#"BNDL",bdl_id2(a0)

	lea	BundleName,a0			;dop_a0
	move.l	a0,d1
	move.l	#MODE_NEWFILE,d2
	move.l	dmb_DosBase(a5),a6
	CALLBASE Open
	tst.l	d0
	bne.s	.diskok
	rts
.diskok:move.l	d0,d7
					;WRITE HEADER FIRST
	move.l	#bundle_header,d2
	move.l	bundle_header+bdl_offset,d3
	move.l	d7,d1			;handle
	CALLBASE Write

					;THEN ADD ALL FILES
	lea	loadseg_buff,a2
	lea	bundle_buff,a3

.cont:	move.l	(a2)+,d2		;adr
	beq.s	.done
	move.l	(a3)+,d3		;size
	move.l	d7,d1			;handle
	CALLBASE Write
	bra.s	.cont
	
.done:	move.l	d7,d1
	JMPBASE Close




TestReco:
	lsl.l	#2,d0			;NOW it's an address
	move.l	d0,d2
	move.l	d0,a0
	bsr	GetTags			;adr of file>tags
	tst.l	d0
	rts


DoCurrDir:				;IN:a0=dirname (if 0=>defaultdir) =>make it currentdir
	move.l	a0,d1			;OUT:d0=Currlock,d1=oldlock
	bne.s	.ok
	move.l	#recodirname,d1
.ok:	move.l	#ACCESS_READ,d2
	move.l	dmb_DosBase(a5),a6
	CALLBASE Lock
	tst.l	d0
	bne.s	DoCurrLock
	moveq	#0,d0
	rts



DoCurrLock:				;IN:d0=lock =>make it currentdir
	move.l	d0,-(sp)		;returns d1=old lock (& d0=preserved)
	move.l	d0,d1
	move.l	dmb_DosBase(a5),a6
	CALLBASE CurrentDir
	move.l	d0,d1			;oldlock
	move.l	(sp)+,d0		;currlock
	rts

MyUnloadSeg:
	lsr.l	#2,d1		;divide back by 4 as *I* use addresses,not these lame BCPL pointers
	move.l	dmb_DosBase(a5),a6
	JMPBASE UnLoadSeg


DoGetFIB:				;IN:d4=lock =>allocates FIB & examines first entry
					;OUT:d0=a4=FIB or 0 if error :-)
	move.l	#fib_SIZEOF,d0
	move.l	#MEMF_ANY+MEMF_PUBLIC+MEMF_CLEAR,d1
	move.l	4.w,a6
	CALLBASE AllocMem
	tst.l	d0
	bne.s	.fibok
	rts
.fibok:	move.l	d0,a4			;a4=FILEINFOBLOCK
	move.l	d0,d2
	move.l	d4,d1
	move.l	dmb_DosBase(a5),a6
	CALLBASE Examine
	tst.l	d0
	bne.s	.ok
	move.l	a4,a0
	bsr	DoFreeFIB
.ok:	move.l	a4,d0
	rts



DoFreeFIB:				;a0=FIB =>frees it
	move.l	a0,a1
	move.l	#fib_SIZEOF,d0
	move.l	4.w,a6
	JMPBASE FreeMem


;--------------------
LoadFile:			;a0
;--------------------
	clr.l	FileAdr
	clr.l	FileSize
	bsr.w	.getlen
	tst.l	d0			;file not found=>err!
	beq	.fileerr
	move.l	d0,FileSize

	move.l	#MEMF_PUBLIC+MEMF_CLEAR,d1
	CALLEXEC AllocMem
	tst.l	d0
	beq.s	.memerr

	move.l	d0,FileAdr
	bsr	.load
	tst.l	d0
	bne.s	.done

	move.l	FileAdr,a1
	move.l	FileSize,d0
	CALLEXEC FreeMem
	clr.l	FileAdr
.fileerr:
.done:	rts
				;if error,we may close the file
.memerr:move.l	d6,d1
	move.l	dmb_DosBase(a5),a6
	JMPBASE Close

.getlen:			;get file length (IN:a0=name/OUT:d0=size)
	move.l	a0,d1			;name
	move.l	#MODE_OLDFILE,d2
	move.l	dmb_DosBase(a5),a6
	CALLBASE Open
	tst.l	d0
	bne.s	.nozero
	rts
.nozero:move.l	d0,d6			;d6=filehandle

	move.l	d6,d1
	moveq	#0,d2
	move.l	#OFFSET_END,d3

.seek:	movem.l	d1-d3,-(sp)
	move.l	dmb_DosBase(a5),a6
	CALLBASE Seek
	movem.l	(sp)+,d1-d3
	JMPBASE Seek

.load:	move.l	d6,d1
	moveq	#0,d2
	move.l	#OFFSET_BEGINNING,d3	;go back at the beginning!! :-)
	bsr	.seek

	move.l	d6,d1			;handle
	move.l	FileAdr,d2		;adr
	move.l	FileSize,d3		;size

	move.l	dmb_DosBase(a5),a6
	CALLBASE Read
	move.l	d0,-(sp)
	move.l	d6,d1
	CALLBASE Close
	move.l	(sp)+,d0		;result
	rts

FileAdr:dc.l	0
FileSize:dc.l	0


GetTags:			;a0=MySegAdr=>d0&a0=recotaglist or zero if error

	movem.l	a1-a2/d1-d2,-(sp)
	move.l	a0,a2		;set scanned data adr & size
	move.l	#200,d2

	move.l	#recdat,a1
	move.l	#-1,d0
	CALLDM	dmFindTextC
	tst.l	d0
	bne.s	.err
	move.l	8(a0),a0	;get the value of RECODATA,xx.l   <---
	move.l	a0,d0
	bra.s	.ok
.err:	moveq	#0,d0
.ok:	movem.l	(sp)+,a1-a2/d1-d2
	rts
	
recdat:	dc.b	"DATARECO",0
	even


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


	
do_NOTFOUNDulock:
	CALLDM dmUnLockPrefs
do_NOTFOUND:
	lea	NotFoundTxt,a0
	bsr	_Write
	bra	OUT


do_ERRORulock:
	CALLDM dmUnLockPrefs
do_ERROR:
	lea	miscerrtxt,a0
	bsr	_Write
	bra	OUT


PrintUnit:			;a0=Unitadr (segment)	;SAVES ALL REGS
	movem.l	a0-a6/d0-d7,-(sp)
	bsr.s	.do
	movem.l	(sp)+,a0-a6/d0-d7
	rts
.do:	clr.l	nbsub
	move.l	a0,a4		;BUFF
	lea	PC_Array,a3

	move.l	a4,a0
	move.l	#DMR_Name,d0
	CALLDM	dmGetDMR_Tag
	move.l	d0,(a3)+	;name

	move.l	a4,a0
	move.l	#DMR_Version,d0
	CALLDM	dmGetDMR_Tag
	move.l	d0,d1
	swap	d0
	and.l	#$ffff,d0
	move.l	d0,(a3)+	;ver
	and.l	#$ffff,d1
	move.l	d1,(a3)+	;rev

	move.l	a4,a0
	move.l	#DMR_Pri,d0
	CALLDM	dmGetDMR_Tag
	move.l	d0,(a3)+	;pri

	bsr	.getpritxt
	move.l	a0,(a3)+	;pritxt

	bsr	.getclass	;class
	move.l	a0,(a3)+

	move.l	a4,a0
	move.l	#DMR_Descr,d0
	CALLDM	dmGetDMR_Tag
	move.l	d0,(a3)+	;descr

	move.l	a4,a0
	move.l	#DMR_Flags,d0
	CALLDM	dmGetDMR_Tag


	move.l	#RECOB_CheckData,d1
	lea	checkdatatxt,a0
	bsr	.insert

	move.l	#RECOB_CheckPatt,d1
	lea	checkpatttxt,a0
	bsr	.insert

	move.l	#RECOB_SubType,d1
	lea	subtypetxt,a0
	bsr	.insert

	move.l	#RECOB_Disabled,d1
	lea	disabledtxt,a0
	bsr	.insert

	move.l	#RECOB_Internal,d1
	lea	internaltxt,a0
	bsr	.insert

	move.l	#RECOB_DupString,d1
	lea	dupstringtxt,a0
	bsr	.insert

	lea	UnitOutput,a0
	bsr	WRITEC

	move.l	a4,a0	
	move.l	#DMR_HookName,d0
	CALLDM	dmGetDMR_Tag
	tst.l	d0
	beq.s	.nohook
	move.l	d0,PC_Array
	lea	HookTxt,a0
	bsr	WRITEC

.nohook:
	move.l	a4,a0	
	move.l	#DMR_SubTypesTable,d0
	CALLDM	dmGetDMR_Tag
	tst.l	d0
	bne	.cpsl

.patt:	move.l	a4,a0			;pattern printing
	move.l	#DMR_Pattern,d0
	CALLDM	dmGetDMR_Tag
	tst.l	d0
	bne.s	.patton
	rts
.patton:lea	PattTxt0,a0		;header
	bsr	_Write

	move.l	d0,a4
.loop:	move.l	a4,PC_Array
	lea	PattTxt,a0
	bsr	WRITEC
.seekZ:	tst.b	(a4)+
	bne.s	.seekZ
	move.b	(a4),d0
	cmp.b	#$ff,d0
	bne.s	.loop
	lea	return,a0
	bra	_Write


;.csl:	move.l	d0,a3
;	move.l	#8,d2		;"size of a array line" (here LONG,LONG =8)
;	move.l	#4,d3		;"name offset"
;	bsr	.intro
;		
;.csll:	move.l	4(a3),d0
;	beq.s	.done
;	bsr	.PrintName
;	move.l	(a3),d0
;	beq.s	.done
;	bsr	.PrintData
;	bsr	.return
;	lea	8(a3),a3
;	bra.s	.csll	
.done:	move.l	.copyadr,d0
	beq.s	.done2
	move.l	d0,a1
	move.l	.copysize,d0
	CALLEXEC FreeMem
.done2:	rts

;
;
;.psl:	move.l	d0,a3
;	move.l	#8,d2		;"size of a array line" (here LONG,LONG =8)
;	move.l	#4,d3		;"name offset"
;	bsr	.intro
;.psll:	move.l	4(a3),d0
;	beq.s	.done
;	bsr	.PrintName
;	move.l	(a3),d0
;	beq.s	.done
;	bsr	.PrintPatt
;	bsr	.return
;	lea	8(a3),a3
;	bra.s	.psll	


.cpsl:	move.l	d0,a3
	move.l	#12,d2		;"size of a array line" (here LONG,LONG,LONG =12)
	move.l	#8,d3		;"name offset"
	bsr	.intro
.cpsll:	move.l	8(a3),d0
	beq.s	.done
	bsr	.PrintName
	move.l	(a3),d0
	beq.s	.done
	bsr	.PrintData
	move.l	4(a3),d0
	beq.s	.done
	bsr	.PrintPatt
	bsr	.return
	lea	12(a3),a3
	bra.s	.cpsll	


.PrintName:
	movem.l	a0/d0,-(sp)
	move.l	d0,a0
	CALLDM dmCountString
	move.l	d0,.ssize
	movem.l	(sp)+,a0/d0
	lea	STypeName,a0
	bsr.s	.com
	move.l	#33,d0
	sub.l	.ssize,d0
	bmi.s	.enough
	subq	#1,d0
	lea	Dummy_Buff,a0
.bof:	move.b	#" ",(a0)+
	dbra	d0,.bof
	clr.b	(a0)
	lea	Dummy_Buff,a0
	bsr	_Write
.enough:rts

.PrintData:
	cmp.l	#-1,d0
	beq.s	.pdn
	move.l	#DataY,d0
	bra.s	.pdd
.pdn:	move.l	#DataN,d0
.pdd:
	lea	STypeData,a0
.com:	move.l	d0,PC_Array
	bra	WRITEC


.PrintPatt:
	cmp.l	#-1,d0
	bne.s	.ppn
	lea	PattN,a0
	bra	_Write
.ppn:	lea	STypePatt,a0
	bsr	_Write

	move.l	d0,a4
.loop2:	move.l	a4,PC_Array
	lea	PattTxt,a0
	bsr	WRITEC
.seekZ2:tst.b	(a4)+
	bne.s	.seekZ2
	move.b	(a4),d0
	cmp.b	#$ff,d0
	bne.s	.loop2
	rts


;prints junk & also counts recos
.intro:
	clr.l	.copyadr
	clr.l	.copysize
	move.l	a3,a0
	add.l	d3,a0
	moveq	#0,d1
.iloop:	move.l	(a0),d0
	beq.s	.idone
	lea	(a0,d2),a0
	addq	#1,d1
	bra.s	.iloop
.idone:	move.l	d1,nbsub
	cmp.l	#2,d1
	blo.s	.useold
.ialloc:
	addq	#1,d1
	mulu	d2,d1
	move.l	d1,d0
	move.l	d0,.copysize
	move.l	#MEMF_CLEAR+MEMF_PUBLIC,d1
	CALLEXEC AllocMem
	tst.l	d0
	beq.s	.useold
	move.l	d0,.copyadr

	move.l	a3,a0
	move.l	.copyadr,a1
	move.l	.copysize,d0
	CALLEXEC CopyMem

	move.l	.copyadr,a3
	bsr	SORTALPHA	;a3=list , d2=linesize , d3=nameoffset

.useold:lea	Stypes,a0		;header
	bra	_Write


.return:lea	return,a0
	bra	_Write

.insert:
	btst	d1,d0
	bne.s	.on
	lea	nothing,a0
.on:	move.l	a0,(a3)+
	rts

.getclass:
	move.l	a4,a0
	move.l	#DMR_Class,d0
	CALLDM	dmGetDMR_Tag

	lea	class_GRAPHICS,a0
	btst	#DMCLASSB_GRAPHICS,d0
	bne.s	.classdone

	lea	class_ANIMATION,a0
	btst	#DMCLASSB_ANIMATION,d0
	bne.s	.classdone

	lea	class_MUSIC,a0
	btst	#DMCLASSB_MUSIC,d0
	bne.s	.classdone

	lea	class_SAMPLE,a0
	btst	#DMCLASSB_SAMPLE,d0
	bne.s	.classdone

	lea	class_PACKED,a0
	btst	#DMCLASSB_PACKED,d0
	bne.s	.classdone

	lea	class_SYSTEM,a0
	btst	#DMCLASSB_SYSTEM,d0
	bne.s	.classdone

	lea	class_MISC,a0
	btst	#DMCLASSB_MISC,d0
	bne.s	.classdone

	lea	class_LINK,a0
	btst	#DMCLASSB_LINK,d0
	bne.s	.classdone

	lea	class_ASCII,a0
	btst	#DMCLASSB_ASCII,d0
	bne.s	.classdone

	lea	class_UNKNOWN,a0
.classdone:
	rts


.getpritxt:
	lea	Pri_FREQUENT,a0
	cmp.l	#PRI_FREQUENT,d0
	beq.s	.pridone

	lea	Pri_SINGLE,a0
	cmp.l	#PRI_SINGLE,d0
	beq.s	.pridone

	lea	Pri_GROUPED,a0
	cmp.l	#PRI_GROUPED,d0
	beq.s	.pridone

	lea	Pri_GROUPEDSLOW,a0
	cmp.l	#PRI_GROUPEDSLOW,d0
	beq.s	.pridone

	lea	Pri_RARE,a0
	cmp.l	#PRI_RARE,d0
	beq.s	.pridone

	lea	Pri_HOOKNAME,a0
	cmp.l	#PRI_HOOKNAME,d0
	beq.s	.pridone

	lea	Pri_CUSTOM,a0
.pridone:
	rts




.copysize:
	dc.l	0	;list size
.copyadr:
	dc.l	0	;list adr

.ssize:	dc.l	0


;oldroutine, no use of datamaster :-) sorry eh...
SORTALPHA:
	PUSH
	move.l	d2,.addval
	move.l	d3,.offset
	add.l	d3,a3
	move.l	a3,a4
	bsr.w	.do
	POP
	rts
.do:	move.l	a4,d6		;CACHE a4=buffer beginning.
.reloop:moveq	#0,d5		;permutation flag
	move.l	d6,a4
.next:	move.l	(a4),a1		;string1
	add.l	.addval,a4
	move.l	(a4),d0
	beq.w	.rescan
	move.l	d0,a0		;string2
				;NOW,compare strings
.aloop:	move.b	(a0)+,d3
	move.b	(a1)+,d2	

	tst.b	d2		;1&2 same but 1 shorter than 2 =>ok
	beq.w	.next
	tst.b	d3		;1&2 same but 1 longer than 2=>permute
	beq.w	.permute

	cmp.b	#"a",d2		;transform to UPPERCASE if necessary
	bhs.w	.min1
.max1:	cmp.b	#"a",d3
	bhs.w	.min2
.max2:	cmp.b	d2,d3
	beq.w	.aloop		;1=2=>loop on name
	blo.w	.permute	;1>2=>permute
	bra.w	.next		;else 1<2 (ok)

.min1:	sub.b   #97-65,d2
	bra.w    .max1
.min2:	sub.b   #97-65,d3
	bra.w    .max2

.permute:
	move.l	a4,-(sp)
	moveq	#1,d5		;exchange pointers

	bsr	.doperm		;NAME

	move.l	.addval,d2	;now Pattern and/or datacheck
.permr:
	lea	-4(a4),a4	
	subq	#4,d2
	beq.s	.permf
	bsr	.doperm
	bra.s	.permr

.permf:	move.l	(sp)+,a4
	bra.w	.next

.doperm:move.l	a4,a2
	sub.l	.addval,a2
	move.l	(a4),d0
	move.l	(a2),(a4)
	move.l	d0,(a2)
	rts

.rescan:tst.l	d5		;ZERO permutation done last time?
	bne.w	.reloop		;nope...not finished yet
	moveq	#1,d0		;flag=OK
	rts			;done

.addval:dc.l	0		;d2
.offset:dc.l	0		;d3


_Write:				;a0=string (PRESERVES *ALL* REGS)
	movem.l	a0-a6/d0-d7,-(sp)
	bsr.s	.do
	movem.l	(sp)+,a0-a6/d0-d7
	rts
.do:	move.l	a0,a1
.loop:	move.b	(a1)+,d0
	bne.s	.loop
	subq	#1,a1
	sub.l	a0,a1
	move.l	a1,d3		;length
	move.l	a0,d2		;beginning
	move.l	Output,d1
	IFND	TESTING
	CALLDOS	Write
	ENDC
	rts
;----------------------------------------------------------------------------
;				Misc/Startup routines
;----------------------------------------------------------------------------
do_ReadArgs:
	move.l	#args_template,d1
	move.l	#Myargs,d2
	moveq	#0,d3
	CALLDOS	ReadArgs
	move.l	d0,ArrayAdr
	rts

do_FreeArgs:
	move.l	ArrayAdr,d1
	CALLDOS	FreeArgs
	rts

OpenLibs:
	lea	DM_name,a1
	moveq	#2,d0
	CALLEXEC OpenLibrary
	move.l	d0,_DatamasterBase
	rts

CloseLibs:
	move.l	_DatamasterBase,a1
	move.l	a1,d0
	bne.s	.do
	rts
.do:	CALLEXEC CloseLibrary
	rts

ArrayAdr:
	dc.l	0

myRecoList:
	dc.l	0

Myargs:
_L:	dc.l	0	;dc.b	"L=LIST/K/S,"
_RT:	dc.l	0	;dc.b	"RR=REMTYPE/K,"		;remove a recognizer (in mem)
_AT:	dc.l	0	;dc.b	"AR=ADDTYPE/K,"		;add (multiple) files [ex:DUMMY=>DUMMY.patt+DUMMY.data+DUMMY.myreco etc]
_AF:	dc.l	0	;dc.b	"AF=ADDFILE/K,"		;add one file [ex:DUMMY.patt]
_AD:	dc.l	0	;dc.b	"AD=ADDDIR/K,"		;add dir
_SD:	dc.l	0	;dc.b	"SD=SETDIR/K,"		;same but frees previous recos before
_TFS:	dc.l	0	;dc.b	"TF=TESTFILE/K,"	;testfile()
_NOXPK:	dc.l	0	;dc.b	"NOXPK/K/S,"		;switch for testfile
_S:	dc.l	5000	;dc.b	"SIZE/K/N,"		;size for tesfile
_NC:	dc.l	0	;dc.b	"NC=NODATACHECK/K/S,"	;switch
_LN:	dc.l	0	;dc.b	"LN=LONGNAME/K/S"	;switch for short filename
_TI:	dc.l	0	;dc.b	"RI=TYPEINFO/K,"	;info on recognizer (may be ALL)
_FI:	dc.l	0	;dc.b	"FI=FILEINFO/K,"	;same but we'll loadseg it. (no ALL)
_CB:	dc.l	0	;dc.b	"CB=CREATEBUNDLE/K,"
fsize:	dc.l	0

_TF:	dc.l	0	;CURRENT file!! (NOT a ReadArgs param!!)

PRINTC:			;IN:		a0=format string
			;**PRESERVES ALL REGS**

	movem.l	a0-a6/d0-d7,-(sp)
	bsr.s	.do
	movem.l	(sp)+,a0-a6/d0-d7
	rts

.do:	lea	PC_Array,a1
	lea	PC_Buff,a2
.ok:	move.l	a2,a3
	lea	.putch,a2
	CALLEXEC RawDoFmt
	rts
.putch:	move.l	a3,PC_adr
	move.b	d0,(a3)+
	rts


WRITEC:			;constructs stuff (same IN as PRINTC) AND Outputs it.
	bsr	PRINTC
	lea	PC_Buff,a0
	bra	_Write

_DatamasterBase:
	dc.l	0
_DOSBase:
	dc.l	0

			section "stuff",data


DM_name:	dc.b	"datamaster.library",0

FileOutput:
	dc.b	10
	NOIRGRAS
	dc.b	"FILE : "
	BLANCGRAS
	dc.b	">>  %s <<",10
	NORMAL
	dc.b	0
UnitOutput:
	dc.b	10
	BLANCGRAS
	dc.b	"------------------------------------------------------------------",10,10
	NOIRGRAS
	dc.b	"MAJOR-FileType:"
	NORMAL
	BLANCGRAS
	dc.b	"%s"
	NORMAL
	dc.b	" (%ld.%ld),Pri=%ld (='%s')",10
	NOIRGRAS
	dc.b	"Class: "
	NORMAL
	dc.b	"%s",10
	NOIRGRAS
	dc.b	"Description: "
	NORMAL
	dc.b	"%s",10
	NOIRGRAS
	dc.b	"Flags:"
	NORMAL
	dc.b	"%s %s %s %s %s %s",10,10
	dc.b	0

Ftypes:	dc.b	10
	NOIRSOUL
	dc.b	">>%ld<< MAJOR-FileTypes available:",10,10
	NORMAL
	dc.b	0

reconbs:dc.b	10
	NOIRSOUL
	dc.b	">>%ld<< Recognizers in memory",10,10
	NORMAL
	dc.b	0

PattTxt0:
	NOIRSOUL
	dc.b	"Pattern Matches:"
	NORMAL
	dc.b	10,0

PattTxt: 
	dc.b	"'%s' ",0


Stypes:
	NOIRGRAS
	dc.b	"Available Sub-FileTypes:"
	dc.b	"      Data-Check"
	dc.b	"        Pattern-Check",10
	dc.b	"-------------------------------------------------------------"
	NORMAL
	dc.b	10
	dc.b	" |",10,0

STypeName:
	NORMAL
	dc.b	" `-"
	BLANCGRAS
	dc.b	" %s"
	NORMAL
	dc.b	0

STypeData:
	dc.b	"%s",0


STypePatt:
	dc.b	"",0

StatsTxt:
	dc.b	10,"Major-FileTypes: %ld , Total Nb of SubTypes: %ld",10,0


return:	dc.b	10
nothing:dc.b	0

internaltxt:
	dc.b	"INTERNAL",0
checkdatatxt:
	dc.b	"CHECKDATA",0
checkpatttxt:
	dc.b	"CHECKPATTERN",0	
disabledtxt:
	dc.b	"DISABLED",0
subtypetxt:
	dc.b	"SUBTYPE",0
dupstringtxt:
	dc.b	"DUPSTRING",0


alltxt:	dc.b	"All",0
NotFoundTxt:
	BLANCGRAS
	dc.b	"Unknown Recognizer/file!"
	NORMAL
	dc.b	10,10,0

miscerrtxt:
	BLANCGRAS
	dc.b	"Sorry... error!"
	NORMAL
	dc.b	10,10,0


Ferrtxt:
	NORMAL
	dc.b "File "
	NOIRGRAS
	dc.b	"%s :"
bundleerrtxt:
	NORMAL
	dc.b	"<< Error >>",10,0

Ftypetxt:
	NORMAL
	dc.b	"File "
	NOIRGRAS
	dc.b	"%s : "
	BLANCGRAS
	dc.b	"%s"
	NORMAL
	dc.b 10,0

Ftypetxt2:
	NORMAL
	dc.b	"File "
	NOIRGRAS
	dc.b	"%s : "
	BLANCGRAS
	dc.b	"%s "
	NORMAL
	dc.b	"(subtype of "
	BLANCGRAS
	dc.b	"%s"
	NORMAL
	dc.b	")"	
	dc.b 10,0

HookTxt:
	NORMAL
	dc.b	"This recognizer HOOKS itself on the"
	NOIRGRAS
	dc.b	" %s"
	NORMAL
	dc.b	" MAJOR-FileType",10,10,0


bundleinfo:
	dc.b	"Please wait: merging recognizers to _bundle.reco...",0
bundleinfo2:
	NOIRGRAS
	dc.b	"done!",10,10
	NORMAL
	dc.b	"datamaster.library initialization should now be sped up.",10,10
	dc.b 0

Intro:
	dc.b	10
	NOIRGRAS
	dc.b	"DMControl v1.9"
	NORMAL
	dc.b	" (c) 1995-1997 Alexis 'Cyb' Nasr (nasr@hol.fr)",10,10
	dc.b	"type 'DMcontrol ?' for templates",10
	dc.b	"(reading the doc would help a lot I think ;-)",10,10,0


DataN:
	dc.b	" No  |  ",0

DataY:	dc.b	"Yes  |  ",0

PattN:	NOIRITAL
	dc.b	" (No file patterns) "
	NORMAL
	dc.b 0

norm:	NORMAL
	dc.b	0
	
bwhite:	BLANCGRAS
	dc.b	0


;class texts:

class_ASCII:
	dc.b	"ASCII",0
class_GRAPHICS:
	dc.b	"Graphics",0
class_ANIMATION:
	dc.b	"Animation",0
class_MUSIC:
	dc.b	"Music",0
class_SAMPLE:
	dc.b	"Sample",0
class_PACKED:
	dc.b	"Packed data",0
class_SYSTEM:
	dc.b	"System data",0
class_MISC:
	dc.b	"miscellaneous",0
class_LINK:
	dc.b	"Link to other recognition system",0
class_UNKNOWN:
	dc.b	"Unknown",0
	even


Pri_FREQUENT:
	dc.b	"Frequent",0

Pri_SINGLE:
	dc.b	"Single",0
	
Pri_GROUPED:
	dc.b	"Grouped",0
Pri_GROUPEDSLOW:
	dc.b	"GroupedSlow",0
Pri_RARE:
	dc.b	"Rare",0
Pri_HOOKNAME:
	dc.b	"HookName",0
	
Pri_CUSTOM:
	dc.b	"Custom",0


recodirname:	dc.b	"LIBS:Recognizers/",0
BundleName:	dc.b	"_bundle.reco",0


		
args_template:
	dc.b	"L=LIST/K/S,"
	dc.b	"RT=REMTYPE/K,"		;remove a recognizer (in mem)
	dc.b	"AT=ADDTYPE/K,"		;add (multiple) files [ex:DUMMY=>DUMMY.patt+DUMMY.data+DUMMY.myreco etc]
	dc.b	"AF=ADDFILE/K,"		;add one file [ex:DUMMY.patt]
	dc.b	"AD=ADDDIR/K,"		;add dir
	dc.b	"SD=SCANDIR/K,"		;same but frees previous recos before
	dc.b	"TF=TESTFILES/K/M,"	;testfile()
	dc.b	"NOXPK/K/S,"		;switch for testfile
	dc.b	"S=SIZE/K/N,"		;size for loading (default=-1 :-)
	dc.b	"NC=NODATACHECK/K/S,"	;switch
	dc.b	"LN=LONGNAME/K/S,"	;switch for long filename
	dc.b	"TI=TYPEINFO/K,"	;info on recognizer (may be ALL)
	dc.b	"FI=FILEINFO/K,"	;same but we'll loadseg it. (no ALL)
	dc.b	"CB=CREATEBUNDLE/K"	
	dc.b	0

			section	"blanks",BSS

Output:	ds.l	1

PC_adr:	ds.l	1
PC_Array:
	ds.l	30
PC_Buff:ds.b	1024

Dummy_Buff:
	ds.b	40

bundle_header:
	ds.b	bdl_SIZEOF
bundle_buff:
	REPT	513
	ds.l	1
	ENDR
	
loadseg_buff:
	ds.l	513



