	include 'exec_offsets.i'
	include 'dos_offsets.i'
	include 'layers_offsets.i'

	ifd	CAPE
	EXEOBJ
	endc

pr_CLI		equ	$ac
MsgPort 	equ	$5c
Version 	equ	20

RETURN_WARN	equ	10
RETURN_FAIL	equ	20

	SECTION CODE
Patch1:
	movea.l _AbsExecBase,a6
	suba.l	a1,a1
	Call	FindTask
	movea.l d0,a5
	moveq	#0,d0
	move.l	pr_CLI(a5),d1           ; were we started from a CLI?
	bne.s	1$			; yes, continue
	lea	MsgPort(a5),a0          ; exit gracefully from Workbench
	Call	WaitPort
	lea	MsgPort(a5),a0
	Call	GetMsg
	Call	Forbid
	movea.l d2,a1
	Call	ReplyMsg
	moveq	#-1,d0
	rts
1$:
	lea	DOSName(pc),a1
	Call	OpenLibrary
	move.l	d0,DOSBase
	movea.l d0,a6
	Call	Output
	move.l	d0,d5
	bne.s	2$
	moveq	#RETURN_FAIL,d0
	rts
2$:
	movea.l _AbsExecBase,a0
	cmpi.w	#33,Version(a0)         ; Version 33 == Kickstart 1.2
	beq.s	3$
	lea	Error(pc),a0
	move.l	d5,d1
	move.l	a0,d2
	moveq	#ErrorLen,d3
	Call	Write
	moveq	#RETURN_WARN,d0
	rts
3$:
	movea.l _AbsExecBase,a6
	move.l	#NewStuffLen,d0
	moveq	#0,d1
	Call	AllocMem
	move.l	d0,d4
	bne.s	4$
	moveq	#RETURN_FAIL,d0
	rts
4$:
	movea.l d4,a1
	lea	NewDeleteLayer(pc),a0
	move.l	#NewStuffLen,d0
	Call	CopyMem
	movea.l DOSBase(pc),a6
	lea	Msg(pc),a0
	move.l	d5,d1
	move.l	a0,d2
	move.l	#MsgLen,d3
	Call	Write
	movea.l _AbsExecBase,a6
	lea	LayersName(pc),a1
	Call	OpenLibrary
	move.l	d0,LayersBase
	movea.l LayersBase,a1
	movea.l d4,a0
	move.l	a0,d0
	movea.w #_LVODeleteLayer,a0
	Call	SetFunction
	movea.l a6,a1
	movea.l d4,a0
	adda.l	#NewDeleteLayerLen,a0
	move.l	a0,d0
	movea.w #_LVOLockLayers,a0
	Call	SetFunction
	movea.l LayersBase(pc),a1
	Call	CloseLibrary
	movea.l DOSBase(pc),a1
	Call	CloseLibrary
	moveq	#0,d0
	rts

NewDeleteLayer:
	move.l	a2,-(a7)
	move.l	a3,-(a7)
	movea.l a1,a2
	move.l	$44(a2),-(a7)
	movea.l $44(a2),a3
	jsr	$00fe1080
	tst.l	d0
	addq.l	#4,a7
	beq.s	1$
	move.l	a2,-(a7)
	jsr	$00fe2c86
	move.l	a3,-(a7)
	jsr	$00fe10a4
	moveq	#1,d0
	addq.l	#8,a7
	bra.s	2$
1$:
	moveq	#0,d0
2$:
	movea.l (a7)+,a3
	movea.l (a7)+,a2
	rts
NewDeleteLayerLen equ	*-NewDeleteLayer

NewLockLayers:
	move.w	#$4000,$00dff09a
	move.l	#'HELP',d0
	cmp.l	0,d0
	beq.s	1$
	move.l	d0,0
	lea	$100,a0
	move.l	d7,(a0)+
	move.l	(a5),(a0)+
	move.l	_AbsExecBase,d0
	move.l	d0,d1
	andi.l	#-$00c00677,d1
	beq.s	2$
1$:
	jmp	$00fc305e
2$:
	jmp	$00fc3006
NewStuffLen	equ	*-NewDeleteLayer

LayersBase:
	dc.l	0
DOSBase:
	dc.l	0
DOSName:
	dc.b	'dos.library',0
LayersName:
	dc.b	'layers.library',0
Error:
	dc.b	10,'Sorry, this ONLY works for V1.2 Kickstart'
ErrorLen equ	*-Error

Msg:
	dc.b	'patch_1.arc  Patch file #1 to fix bugs in V1.2 Kickstart'
	dc.b	10,'Written by Bryce Nesbitt.  Please redistribute this file!'
	dc.b	10,10,'Kickstart V1.2 has some bugs that can randomly GURU the'
	dc.b	10,'machine.  Running this program has protected you from these'
	dc.b	10,'problems.  You must reboot to get rid of the protection.'
	dc.b	10,10,'This program runs under Version 1.2 Kickstart ONLY.  If'
	dc.b	10,'Version 1.3 Kickstart needs these fixes, then a separate'
	dc.b	10,'patch file will be provided.  The bugs that are fixed:'
	dc.b	10,10,'1>  On machines with $C00000 memory (like the A500 and'
	dc.b	10,'    A2000), the Alert() function misbehaves.  This makes any'
	dc.b	10,'    alert fatal, even the "recoverable" ones.'
	dc.b	10,'2>  Every time a window is closed (or, in fact, any layer is'
	dc.b	10,'    deleted) the system frees a bit of memory then uses it'
	dc.b	10,'    again.  This can cause random crashes.',10
MsgLen	equ	*-Msg

	END
