***************************************
***        Czech Patch v1.0         ***
***                                 ***
*** © 2000 by Sebastian Vater, Inc. ***
***                                 ***
***      This program patches       ***
***  diskfont.library to open a     ***
***  font with ending -CZ at first  ***
*** before trying to open original  ***
***              name.              ***
***                                 ***
***            Usage:               ***
***   CzechPatch [ON|OFF] [-f fmt]  ***
***   default <fmt> is %s-CZ%s      ***
*** means to add a -CZ before .font ***
***            extension            ***
*** Zuzana Burkertová, I love you ! ***
***************************************

	include	"exec/types.i"
	include	"exec/libraries.i"
	include	"exec/execbase.i"
	include	"exec/exec_lib.i"
	include	"exec/memory.i"
	include	"libraries/dos_lib.i"
	include	"graphics/graphics_lib.i"
	include	"graphics/text.i"
	include	"libraries/diskfont.i"
	include	"libraries/diskfont_lib.i"

	SECTION	"Zuzka",CODE

Start:	move.l	a0,a2
	moveq	#0,d0
	moveq	#10,d7
	lea	DOSName(pc),a1
	lea	DOSBase(pc),a5
	move.l	4.w,a6
	jsr	_LVOOpenLibrary(a6)
	move.l	d0,(a5)
	moveq	#0,d0
	lea	GfxName(pc),a1
	jsr	_LVOOpenLibrary(a6)
	move.l	d0,GfxBase-DOSBase(a5)
	lea	WelcomeText(pc),a0
	bsr	PutStr
	moveq	#0,d0
	moveq	#0,d1
	lea	Fmt(pc),a0
	lea	FmtBuf(pc),a1
CopyDefLoop:	move.b	(a0)+,(a1)+
	bne.s	CopyDefLoop
ScanArgsLoop:	move.b	(a2)+,d2
	beq	ScanArgsDone
	cmp.b	#10,d2
	beq	ScanArgsDone
	cmp.b	#13,d2
	beq	ScanArgsDone
	cmp.b	#" ",d2
	beq.s	ScanArgsLoop
	cmp.b	#9,d2
	beq.s	ScanArgsLoop
	cmp.b	#"-",d2
	beq.s	CheckFmt
	tst.l	d0
	bne	ScanArgsError
	and.b	#~("a"-"A"),d2
	cmp.b	#"O",d2
	bne	ScanArgsError
	move.b	(a2)+,d2
	and.b	#~("a"-"A"),d2
	cmp.b	#"N",d2
	beq.s	TurnOn
	cmp.b	#"F",d2
	bne	ScanArgsError
	move.b	(a2)+,d2
	and.b	#~("a"-"A"),d2
	cmp.b	#"F",d2
	bne	ScanArgsError
	moveq	#2,d0
ScanArgsOnOffCheck:	move.b	(a2)+,d2
	beq.s	ScanArgsDone
	cmp.b	#10,d2
	beq.s	ScanArgsDone
	cmp.b	#13,d2
	beq.s	ScanArgsDone
	cmp.b	#" ",d2
	beq.s	ScanArgsLoop
	cmp.b	#9,d2
	beq.s	ScanArgsLoop
	bra	ScanArgsError
TurnOn:	moveq	#1,d0
	bra.s	ScanArgsOnOffCheck
CheckFmt:	tst.l	d1
	bne	ScanArgsError
	move.b	(a2)+,d2
	or.b	#("a"-"A"),d2
	cmp.b	#"f",d2
	bne	ScanArgsError
ScanArgsFmtLoop:	move.b	(a2)+,d2
	beq	ScanArgsError
	cmp.b	#10,d2
	beq	ScanArgsError
	cmp.b	#13,d2
	beq	ScanArgsError
	cmp.b	#" ",d2
	beq.s	ScanArgsFmtLoop
	cmp.b	#9,d2
	beq.s	ScanArgsFmtLoop
	lea	FmtBuf(pc),a1
ScanArgsPutFmtLoop:	move.b	d2,(a1)+
	move.b	(a2)+,d2
	beq.s	ScanArgsDone
	cmp.b	#10,d2
	beq.s	ScanArgsDone
	cmp.b	#13,d2
	beq.s	ScanArgsDone
	cmp.b	#" ",d2
	beq	ScanArgsLoop
	cmp.b	#9,d2
	beq	ScanArgsLoop
	bra.s	ScanArgsPutFmtLoop
ScanArgsDone:	move.l	d0,d2
	moveq	#0,d0
	lea	DiskFontName(pc),a1
	lea	DiskFontBase(pc),a5
	move.l	4.w,a6
	jsr	_LVOOpenLibrary(a6)
	move.l	d0,(a5)
	beq	DiskFontError
	move.l	d0,a2
	move.l	_LVOOpenDiskFont+2(a2),a3
	move.l	GfxBase(pc),a4
	move.l	_LVOOpenFont+2(a4),a4
	tst.l	d2
	beq.s	CheckType
	subq.l	#1,d2
	bne.s	CheckOff
	lea	TurningOnText(pc),a0
	bsr	PutStr
	cmp.w	#$6008,(a3)+
	bne.s	MakeOn
	cmp.l	#"ZUZK",(a3)+
	bne.s	MakeOn
	cmp.l	#"A !"<<8,(a3)
	bne.s	MakeOn
	lea	AlreadyOnText(pc),a0
	bsr	PutStr
	bra	CloseDiskFont
CheckOff:	lea	TurningOffText(pc),a0
	bsr	PutStr
	cmp.w	#$6008,(a3)+
	bne.s	AlreadyOff
	cmp.l	#"ZUZK",(a3)+
	bne.s	AlreadyOff
	cmp.l	#"A !"<<8,(a3)
	beq	MakeOff
AlreadyOff:	lea	AlreadyOffText(pc),a0
	bsr	PutStr
	bra	CloseDiskFont
CheckType:	cmp.w	#$6008,(a3)+
	bne.s	IsOff
	cmp.l	#"ZUZK",(a3)+
	bne.s	IsOff
	cmp.l	#"A !"<<8,(a3)
	beq	IsOn
IsOff:	cmp.w	#$6008,(a4)+
	bne.s	KeepOff
	cmp.l	#"ZUZK",(a4)+
	bne.s	KeepOff
	cmp.l	#"A !"<<8,(a4)
	beq.s	IsOn
KeepOff:	lea	CurrentOffText(pc),a0
	bsr	PutStr
MakeOn:	move.l	#PatchFontSize,d0
	moveq	#MEMF_PUBLIC,d1
	move.l	4.w,a6
	jsr	_LVOAllocMem(a6)
	tst.l	d0
	bne.s	VectorAllocOk
	lea	OutOfMemText(pc),a0
	bsr	PutStr
	bra	CloseDiskFont
VectorAllocOk:	move.l	d0,a2
	move.l	#PatchFontSize,d0
	lea	PatchOpenDiskFont(pc),a0
	move.l	a2,a1
	jsr	_LVOCopyMemQuick(a6)
	moveq	#0,d0
	lea	DiskFontName(pc),a1
	jsr	_LVOOpenLibrary(a6)
	lea	_LVOOpenDiskFont,a0
	move.l	d0,a1
	move.l	_LVOOpenDiskFont+2(a1),OSOpenDiskFont-PatchOpenDiskFont(a2)
	move.l	a2,d0
	jsr	_LVOSetFunction(a6)
	lea	_LVOOpenFont,a0
	move.l	GfxBase(pc),a1
	lea	PatchOpenFont-PatchOpenDiskFont(a2),a2
	move.l	_LVOOpenFont+2(a1),OSOpenFont-PatchOpenFont(a2)
	move.l	a2,d0
	jsr	_LVOSetFunction(a6)
	lea	DoneText(pc),a0
	bsr	PutStr
	moveq	#0,d7
	bra.s	CloseDiskFont
IsOn:	lea	CurrentOnText(pc),a0
	bsr	PutStr
MakeOff:	lea	_LVOOpenFont,a0
	move.l	GfxBase(pc),a1
	move.l	_LVOOpenFont+2(a1),a2
	move.l	OSOpenFont-PatchOpenFont(a2),d0
	move.l	4.w,a6
	jsr	_LVOSetFunction(a6)
	lea	_LVOOpenDiskFont,a0
	move.l	DiskFontBase(pc),a1
	move.l	_LVOOpenDiskFont+2(a1),a2
	move.l	OSOpenDiskFont-PatchOpenDiskFont(a2),d0
	jsr	_LVOSetFunction(a6)
	move.l	DiskFontBase(pc),a1
	jsr	_LVOCloseLibrary(a6)
	lea	DoneText(pc),a0
	bsr.s	PutStr
	moveq	#0,d7
CloseDiskFont:	move.l	DiskFontBase(pc),a1
	move.l	4.w,a6
	jsr	_LVOCloseLibrary(a6)
CloseDOS:	move.l	GfxBase(pc),a1
	move.l	4.w,a6
	jsr	_LVOCloseLibrary(a6)
	move.l	DOSBase(pc),a1
	jsr	_LVOCloseLibrary(a6)
ExitToDOS:	move.l	d7,d0
	rts

DiskFontError:	lea	DiskFontErrText(pc),a0
	bsr.s	PutStr
	bra.s	CloseDOS
ScanArgsError:	lea	ScanArgsErrFmt(pc),a0
	lea	FmtBuf(pc),a1
	clr.l	(a1)
	move.b	-(a2),d2
	cmp.b	#"?",d2
	beq.s	OmmitPrintf
	move.b	d2,3(a1)
	bsr.s	Printf
OmmitPrintf:	lea	UsageText(pc),a0
	bsr.s	PutStr
	bra.s	CloseDOS
	
	cnop	0,4

PutStr:	move.l	a0,d2
	moveq	#-1,d3
GetTextLen:	addq.l	#1,d3
	tst.b	(a0)+
	bne.s	GetTextLen
	move.l	DOSBase(pc),a6
	jsr	_LVOOutput(a6)
	move.l	d0,d1
	beq.s	ExitToDOS
	jmp	_LVOWrite(a6)

	cnop	0,4

Printf:	lea	PutChProc(pc),a2
	lea	-256(sp),sp
	move.l	sp,a3
	move.l	4.w,a6
	jsr	_LVORawDoFmt(a6)
	move.l	sp,a0
	bsr.s	PutStr
	lea	256(sp),sp
	rts

	cnop	0,4

PatchOpenDiskFont:	bra.s	I_LOVE
	dc.b	'ZUZKA !',0
I_LOVE:	movem.l	d0-d7/a0-a6,-(sp)
	moveq	#0,d0
	move.l	a0,d6
	move.l	(a0),a0
OpenDiskFontGetFontNameLen:	addq.l	#1,d0
	tst.b	(a0)+
	bne.s	OpenDiskFontGetFontNameLen
	lea	FmtBuf(pc),a0
FmtBufLen:	addq.l	#1,d0
	tst.b	(a0)+
	bne.s	FmtBufLen
	move.l	d0,d7
	move.l	#MEMF_PUBLIC|MEMF_CLEAR,d1
	move.l	4.w,a6
	jsr	_LVOAllocMem(a6)
	tst.l	d0
	beq	OpenDiskFontErr1
	move.l	d0,a3
	move.l	d7,d0
	move.l	#MEMF_PUBLIC|MEMF_CLEAR,d1
	jsr	_LVOAllocMem(a6)
	tst.l	d0
	beq	OpenDiskFontErr2
	move.l	d0,d2
	move.l	d7,d0
	move.l	#MEMF_PUBLIC|MEMF_CLEAR,d1
	jsr	_LVOAllocMem(a6)
	tst.l	d0
	beq	OpenDiskFontErr3
	move.l	d0,a4
	moveq	#64,d0
	move.l	#MEMF_PUBLIC|MEMF_CLEAR,d1
	jsr	_LVOAllocMem(a6)
	tst.l	d0
	beq	OpenDiskFontErr4
	move.l	d0,a5
	moveq	#64,d0
	move.l	d6,a0
	move.l	a5,a1
	jsr	_LVOCopyMemQuick(a6)
	move.l	a3,(a5)
	move.l	d6,a0
	move.l	(a0),a0
FindOpenDiskFontExtension:	tst.b	(a0)+
	bne.s	FindOpenDiskFontExtension
FindOpenDiskFontExtPoint:	cmp.l	a1,a0
	beq.s	NoOpenDiskFontExtFound
	cmp.b	#".",-(a0)
	bne.s	FindOpenDiskFontExtPoint
	move.l	a0,d5
	move.l	a4,a1
CopyOpenDiskFontExtLoop:	move.b	(a0)+,(a1)+
	bne.s	CopyOpenDiskFontExtLoop
NoOpenDiskFontExtFound:	move.l	d6,a0
	move.l	(a0),a0
	move.l	d2,a1
CopyOpenDiskFontFileLoop:	cmp.l	d5,a0
	beq.s	CopyOpenDiskFontFileDone
	move.b	(a0)+,(a1)+
	bra.s	CopyOpenDiskFontFileLoop
CopyOpenDiskFontFileDone:	lea	FmtBuf(pc),a0
	movem.l	d2/a4,-(sp)
	move.l	sp,a1
	lea	PutChProc(pc),a2
	jsr	_LVORawDoFmt(a6)
	addq.l	#8,sp
	move.l	a5,a0
	move.l	56(sp),a6
	move.l	OSOpenDiskFont(pc),a1
	jsr	(a1)
	movem.l	d0-d1/a0-a1,-(sp)
	move.l	4.w,a6
	jsr	_LVOGetCC(a6)
	move.l	d0,d4
	moveq	#64,d0
	move.l	a5,a1
	jsr	_LVOFreeMem(a6)
	move.l	d7,d0
	move.l	a4,a1
	jsr	_LVOFreeMem(a6)
	move.l	d7,d0
	move.l	d2,a1
	jsr	_LVOFreeMem(a6)
	move.l	d7,d0
	move.l	a3,a1
	jsr	_LVOFreeMem(a6)
	movem.l	(sp)+,d0-d1/a0-a1
	tst.l	d0
	bne.s	OpenDiskFontOk
	movem.l	(sp)+,d0-d7/a0-a6
	move.l	OSOpenDiskFont(pc),-(sp)
	rts
OpenDiskFontOk:	movem.l	d0-d1,(sp)
	movem.l	a0-a1,32(sp)
	btst	#AFB_68010,AttnFlags+1(a6)
	movem.l	(sp)+,d0-d7/a0-a6
	beq.s	OpenDiskFont68000
	dc.w	$44C4								* move.w	d4,ccr
	rts
OpenDiskFont68000:	dc.w	$46C4				* move.w	d4,sr
	rts

	cnop	0,4

OpenDiskFontErr4:	move.l	d7,d0
	move.l	a4,a1
	jsr	_LVOFreeMem(a6)
OpenDiskFontErr3:	move.l	d7,d0
	move.l	d2,a1
	jsr	_LVOFreeMem(a6)
OpenDiskFontErr2:	move.l	d7,d0
	move.l	a3,a1
	jsr	_LVOFreeMem(a6)
OpenDiskFontErr1:	movem.l	(sp)+,d0-d7/a0-a6
	move.l	OSOpenDiskFont(pc),-(sp)
	rts

	cnop	0,4

PutChProc:	move.b	d0,(a3)+
	rts

OSOpenDiskFont:	dc.l	0
FmtBuf:	ds.b	256

	cnop	0,4

PatchOpenFont:	bra.s	I_LOVE_
	dc.b	'ZUZKA !',0
I_LOVE_:	movem.l	d0-d7/a0-a6,-(sp)
	move.l	a0,d6
	lea	-128(sp),sp
	move.l	sp,a3
	clr.l	(a3)
	lea	-128(sp),sp
	move.l	sp,d2
	clr.l	(sp)
	lea	-128(sp),sp
	move.l	sp,a4
	clr.l	(a4)
	lea	-64(sp),sp
	move.l	sp,a5
	moveq	#64,d0
	move.l	d6,a0
	move.l	a5,a1
	move.l	4.w,a6
	jsr	_LVOCopyMemQuick(a6)
	move.l	a3,(a5)
	move.l	d6,a0
	move.l	(a0),a0
FindOpenFontExtension:	tst.b	(a0)+
	bne.s	FindOpenFontExtension
FindOpenFontExtPoint:	cmp.l	a1,a0
	beq.s	NoOpenFontExtFound
	cmp.b	#".",-(a0)
	bne.s	FindOpenFontExtPoint
	move.l	a0,d5
	move.l	a4,a1
CopyOpenFontExtLoop:	move.b	(a0)+,(a1)+
	bne.s	CopyOpenFontExtLoop
NoOpenFontExtFound:	move.l	d6,a0
	move.l	(a0),a0
	move.l	d2,a1
CopyOpenFontFileLoop:	cmp.l	d5,a0
	beq.s	CopyOpenFontFileDone
	move.b	(a0)+,(a1)+
	bra.s	CopyOpenFontFileLoop
CopyOpenFontFileDone:	clr.b	(a1)
	lea	FmtBuf(pc),a0
	movem.l	d2/a4,-(sp)
	move.l	sp,a1
	lea	PutChProc(pc),a2
	jsr	_LVORawDoFmt(a6)
	addq.l	#8,sp
	move.l	a5,a0
	move.l	(128*3)+64+56(sp),a6
	move.l	OSOpenFont(pc),a1
	jsr	(a1)
	lea	(128*3)+64(sp),sp
	movem.l	d0-d1/a0-a1,-(sp)
	move.l	4.w,a6
	jsr	_LVOGetCC(a6)
	move.l	d0,d4
	movem.l	(sp)+,d0-d1/a0-a1
	tst.l	d0
	bne.s	OpenFontOk
	movem.l	(sp)+,d0-d7/a0-a6
	move.l	OSOpenFont(pc),-(sp)
	rts
OpenFontOk:	movem.l	d0-d1,(sp)
	movem.l	a0-a1,32(sp)
	btst	#AFB_68010,AttnFlags+1(a6)
	movem.l	(sp)+,d0-d7/a0-a6
	beq.s	OpenFont68000
	dc.w	$44C4								* move.w	d4,ccr
	rts
OpenFont68000:	dc.w	$46C4					* move.w	d4,sr
	rts

	cnop	0,4

OSOpenFont:	dc.l	0

PatchFontSize:	equ	*-PatchOpenDiskFont


	cnop	0,4

DOSBase:	dc.l	0
DiskFontBase:	dc.l	0
GfxBase:	dc.l	0

DOSName:	dc.b	'dos.library',0
GfxName:	dc.b	'graphics.library',0
DiskFontName:	dc.b	'diskfont.library',0

UsageText:	dc.b	"Usage is: CzechPatch [ON|OFF] [-f fmt]",10,10
	dc.b	"where ON or OFF either enable/disable the tool. If this is omitted,",10
	dc.b	"it will swap the current state.",10
	dc.b	"[-f fmt] is an alternative format string if you dislike the default:",10
	dc.b	"%s-CZ%s",10
	dc.b	"This means, if you open e.g. zuzka.font, it will redirect to:",10
	dc.b	"zuzka-CZ.font",10,0
WelcomeText:	dc.b	"CzechPatch v1.0 © 2000 by Basty/Seasons (cdgs.basty@usa.net).",10,10
	dc.b	"CzechPatch patches graphics.library's OpenFont() and diskfont.library's",10
	dc.b	"OpenDiskFont() in order to scan first for a font with an added name and if",10
	dc.b	"that fails, the font normally passed. I wrote this program for support",10
	dc.b	"ISO Latin II characters (e.g. czech language). This way you simply copy your",10
	dc.b	"ISO Latin II fonts to FONTS: dir, all named like: topaz-CZ.font,",10
	dc.b	"sapphire-CZ.font and all programs will automatically use them. So you",10
	dc.b	"don't have to adjust your progams font settings. Now simply use a czech",10
	dc.b	"version (locale) of your program and you've ISO II support !",10
	dc.b	"Dedicated to Zuzana Burkertová, I love you !",10,10,0
DiskFontErrText:	dc.b	"Couldn't open diskfont.library !",10,0
ScanArgsErrFmt:	dc.b	'Error while parsing: %lc !',10,10,0
Fmt:	dc.b	'%s-CZ%s',0
CurrentOnText:	dc.b	"Current state is enabled so I will disable it...",0
CurrentOffText:	dc.b	"Current state is disabled so I will enable it...",0
OutOfMemText:	dc.b	"out of memory!",10,0
DoneText:	dc.b	"done.",10,0
TurningOnText:	dc.b	"Installing patch...",0
TurningOffText:	dc.b	"Removing patch...",0
AlreadyOnText:	dc.b	"already installed!",10,0
AlreadyOffText:	dc.b	"not installed or vector changed !",10,0
