; DMSTextLinker V1.0					20.04.92
;
; © by Carnivore/BeerMacht
;
; - for public distribution -
;
; * this tiny tool NEEDS  the arp.library *
;
Version		=0
User		=9

builtlen = -[User=3]*227

configmax	=8192	;max length of configfile
numcrc		=100	;max num of titlecrc
;
AllocMem		= -198
FreeMem			= -210
OldOpenLibrary		= -408
OpenLibrary		= -552

ArpExit			= -378
Close			=  -36
DeleteFile		=  -72
DosAllocMem		= -342
DosFreeMem		= -348
Examine			= -102
Execute			= -222
GADS			= -252
Input			=  -54
Lock			=  -84
Open			=  -30
Output                  =  -60
Puts			= -240
Read			=  -42
ReName			=  -78
Seek			=  -66
SetProtection		= -186
UnLock			=  -90
Write			=  -48

start:	movem.l d0/a0,-(a7)
	move.l 4.w,a6
	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
	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
	dc.w user
arpok:	st fail
	sf errflag
	move.l d0,a6

	jsr Output(a6)
	move.l d0,clihd

	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),a3
	beq.L noopt

	move.l clihd(pc),d1
	beq.s nocli2
	lea titltxt(pc),a2
	move.l a2,d2
	moveq #titlelen,d3
	jsr Write(a6)
nocli2:	

	lea tmp2buf,a2
	move.l clipara(pc),a0
nameend:tst.b (a0)+
	bne.s nameend
	move.l a0,a3		;nameend
goback:	cmp.b #"/",-(a0)
	beq.s cpypath
	cmp.b #":",(a0)
	beq.s cpypath
	cmp.l clipara(pc),a0
	bhi.s goback
	bra.s onlynam		;beg name
cpypath:move.l clipara(pc),a1
cpypat2:move.b (a1),tmp1buf-tmp2buf(a2)	
	move.b (a1)+,(a2)+
	cmp.l a0,a1
	bls.s cpypat2
onlynam:lea tmpname(pc),a1
filpath:move.b (a1),tmp1buf-tmp2buf(a2)
	move.b (a1)+,(a2)+
	bne.s filpath
	sf (a2)
	move.b #"2",-(a2)
	
	clr.l bannbuf
	clr.l workbuf

	lea cnffile(pc),a1	;ram:
	move.l a1,d1
	move.l #1005,d2
	jsr Open(a6)
	move.l d0,d4
	bne.s ramcnf

	lea cnffil2(pc),a1	;s:
	move.l a1,d1
	move.l #1005,d2
	jsr Open(a6)
	lea ucnftxt(pc),a3
	move.l #"open",10(a3)
	move.l d0,d4
	beq.L noopt

ramcnf:	move.l d4,d1
	lea config,a2
	move.l a2,d2
	move.l #configmax,d3
	jsr Read(a6)
	move.l d0,cnflen
	move.l d4,d1
	jsr Close(a6)
	lea ucnftxt(pc),a3
	move.l #"read",10(a3)
	tst.l cnflen
	beq.L noopt

	lea config,a2
donext:	move.l a2,a1

	lea cnferr(pc),a0
	bsr.L cmptxt
	bne.s noerr
		
	lea cnferrt(pc),a3
srcstrt:cmp.b #10,(a1)
	beq.L noopt
	cmp.b #34,(a1)+
	bne.s srcstrt
	lea failtxt(pc),a0
	moveq #faillen-1-1,d0
cpyfail:cmp.b #34,(a1)
	beq.s endfail
	move.b (a1)+,(a0)+
	dbf d0,cpyfail
	bra.L noopt
endfail:sf (a0)
	bra.L nxtline

noerr:	tst.l clipara+8		;strip?
	bne.s nostr
	tst.l clipara+12	;nostrip?
	bne.s nostr

	lea cnfstr(pc),a0
	bsr.L cmptxt
	bne.s nostr
	lea cnfstrt(pc),a3
skip2:	cmp.b #10,(a1)
	beq.L noopt
	cmp.b #" ",(a1)+
	beq.s skip2
	
	cmp.b #"y",-1(a1)
	bne.s chkno
	cmp.b #"e",(a1)+
	bne.L noopt
	cmp.b #"s",(a1)+
	bne.L noopt
	moveq #-1,d0
	move.l d0,clipara+8	;strip!
	bra.L nxtline
chkno:	cmp.b #"n",-1(a1)
	bne.L noopt
	cmp.b #"o",(a1)
	bne.L noopt
	bra.L nxtline

nostr:	tst.l clipara+16	;add?
	bne.L noaddit
	tst.l clipara+20	;noadd?
	bne.L noaddit

	lea cnfadd(pc),a0
	bsr.L cmptxt
	bne.L noaddit
	lea cnfaddt(pc),a3
skip3:	cmp.b #10,(a1)
	beq.L noopt
	cmp.b #" ",(a1)+
	beq.s skip3
	
	cmp.b #"y",-1(a1)
	bne.s chkno2
	cmp.b #"e",(a1)+
	bne.L noopt
	cmp.b #"s",(a1)+
	bne.L noopt
	bra.L nxtline
chkno2:	cmp.b #"n",-1(a1)
	bne.L noopt
	cmp.b #"o",(a1)
	bne.L noopt
	moveq #-1,d0
	move.l d0,clipara+20	;noadd!
	bra.L nxtline

noaddit:lea cnfrnd(pc),a0
	bsr.L cmptxt
	bne.s nornd
	lea cnfrndt(pc),a3
skip4:	cmp.b #10,(a1)
	beq.L noopt
	cmp.b #" ",(a1)+
	beq.s skip4
	move.b (a1),d1
	move.b -(a1),d0
	sub.b #"0",d0
	bcs.L noopt
	cmp.b #9,d0
	bhi.L noopt
	ext.w d0
	cmp.b #"0",d1
	bcs.s onechr
	cmp.b #"9",d1
	bhi.s onechr
	mulu #10,d0
	sub.b #"0",d1
	add.b d1,d0	
onechr:	tst.w d0
 	beq.l noopt
	move.w d0,textnum
	bra.L nxtline	

nornd:	lea cnfcrc(pc),a0
	bsr.L cmptxt
	bne.s nocrc

	lea cnfcrct(pc),a3
skip5:	cmp.b #10,(a1)
	beq.L noopt
	cmp.b #" ",(a1)+
	beq.s skip5

	moveq #4-1,d2
doword:	move.b -1(a1),d0
	cmp.b #"0",d0
	bcs.L noopt
	cmp.b #"9",d0
	bhi.s nodez
	sub.b #"0",d0
	bra.s isdez	
nodez:	or.b #$20,d0
	cmp.b #"a",d0
	bcs.L noopt
	cmp.b #"f",d0
	bhi.L noopt
	sub.b #"a"-10,d0
isdez:	lsl.w #4,d1
	add.b d0,d1
	addq.l #1,a1
	dbf d2,doword
	lea textcrc,a0
	move.w crcoff(pc),d0
	cmp.w #numcrc*2,d0
	lea manytxt(pc),a3
	bcc.L noopt
	move.w d1,(a0,d0.w)
	addq.w #2,crcoff
	bra.s nxtline
nocrc:	
	nop

nxtline:subq.l #1,cnflen
	bmi.s endcnf
	cmp.b #10,(a2)+
	beq.L donext
	bra.s nxtline
	
endcnf:	

	move.l clipara+4(pc),d1
	bne.s withopt

	lea $dff004,a0		;randomize
	move.l $0a-4(a0),d0
	move.l (a0),d1
	add.l d1,d0
	rol.w d1,d0
	move.l $12-4(a0),d1
	eor.l d1,d0
	ror.b d1,d0
	move.w d0,d1
	clr.w d0
	swap d0
	add.w d1,d0
	divu textnum(pc),d0	;1..99
	swap d0
	addq.w #1,d0		;(1..TextNum).w
	lea defnum(pc),a1
	bsr.L byt2dez
	move.b #"#",linknum
	move.w defnum(pc),linknum+1

	lea defname(pc),a1	;ram:
	move.l a1,d1
	move.l #1005,d2
	jsr Open(a6)
	move.l d0,d7		;filehd
	bne.s ramban
	lea defname+2(pc),a1
	move.b #"S",(a1)	;ram: -> s:
	move.l a1,d1
withopt:move.l #1005,d2
	jsr Open(a6)
	lea banntxt(pc),a3	
	move.l #"open",10(a3)
	move.l d0,d7		;filehd
	beq.L noopt
ramban:	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,bannlen	;len
	jsr DosAllocMem(a6)
	move.l d0,a4
	moveq #-1,d3
	move.l d0,bannbuf
	lea memtxt(pc),a3
	beq.s nomem
	move.l d7,d1		;hd
	move.l d0,d2
	move.l bannlen(pc),d3	;len
	jsr Read(a6)	
	sub.l d0,d3		;eof
nomem:	move.l d7,d1
	jsr Close(a6)
	lea banntxt(pc),a3
	move.l #"read",10(a3)
	tst.l d3
	bne.L nofree
	lea frmtxt(pc),a3
;	bchg #7,8(a4)
;	cmp.l #"BEER",(a4)
	cmp.l #$5452ffff,(a4)
	bne.L nofree
;	eor.l #$5452ffff~"BEER",(a4)

	move.l #$e0000,d5
trymem:	sub.l #$8000,d5
	lea mem2txt(pc),a3
	beq.L nofree
	move.l d5,d0
	moveq #1,d1		;faster than !clear
	move.l a6,-(a7)
	move.l 4.w,a6
	jsr AllocMem(a6)
	move.l (a7)+,a6
	move.l d5,worksiz
	move.l d0,workbuf
	move.l d0,a5
	beq.s trymem

	move.l #tmp1buf,d1
	move.l #1006,d2		;new
	jsr Open(a6)
	move.l d0,dsthd
	lea temptxt(pc),a3
	beq.L lateerr

	move.l clipara(pc),d1
	move.l #1005,d2
	jsr Open(a6)
	move.l d0,srchd		;filehd
	move.l d0,d7
	lea opentxt(pc),a3	
	beq.L closdst
	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,srclen	;dmssourcelen
	st errflag
	lea failtxt(pc),a3
	moveq #76,d1
	cmp.l d1,d0		;file too small
	bls.L closall
	sf errflag

	move.l srchd(pc),d1
	move.l a5,d2
	moveq #$38,d3		;dms header
	move.l d3,d7		;srcpos
	jsr Read(a6)
	lea rsrctxt(pc),a3
	sub.l d3,d0
	bne.L closall
;	cmp.l #"YOUA",(a5)
;	bne.s nocrash
;	cmp.l #"RELA",4(a5)
;	bne.s nocrash
;	cmp.w #"ME",8(a5)
;	bne.s nocrash
;oi:	jsr oi(pc)		;recursive checking :-)
nocrash:st errflag
	lea failtxt(pc),a3
	cmp.l #"DMS!",(a5)
	bne.L closall
	sf errflag

	move.b $11(a5),d0
	lea lowtxt(pc),a1
	bsr.L byt2dez
	move.b $13(a5),d0
	lea hitxt(pc),a1
	bsr.L byt2dez
	move.l #"NOCO",d0
	move.w #"MP",d1
	move.b $35(a5),d2
	beq.s cmod0
	move.l #"SIMP",d0
	move.w #"LE",d1
	subq.b #1,d2
	beq.s cmod0
	move.l #"QUIC",d0
	move.w #"K ",d1
	subq.b #1,d2
	beq.s cmod0
	move.l #"MEDI",d0
	move.w #"UM",d1
	subq.b #1,d2
	beq.s cmod0
	move.l #"DEEP",d0
	move.w #"  ",d1
cmod0:	move.l d0,cmodtxt
	move.w d1,cmodtxt+4

	move.l $0c(a5),d2	;seconds since 1.1.70 06:00:00
	sub.l #60*60*6,d2	;seconds since 1.1.70 00:00:00
	moveq #60,d0
	bsr.L div32
	lea time+6(pc),a1
	bsr.L byt2dez
	moveq #60,d0
	bsr.L div32
	subq.l #5,a1
	bsr.L byt2dez
	moveq #24,d0
	bsr.L div32
	subq.l #5,a1
	bsr.L byt2dez
	move.l d2,d1
	bsr.L todate
	lea date+3(pc),a1
	lea months-3(pc),a0
	add.w d0,a0
	add.w d0,a0
	add.w d0,a0
	move.b (a0)+,(a1)+
	move.b (a0)+,(a1)+
	move.b (a0)+,(a1)
	subq.l #5,a1
	move.b d1,d0
	bsr.L byt2dez
	addq.l #5,a1
	move.w d2,d0
	cmp.w #77,d0
	bls.s invalid
	cmp.w #99,d0
	bhi.s invalid
	bsr.L byt2dez
	bra.s isvalid
invalid:lea timetxt(pc),a0
	lea time(pc),a1
	moveq #timelen-1,d0
copyerr:move.b (a0)+,(a1)+
	dbf d0,copyerr
isvalid:

	move.l clihd(pc),d1
	beq.s nocli4
	lea infotxt(pc),a0
	move.l a0,d2
	moveq #infolen,d3
	jsr Write(a6)
nocli4:

	move.l dsthd(pc),d1
	move.l a5,d2
	moveq #$38,d3
	jsr Write(a6)
	lea wtmptxt(pc),a3
	sub.l d3,d0
	bne.L closall

	move.l srchd(pc),d1
	move.l a5,d2
	moveq #$14,d3		;trknum/len
	jsr Read(a6)
	lea rsrctxt(pc),a3
	sub.l d3,d0
	bne.L closall
	move.l srchd(pc),d1
	moveq #-$14,d2		;back to header
	moveq #0,d3
	jsr Seek(a6)
	cmp.l #$5452ffff,(a5)	;txt?
	bne.s domine

	addq.b #1,rmvcnt

	move.l srchd(pc),d1	;copy 1st dmstxt
	move.l a5,d2
	move.w 6(a5),d3
	ext.l d3
	moveq #$14,d0		;headerlen
	add.l d0,d3
	cmp.l d5,d3		;readlen>buffer
	bhi.L closall
	add.l d3,d7		;srcpos
	jsr Read(a6)
	sub.l d3,d0
	lea rsrctxt(pc),a3
	bne.L closall

	tst.l clipara+8		;strip?
	bne.s domine

	move.w $10(a5),d1	;datacrc
	lea textcrc,a0
	move.w crcoff(pc),d0
nextcmp:subq.w #2,d0
	bmi.s unknown
	cmp.w (a0,d0.w),d1	;is it a known dmstext?
	beq.s domine
	bra.s nextcmp

unknown:move.l dsthd(pc),d1
	move.l a5,d2		;d3:readlen
	jsr Write(a6)
	sub.l d0,d3
	lea wtmptxt(pc),a3
	bne.L closall	
	subq.b #1,rmvcnt

domine:	tst.l clipara+20
	bne.s noadd
	move.l dsthd(pc),d1	;write my dmstxt
	move.l bannbuf(pc),d2
	move.l bannlen(pc),d3
	jsr Write(a6)
	sub.l d3,d0
	lea writtxt(pc),a3
	bne.L closall

noadd:	move.l srchd(pc),d1
	move.l a5,d2
	moveq #$14,d3		;trknum/len
	add.l d3,d7		;srcpos
	jsr Read(a6)
	lea rsrctxt(pc),a3
	sub.l d3,d0
	bne.L closall

	move.l clihd(pc),d1
	beq.L nocli5
	lea chkctxt(pc),a0
	move.l a0,d2
	moveq #chkclen,d3
	jsr Write(a6)
nocli5:	move.l a5,a4		;bufstart

getnext:
	move.b (a4)+,d0
	moveq #8,d1
	lsl.w d1,d0
	move.b (a4)+,d0
	st errflag
	cmp.w #"TR",d0
	lea failtxt(pc),a3
	bne.L closall
	sf errflag
	move.b (a4)+,d0
	lsl.w d1,d0
	move.b (a4)+,d0		;d0.w:trknum

	lea cyltxt+2(pc),a1
	move.w #"-1",(a1)	;text
	st pcktxt
	ext.l d0
	bmi.s txtcyl
	sf pcktxt
	moveq #79,d1
	st errflag
	cmp.l d1,d0
	lea failtxt(pc),a3
	bhi.L closall
	sf errflag
	bsr.L byt2dez
txtcyl:	move.l clihd(pc),d1
	beq.s noclihd
	lea cyltxt(pc),a0
	move.l a0,d2
	moveq #cyllen,d3
	jsr Write(a6)

noclihd:addq.l #2,a4
	moveq #0,d0
	move.b (a4)+,d0
	lsl.w #8,d0
	move.b (a4)+,d0		;blklen
	move.w d0,blklen
	addq.l #8,a4
	move.b (a4)+,crc
	move.b (a4)+,crc+1
	addq.l #2,a4		;a4:bufptr
	moveq #$14,d3		;next headerlen
	add.l d0,d3		;next readlen	

	tst.b pcktxt
	beq.s nostrip
	tst.l clipara+12
	bne.s nostrip		;keep all texts

	moveq #$14,d0
	sub.l d0,a4		;remove header
	move.l srchd(pc),d1
	move.l d3,d2		;offset
	sub.l d0,d2
	add.l d2,d7
	moveq #0,d3		;offset current
	jsr Seek(a6)
	moveq #$14,d3
	addq.b #1,rmvcnt
	bra.s nosvbuf
nostrip:move.l a4,d1		;bufptr
	sub.l a5,d1		;bufstart>bufpos	
	add.l d3,d1		;blklen
	cmp.l d5,d1		;buflen
	bls.s nosvbuf

	move.l d3,-(a7)
	move.l dsthd(pc),d1
	move.l a5,d2		;bufstart
	move.l a4,d3
	sub.l a5,d3		;wrlen
	jsr Write(a6)
	sub.l d3,d0
	move.l (a7)+,d3
	lea wtmptxt(pc),a3
	tst.l d0
	bne.L closall
	move.l a5,a4

nosvbuf:move.l srchd(pc),d1
	move.l a4,d2	;bufptr, d3:readlen
	add.l d3,d7	;srcpos
	jsr Read(a6)	

	tst.l clipara+12 ;check text crc, if not stripped
	bne.s chkit

	tst.b pcktxt	;no chk crc
	bne.s nodata

chkit:	st errflag	;a4:trkdata
	bsr.L docrc	;del:a0,a1,d1,d2
	lea failtxt(pc),a3

	bne.s closall
	sf errflag

nodata:	moveq #$14,d1
	add.l d3,a4	;a4:bufptr+$14
	sub.l d1,a4	;a4:bufptr(header)	

	cmp.l d0,d3
	beq.s noshrd

	moveq #$14,d1
	add.l srclen(pc),d1
	st errflag
	cmp.l d1,d7
	lea rsrctxt(pc),a3
	beq.s srcend
	cmp.l #$14,d0	;trash smaller than header
	bcs.s srcend2	;no error, just strip it
	cmp.l d1,d7	;
	lea failtxt(pc),a3
	bhi.s srclong	;eof?
	bcs.s srcshrt

noshrd:	bra.L getnext

srcend:	moveq #$14,d1
	sub.l d1,d3
	cmp.l d3,d0
	bne.s enderr
srcend2:sf errflag
	move.l dsthd(pc),d1
	move.l a5,d2
	move.l a4,d3
	sub.l a5,d3
	jsr Write(a6)
	sub.l d3,d0
	lea wtmptxt(pc),a3
	bne.s closall
	sf fail
enderr:
srclong:
srcshrt
closall:move.l srchd(pc),d1
	jsr Close(a6)
closdst:move.l dsthd(pc),d1
	jsr Close(a6)
	tst.b fail
	beq.s nolater
	move.l #tmp1buf,d1	;delete dms.workfile
	jsr DeleteFile(a6)	;if file not fnd etc...
	;tst.l d0
	;beq.s delerr
	bra.s lateerr

nolater:move.l clipara(pc),d1
	move.l #tmp2buf,d2
	jsr ReName(a6)
	tst.l d0
	lea mov1txt(pc),a3
	beq.s lateerr
	move.l #tmp1buf,d1
	move.l clipara(pc),d2
	jsr ReName(a6)
	tst.l d0
	lea mov2txt(pc),a3
	beq.s lateerr
	move.l #tmp2buf,d1
	jsr DeleteFile(a6)
	tst.l d0
	lea deltxt(pc),a3
	beq.s lateerr

	lea rmvtxt(pc),a1
	move.w #"no",d2
	move.b rmvcnt(pc),d0
	beq.s normv
	cmp.b #9,d0
	bls.s notmany
	moveq #9,d0	;output max 9
notmany:move.w #" 0",d2
	add.b d0,d2
	subq.b #1,d0
	bne.s normv
	move.b #" ",texts-rmvtxt(a1)
normv:	move.w d2,(a1)

	tst.l clipara+20	;noadd?
	beq.s txtlnk
	sf addtxt		;eot, no linktxt
txtlnk:	lea suxtxt(pc),a3
lateerr:move.l workbuf(pc),a1
	move.l worksiz(pc),d0
	beq.s nofree
	move.l 4.w,a6
	jsr FreeMem(a6)		;!!
nofree:	move.l bannbuf(pc),a1
	move.l arpbase(pc),a6
	jsr DosFreeMem(a6)
noopt:	
	move.l arpbase(pc),a6
	lea zerotxt(pc),a1	;lf
	jsr Puts(a6)
	move.l a3,a1		;a3:errortxt
	jsr Puts(a6)

isquiet:

quit:	move.l arpbase(pc),a6
	moveq #0,d0		;returncode
	tst.b errflag
	beq.s isok
	moveq #10,d0
isok:	jmp ArpExit(a6)
; end of mainprg

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

byt2dez:moveq #0,d1
	st d1
	and.l d1,d0
	divu #10,d0
	moveq #"0",d1
	add.b d1,d0
	move.b d0,(a1)+
	swap d0
	add.b d1,d0
	move.b d0,(a1)+
	rts

cmptxt:	tst.b (a0)
	beq.L endcmp
	cmpm.b (a1)+,(a0)+
	beq.s cmptxt
	move.l a2,a1
endcmp:	rts		;beq.s ok - bne.s diff

div32:	moveq #0,d3	;d2.l:d0.w > d2.l , rest d0.w
	divu d0,d2
	bvc.s div16
	move.l d2,d1
	clr.w d2
	swap d2
	divu d0,d2
	move.w d2,d3
	move.w d1,d2
	divu d0,d2
div16:	move.l d2,d0
	swap d2
	move.w d3,d2
	swap d2
	clr.w d0
	swap d0
	rts

todate:	add.l #28431-2922,d1	;d1:days since 1.1.70 00:00:00
	lsl.l #2,d1
	move.l d1,d2
	subq.l #1,d2
	divu #1461,d2
	and.l #$0000ffff,d2
	addq.l #3,d1
	move.l d2,d4
	mulu #1461,d4
	sub.l d4,d1
	lsr.l #2,d1
	move.l d1,d4
	lsl.l #2,d1
	add.l d4,d1		;5*days
	move.l d1,d0
	subq.l #3,d0
	divu #153,d0
	and.l #$0000ffff,d0
	addq.l #2,d1
	move.l d0,d4
	mulu #153,d4
	sub.l d4,d1
	divu #5,d1
	and.l #$0000ffff,d1
	cmp.l #10,d0
	bge.s date1
	addq.l #3,d0		;month+3
	bra.s date2
date1:	sub.l #9,d0		;month-9
	addq.l #1,d2		;year+1
				;d0:month
				;d1:day
date2:	rts			;d2:year

docrc:	move.w d3,-(a7)
	move.l a4,a0
	lea crctab(pc),a1
	move.w blklen(pc),d3
	subq.w #1,d3
	moveq #0,d1
calccrc:moveq #0,d2
	move.b (a0)+,d2
	eor.b d1,d2
	add.w d2,d2
	lsr.w #8,d1
	move.w 0(a1,d2.w),d2
	eor.w d2,d1
	dbf d3,calccrc
	move.w (a7)+,d3
	cmp.w crc(pc),d1
	rts

crctab:	dc.l $0000c0c1,$c1810140,$c30103c0,$0280c241,$c60106c0,$0780c741
	dc.l $0500c5c1,$c4810440,$cc010cc0,$0d80cd41,$0f00cfc1,$ce810e40
	dc.l $0a00cac1,$cb810b40,$c90109c0,$0880c841,$d80118c0,$1980d941
	dc.l $1b00dbc1,$da811a40,$1e00dec1,$df811f40,$dd011dc0,$1c80dc41
	dc.l $1400d4c1,$d5811540,$d70117c0,$1680d641,$d20112c0,$1380d341
	dc.l $1100d1c1,$d0811040,$f00130c0,$3180f141,$3300f3c1,$f2813240
	dc.l $3600f6c1,$f7813740,$f50135c0,$3480f441,$3c00fcc1,$fd813d40
	dc.l $ff013fc0,$3e80fe41,$fa013ac0,$3b80fb41,$3900f9c1,$f8813840
	dc.l $2800e8c1,$e9812940,$eb012bc0,$2a80ea41,$ee012ec0,$2f80ef41
	dc.l $2d00edc1,$ec812c40,$e40124c0,$2580e541,$2700e7c1,$e6812640
	dc.l $2200e2c1,$e3812340,$e10121c0,$2080e041,$a00160c0,$6180a141
	dc.l $6300a3c1,$a2816240,$6600a6c1,$a7816740,$a50165c0,$6480a441
	dc.l $6c00acc1,$ad816d40,$af016fc0,$6e80ae41,$aa016ac0,$6b80ab41
	dc.l $6900a9c1,$a8816840,$7800b8c1,$b9817940,$bb017bc0,$7a80ba41
	dc.l $be017ec0,$7f80bf41,$7d00bdc1,$bc817c40,$b40174c0,$7580b541
	dc.l $7700b7c1,$b6817640,$7200b2c1,$b3817340,$b10171c0,$7080b041
	dc.l $500090c1,$91815140,$930153c0,$52809241,$960156c0,$57809741
	dc.l $550095c1,$94815440,$9c015cc0,$5d809d41,$5f009fc1,$9e815e40
	dc.l $5a009ac1,$9b815b40,$990159c0,$58809841,$880148c0,$49808941
	dc.l $4b008bc1,$8a814a40,$4e008ec1,$8f814f40,$8d014dc0,$4c808c41
	dc.l $440084c1,$85814540,$870147c0,$46808641,$820142c0,$43808341
	dc.l $410081c1,$80814040
arpbase:dc.l 0
clipara:dc.l 0,0,0,0,0,0,0,0,0
clihd:	dc.l 0
srchd:	dc.l 0
dsthd:	dc.l 0
srclen:	dc.l 0
bannbuf:dc.l 0
bannlen:dc.l 0
worksiz:dc.l 0
workbuf:dc.l 0
cnflen:	dc.l 0
blklen:	dc.w 0
crc:	dc.w 0
crcoff:	dc.w 0
textnum:dc.w 1		; default last dmstext DMS.BANNER01
ucnftxt:dc.b "Unable to xxxx "		;even!
cnffil2:dc.b "S:DMSck.config",0
cnffile:dc.b "RAM:DMSck.config",0
pcktxt:	dc.b 0
fail:	dc.b 0
errflag:dc.b 0
rmvcnt:	dc.b 0
dosname:dc.b "dos.library",0
arptxt:	dc.b "you need "
arpname:dc.b "arp.library",0," V39+",10
arplen=*-arptxt
templ:	dc.b "DMSFile,Banner,Strip/S,NoStrip/S,Add/S,NoAdd/S,"
	dc.b "-n/S,t/S,-t/S",0
helptxt:dc.b $9b,"0;42m"
	dc.b " DMSck TurboCRC V1.",Version+"0"," ",$a9," "
	dc.b "Carnivore/BeerMacht 20-Apr-92 "
	dc.b $9b,"0m",10
	dc.b " This tool removes any text inside a DMSfile except  "
	dc.b 10
	dc.b " for the titletext (if there is any and unless you   "
	dc.b 10
	dc.b " force a removal!) and also links a new text inside  "
	dc.b 10
	dc.b " the DMSfile, if this DMSfile didn't fail during the "
	dc.b 10
	dc.b " CRC calculations. Note that a temporary workfile is "
	dc.b 10
	dc.b " created on the same unit the file to be processed   "
	dc.b 10
	dc.b " is on.                                              "
	dc.b 10
	dc.b " Greetings to all sick necrophiliacs and lovers of   "
	dc.b 10
	dc.b " Diebels Alt.                                        "
	dc.b 10,10
	dc.b "Usage: DMSck [-n t|-t] DMSFILE"
	dc.b " <BANNER> [Strip] [NoStrip] [Add] [NoAdd]",10
helplen=*-helptxt
zerotxt:dc.b 0
tmpname:dc.b "DMSCK.WORKFILE",0
	even
banntxt:dc.b "Unable to **** banner",0	;open,read - even !!
opentxt:dc.b "Unable to open DMSfile",0
rsrctxt:dc.b "Unable to read DMSfile",0
writtxt:dc.b "Unable to insert banner",0
temptxt:dc.b "Unable to create tempfile",0
wtmptxt:dc.b "Unable to write tempfile",0
mov1txt:dc.b "Unable to move DMSfile",0
mov2txt:dc.b "Unable to move tempfile",0
deltxt:	dc.b "Unable to delete tempfile",0
frmtxt:	dc.b "Wrong textfile format",0
manytxt:dc.b "Too many CRC entries in configfile",0
memtxt:	dc.b "Not enough free ram to load textfile",0
mem2txt:dc.b "Not enough free ram for workspace",0
	even
suxtxt:	dc.b "File passed CRC,"
rmvtxt:	dc.b "** text"			;even !!
texts:	dc.b "s stripped"
addtxt:	dc.b ", DMStext "
linknum:dc.b "was linked within DMSfile",0	;odd !!
	; or ^#01^
	even
defname:dc.b "RAM:DMS.Banner"
defnum:	dc.b "00",0	;RAM:,RAS:		;even !!

faillen=80
failtxt:dc.b "     WARNING: FILE HAS A CRC ERROR"
	;dc.b "ERROR IN DMS FORMAT - CRC ERROR"
	;dc.b "ERROR in DMS format - CRC ERROR"
	dc.b " - CHECK YOUR FILES !",0
deflen=*-failtxt	
	blk.b faillen-deflen,0
	
cnferrt:dc.b "Error in configfile at " 
cnferr:	dc.b "ErrorMSG=",0
cnfstrt:dc.b "Error in configfile at "
cnfstr:	dc.b "Strip=",0
cnfaddt:dc.b "Error in configfile at "
cnfadd:	dc.b "Add=",0
cnfrndt:dc.b "Error in configfile at "
cnfrnd:	dc.b "TextNum=",0
cnfcrct:dc.b "Error in configfile at "
cnfcrc:	dc.b "RemoveCRC=",0
months:	dc.b "JanFebMarAprMayJunJulAugSepOctNovDec"
titltxt:dc.b $1b,$5b,"33m",
	dc.b "DMSck TurboCRC V1.",Version+"0"
	dc.b $1b,$5b,"0m"," by ",$1b,$5b,"1m"
	dc.b "Carnivore/BeerMacht",$1b,$5b,"0m"," 20-Apr-92"
	dc.b " * A BeerMacht Util *",10
titlelen=*-titltxt
	even
infotxt:dc.b "LowCyl: "
lowtxt:	dc.b "** HighCyl: "
hitxt:	dc.b "** CMode: "
cmodtxt:dc.b "****** "		;even!
time:	dc.b "**:**:** "	;\
date:	dc.b "**-***-**"	;/=timelen
infolen=*-infotxt
timetxt:dc.b "Time/Date invalid."
timelen=*-timetxt
chkctxt:dc.b 10,"Checking Cylinder: **"
chkclen=*-chkctxt
	even
cyltxt:	dc.b 8,8,"00"
cyllen=*-cyltxt
builtin:blk.b builtlen,0
	data
config:	blk.b configmax,0
textcrc:blk.w numcrc,0
tmp1buf:blk.b 256,0		;complete path
tmp2buf:blk.b 256,0
