; DMSTextPrepare V0.6					20.04.92
;
; © by Carnivore/BeerMacht
;
; - for public distribution -
;
; * this tiny tool NEEDS  the arp.library *
;
User = 9

AllocMem		= -198
Execute			= -222
FreeMem			= -210
OldOpenLibrary		= -408
OpenLibrary		= -552

ArpExit			= -378
Close			=  -36
DeleteFile		=  -72
DosAllocMem		= -342
DosFreeMem		= -348
Examine			= -102
GADS			= -252
Info			= -114
Lock			=  -84
Open			=  -30
Output                  =  -60
Puts			= -240
Read			=  -42
Seek			=  -66
UnLock			=  -90
Write			=  -48

start:	movem.l d0/a0,-(a7)
	move.l 4.w,a6
	lea start(pc),a5
	lea helptxt(pc),a3
	move.l #helplen,d3
	cmp.b #"?",-2(a0,d0.w)
	beq.s ramhelp
	lea arpname(pc),a1
	moveq #39,d0		;Arp V39.1
	jsr OpenLibrary(a6)
	move.l d0,arpbase-start(a5)
	bne.s arpok
	lea arptxt(pc),a3
	moveq #arplen,d3
ramhelp:addq.w #8,a7
	lea dosname(pc),a1
	jsr OldOpenLibrary(a6)
	move.l d0,a6		;sure
	jsr Output(a6)
	move.l d0,d1
	beq.s nodos
	move.l a3,d2
	jmp Write(a6)
nodos:	rts
arpok:	move.l d0,a6

	movem.l (a7)+,d0/a0
	sub.l a1,a1		;helptxt
	lea clipara(pc),a2	;clipara
	lea templ(pc),a3
	jsr GADS(a6)
	tst.l d0
	bmi.L argerr
	lea helptxt(pc),a1
	beq.L noopt

	move.l clipara(pc),d1	;srctext
	move.l #1005,d2
	jsr Open(a6)
	move.l d0,d7		;filehd
	lea otxttxt(pc),a1	
	beq.L noopt
	move.l d7,d1
	moveq #0,d2
	moveq #1,d3		;eof
	jsr Seek(a6)
	move.l d7,d1
	moveq #0,d2
	moveq #-1,d3		;start
	jsr Seek(a6)
	move.l d0,d5		;len
	jsr DosAllocMem(a6)
	moveq #-1,d3
	move.l d0,d2
	lea mem1txt(pc),a1
	beq.s nomem1
	move.l d0,a4
	move.l d7,d1		;hd
	move.l d5,d3		;len
	jsr Read(a6)	
	sub.l d0,d3		;eof
	lea rtxttxt(pc),a1
	bne.s rderr

	lea tmpfile(pc),a1
	move.l a1,d1
	move.l #1006,d2
	jsr Open(a6)
	move.l d0,d6
	lea otmptxt(pc),a1
	beq.s rderr
	move.l d6,d1
	lea idmsg(pc),a1
	move.l a1,d2
	moveq #idlen,d3
	jsr Write(a6)
	sub.l d0,d3
	lea wtmptxt(pc),a1
	bne.s wrerr
	move.l d6,d1
	move.l a4,d2	
	move.l d5,d3
	jsr Write(a6)
	sub.l d0,d3
	lea wtmptxt(pc),a1
	bne.s wrerr
	moveq #0,d3	;no error
wrerr:	move.l d6,d1
	jsr Close(a6)
rderr:	move.l a1,-(a7)
	move.l a4,a1
	jsr DosFreeMem(a6)
	move.l (a7)+,a1
nomem1:	move.l d7,d1
	jsr Close(a6)
	tst.l d3
	bne.L noopt

	lea drvname(pc),a1
	move.l a1,d1
	jsr Lock (a6)
	move.l d0,d3
	lea drvtxt(pc),a1
	beq.L noopt
	move.l d3,d1
	lea diskinf,a1
	move.l a1,d2
	jsr Info(a6)
	move.l d3,d1
	jsr Unlock(a6)	
	cmp.l #"DOS"*256,diskinf+$18
	lea ndsktxt(pc),a1
	bne.L noopt

	sub.l a4,a4
	moveq #"3",d0			;medium
	move.l clipara+8(pc),d1
	beq.s nocmode
	move.l d1,a0
	move.b (a0),d0
	cmp.b #"0",d0
	lea cmodtxt(pc),a1
	bcs.L noopt
	cmp.b #"4",d0
	bhi.L noopt
nocmode:lea extern(pc),a1
	move.b d0,cmode-extern(a1)
	move.l a1,d1
	clr.l d2
	clr.l d3
	jsr Execute(a6)

	lea tmpfile(pc),a1
	move.l a1,d1
	jsr DeleteFile(a6)

	lea tmpdms(pc),a0
	move.l a0,d1
	move.l #1005,d2
	jsr Open(a6)
	move.l d0,d7		;filehd
	lea opentxt(pc),a1	
	beq.L lateerr
	move.l d7,d1
	moveq #0,d2
	moveq #1,d3		;eof
	jsr Seek(a6)
	move.l d7,d1
	moveq #0,d2
	moveq #-1,d3		;start
	jsr Seek(a6)
	move.l d0,d6		;len
	jsr DosAllocMem(a6)
	moveq #-1,d3
	move.l d0,d2
	lea memtxt(pc),a1
	beq.s nomem
	move.l d0,a4
	move.l d7,d1		;hd
	move.l d6,d3		;len
	jsr Read(a6)	
	sub.l d0,d3		;eof
	lea readtxt(pc),a1
nomem:	move.l d7,d1
	jsr Close(a6)
	tst.l d3
	bne.s lateerr

	lea notxt(pc),a1
	cmp.l #$5452ffff,$38(a4)
	bne.s lateerr
;	move.l #"BEER",$38(a4)
;	bchg #7,$38+8(a4)	

	move.l clipara+4(pc),d1
	bne.s withopt
	lea defname(pc),a1
	move.l a1,d1
withopt:move.l #1006,d2		;mode new
	jsr Open(a6)
	move.l d0,d7		;filehd
	lea opn2txt(pc),a1	
	beq.s lateerr
	move.l d7,d1		;hd
	moveq #$38,d2		;offset dms info header
	add.l a4,d2		;buf
	moveq #$14,d3		;offset trk header
	add.l $38+4(a4),d3 	;len
	jsr Write(a6)	
	sub.l d0,d3
	move.l d7,d1
	jsr Close(a6)
	tst.l d3
	lea writtxt(pc),a1
	bne.s lateerr

	lea suxtxt(pc),a1

lateerr:move.l a1,-(a7)
	move.l a4,a1
	jsr DosFreeMem(a6)

	lea tmpdms(pc),a0
	move.l a0,d1
	jsr DeleteFile(a6)
	move.l (a7)+,a1
noopt:				;a1:errortxt
	move.l arpbase(pc),a6
	jsr Puts(a6)

isquiet:

quit:	move.l arpbase(pc),a6
	moveq #0,d0		;returncode
	jmp ArpExit(a6)
; end of mainprg

argerr:	move.l clipara(pc),a1		;arperrortxt
	jsr Puts(a6)
	bra.s quit

arpbase:dc.l 0
clipara:dc.l 0,0,0
dosname:dc.b "dos.library",0
arptxt:	dc.b "you need "
arpname:dc.b "arp.library",0," V39+",10
arplen=*-arptxt
extern:	dc.b "DMS <nil: >nil: READ "
	dc.b "ram:Necromantical_Screams.DMS "
	dc.b "TEXT ram:Black_Monkeys LOW 0 HIGH 0 CMODE "
cmode:	dc.b "x",0
tmpdms:	dc.b "ram:Necromantical_Screams.DMS",0
tmpfile:dc.b "ram:Black_Monkeys",0
templ:	dc.b "Text,Banner,CMode/K",0
helptxt:dc.b $9b,"0;42m"
	dc.b " DMSTextPrepare V0.6 ",$a9
	dc.b " Carnivore/BeerMacht 20-Apr-92 "
	dc.b $9b,"0m",10
	dc.b " Be sure that you have placed a formatted disk in    "
	dc.b 10
	dc.b " drive 0 and that DMS can be launched by using the   "
	dc.b 10
	dc.b " AmigaDOS search path.                               "
	dc.b 10,10
	dc.b "Usage: DTP <TEXT> [BANNER] [CMode n]",10
helplen=*-helptxt
	dc.b 0
defname:dc.b "S:DMS.Banner01",0
otmptxt:dc.b "Unable to create tempfile",0
wtmptxt:dc.b "Unable to write tempfile",0
notxt:	dc.b "Unable to process textfile",0
otxttxt:dc.b "Unable to open textfile",0
rtxttxt:dc.b "Unable to read textfile",0
mem1txt:dc.b "Not enough free ram to process textfile",0
opentxt:dc.b "Unable to open dmsfile",0
readtxt:dc.b "Unable to read dmsfile",0
opn2txt:dc.b "Unable to create banner",0
writtxt:dc.b "Unable to write banner",0
cmodtxt:dc.b "Illegal CMode specified",0
drvtxt:	dc.b "No disk in drive "
drvname:dc.b "DF0:",0
ndsktxt:dc.b "No DOS disk in drive DF0:",0
memtxt:	dc.b "Not enough free ram to process dmsfile"
suxtxt:	dc.b "The textfile has been converted into "
	dc.b "a banner for DMS",0
idmsg:	dc.b 12		;clear screen
	dc.b "ID:"
	dc.b user/10+"0"
	dc.b user-[user/10*10]+"0"
	dc.b " DMSck (c) by Carnivore/BeerMacht"
	dc.b 12,$1b,"c",7	;reset screen,beep
idlen=*-idmsg

	data
diskinf:blk.b $24,0
