	opt	l-,e+

* Cookie: By Euphoria & Fuzzz (and Yakka's RND routine)


	output	cookie

	incdir	include:
	include	exec/funcdef.i
	include	exec/exec_lib.i
	include	libraries/dos_lib.i
	include	libraries/dos.i
	


		
	movem.l	d0-a6,-(sp)	;save reges

	lea	filename,a1
loop
	move.b	(a0)+,d0
	cmp.b	#10,d0
	beq	done
	move.b	d0,(a1)+
	bra	loop
done
	move.b	#0,(a1)

	lea	filename,a1
	move.b	(a1),d0
	bne	goodfile

	lea	defile,a0

	move.l	(a0)+,(a1)+
	move.l	(a0)+,(a1)+
	move.l	(a0)+,(a1)+
	move.l	(a0)+,(a1)+

goodfile	
	moveq	#0,d0
	
	lea		dname,a1
	CALLEXEC	OpenLibrary	
	
	move.l	d0,_DOSBase
	
	CALLDOS	Output
	
	move.l	d0,dhandle

	move.l	#filename,d1
	move.l	#-2,d2
	CALLDOS	Lock
	move.l	d0,filelock
	
	lea	grobblies,a0
	and.w	#$FFFE,d0
	move.l	(a0,d0.l),d2
	eor.l	d2,d0
	move.l	d0,seed2

	move.l	#date,d1
	CALLDOS	DateStamp
	
	lea	date,a0
	move.w	10(a0),d2
	swap	d2
	move.w	6(a0),d2
	move.l	d2,seed2

	lea	filename,a0
	move.l	a0,d1
	move.l	#MODE_OLDFILE,d2
	CALLDOS	Open
	
	tst.l	d0
	bne	isgood
	move.l	#nofile,d2
	move.l	#17,d3
	move.l	dhandle,d1
	CALLDOS	Write
	bra 	ohshit
isgood
	move.l	d0,filehandle
	
	lea	grobblies,a0
	and.w	#$FFFE,d0
	move.l	(a0,d0.l),d2
	eor.l	d2,d0

	move.l	#date,d1
	CALLDOS	DateStamp

	lea	date,a0
	move.w	10(a0),d2
	swap	d2
	move.w	6(a0),d2
	move.l	d2,seed1


notthere
	move.l	filelock,d1
	lea	datablock,a0
	move.l	a0,d2

	CALLDOS	Examine

	tst.l	d0
	beq	ohshit
		
	lea	datablock,a0
	move.l	124(a0),d0
	cmp.w	#0,d0
	bne	ok
	swap	d0
ok
	move.l	d0,length

	bsr	random
	move.l	d0,d7

	ifeq	1
	lea	hexbuff,a4
	move.l	a4,a5
	bsr	hexlongb
	move.b	#10,(a4)+
	sub.l	a5,a4
	move.l	a4,d3
	move.l	dhandle,d1
	move.l	a5,d2
	CALLDOS Write
	endc

	move.l	filehandle,d1
	move.l	d7,d2
	move.l	#-1,d3
	CALLDOS	Seek

	move.l	filehandle,d1
	lea	buffer,a0
	move.l	a0,d2
	move.l	#1024,d3

	CALLDOS	Read

	cmp.l	#-1,d0
	beq	ohshit

	lea	buffer,a0
	move.w	#1023,d1
loop1	
	move.b	(a0)+,d0
	cmp.b	#12,d0
	beq	gotstart
	dbra	d1,loop1

	Bra 	notthere

gotstart
	Lea	textbuff,a1
	moveq	#0,d3
loop2
	add.l	#1,d3
	move.b	(a0)+,d0
	cmp.b	#12,d0
	beq	gotend
	move.b	d0,(a1)+
	dbra	d1,loop2
	
	Bra	notthere

gotend
	sub.l	#1,a1
	move.b	#10,(a1)+
	move.b	#10,(a1)+

	add.l	#1,d3	
	move.l	dhandle,d1
	lea	textbuff,a0
	move.l	a0,d2
	
	CALLDOS	Write

ohshit
	move.l	filelock,d1
	CALLDOS	UnLock

	move.l	filehandle,d1
	CALLDOS	Close

	move.l	_DOSBase,a1
	CALLEXEC CloseLibrary

	movem.l	(sp)+,d0-a6
	rts

random

rnd32	moveq	#32,d0		* Returns a 32 bit rnd number in d0
rnd32lp	move.b	seed1+2,d6		* Trashes d6,d7
	move.b	seed1+4,d7
	lsr.b	#3,d6
	eor.b	d6,d7
	roxr.b	#1,d7
	roxl.w	seed1
	roxl.w	seed1+2
	roxl.w	seed1+4
	dbra	d0,rnd32lp
	move.l	seed1,d0

	move.l	length,d1
	muls	d1,d0
	lsr.l	#8,d0
	lsr.l	#8,d0

	rts



** hex print routines (c) AS


singdigitb
	add.b	#$30,d0			* Special case of a single digit decimal number
	move.b	d0,(a4)+		* ie: no hex sign
	rts


hexminusb
	neg.l	d0			* Turn negative Longs into positive
	move.b	#"-",(a4)+		* to save extra digits
	bra.s	hexlong22


hexlongb
	tst.l	d0			* Is it zero only?
	beq	hexzerob2
	bmi.s	hexminusb		* Negative?

hexlong22

	cmp.l	#9,d0			* Can it be expressed as a 1 digit decimal?
	bls.s	singdigitb

	move.b	#"$",(a4)+		* Put up the hex sign then!

	sf	d4

	movem.l	d0/d1/d3/d4,-(sp)

	moveq	#8-1,d3		* Number of digits (-1)
aschexl	rol.l	#4,d0		* Get next digit into bottom four bits
	move.b	d0,d1		* Mask digit into d1
	andi.w	#15,d1		* will need to be a word later
	bne	asch1		* If non zero, insert it
	tst.b	d4		* Otherwise, check if leading zero
	beq	asch2
asch1	moveq	#1,d4		* No more leading zeroes
	move.b	hextabl(pc,d1.w),(a4)+	* Insert digit
asch2	dbra	d3,aschexl

	movem.l	(sp)+,d0/d1/d3/d4
	rts

	cnop	0,4
hextabl	dc.b	'0123456789ABCDEF'	* Self Explanatory!!!
	cnop	0,4

hexzerob2
	move.b	#"0",(a4)+	* A lone zero (no hex sign)
	moveq	#0,d0
	rts



hexlongb2
	movem.l	d0/d1/d3/d4,-(sp)
	moveq	#8-1,d3
	st	d4
	bra.s	aschexl



hexbuff	ds.l	3

_DOSBase
	dc.l	0
filehandle
	dc.l	0	
filelock
	dc.l	0

filename
	ds.b	100

	even
	cnop	0,4			* longword align
datablock
	ds.b	260
dhandle
	dc.l	0
textbuff
	ds.b	512
buffer
	ds.b	1024
dname	DOSNAME

seed1
	dc.l	0
seed2
	dc.l	0

length
	dc.l	0
date	
	dc.l	0
	dc.l	0
	dc.l	0
long
	dc.l	0
defile
	dc.b	"Cookie.dat",0,0
	even
nofile
	dc.b	10,"Cookie.dat not found",10,10
	even
grobblies

