* Programmheader
*
*	Name:		GrabKick
*	Author:		SDI
*	Distribution:	PD
*	Description:	read Kickstart out of ROM and write it to file
*	Compileropts:	-
*	Linkeropts:	-
*	CLI-Parameter:	FILENAME
*
* vers    date   length
* 1.1   23.12.92  10612	: ANSI-C-Version made by Ralf Gruner
*
*	I used name and idea of this program, but not the ANSI-C code,
*	because Amiga-ROM functions need no program space: 10612 --> 880 byte
*	My program makes no output to user! It can fail at some positions,
*	but these fails are so seldom that error messages aren't neccessary!
*	The only needed error message (disk full) is made by system function.
* 1.2   07.08.95    880	: new calculation for Revisionnummer works around
*	calculation-error of MaxonC++
* 1.3   03.12.95    244	: made new with Maxon Assembler
* 1.4   04.12.95    236	: made shorter
* 1.5   10.12.95    228	: made shorter
* 1.6   19.12.95    300	: now also working from WorkBench
* 1.7   05.01.96    332	: included CLI parameter FILENAME, removed some bugs
* 1.8   10.02.96    508 : Asl-Requester on Workbench-start, new Icon
* 1.9   11.02.96    488 : made shorter, commented text
* 1.10  12.02.96    484 : made shorter
* 1.11  14.02.96    476 : CMPA.L #0,Ax replaced by MOVE.L Ax,D0
* 1.12  17.02.96    476 : BUG: Open_Mode is .L and not .W !
* 1.13  08.03.96    472 : a little bit shorter, tricky LibName method,
*	translated source to english
* 1.14  11.04.96    472 : overworked the source code, no new binary file
* 1.15  11.06.96    496 : removed a .L / .W error in name-creation, removed
*	libname trick, added version-string to ASL-Req. title
* 1.16  21.11.96    504 : added correct Return codes (#20 on failing)
* 1.17  07.01.97    508 : added second exe copying the ROM first to RAM,
*	corrected WB start Enforcer hit

	NOLIST
	INCLUDE	AINCLUDE:IncDirs.i
	INCLUDE	dos/dosextens.i
	INCLUDE lvo.i
	INCLUDE dos/dos.i
	INCLUDE	exec/execbase.i
	INCLUDE exec/memory.i
	INCLUDE	workbench/startup.i
	INCLUDE	workbench/workbench.i
	INCLUDE	libraries/asl.i
	LIST

	SECTION "GrabKick",CODE
	MOVEA.L	A0,A5		;store it
	MOVE.L	D0,D6		;store it
	MOVEQ	#0,D5		;no start string in D5
	MOVEQ	#RETURN_FAIL,D7		;return code - everytime fail
	MOVE.L	D7,-(A7)
	MOVEQ	#0,D7				;D7 - clear (later StartupM)
	MOVEA.L	4.W,A6
	MOVEA.L	ThisTask(A6),A4
	TST.L	pr_CLI(A4)		;are we on WorkBench ?
	BNE.B	.CLI
	LEA	pr_MsgPort(A4),A0
	JSR	_LVOWaitPort(A6)	;wait for StartUp message
	LEA	pr_MsgPort(A4),A0
	JSR	_LVOGetMsg(A6)		;get Message into D7
	MOVE.L	D0,D7				;D7 - WBStartUp message
	BRA.B	.start
.CLI	SUBQ.W	#1,D6
	BEQ.B	.start		;if only one char, no parameter is given
	CLR.B	(A5,D6.W)		;make C-string, last char is 0
	MOVE.L	A5,D5				;D5 - CLI parameter name
.start	LEA	KICKEND(PC),A2			;A2 - KickStr-End
	MOVEQ	#33,D0
	LEA	DOSNAME(PC),A1
	JSR	_LVOOpenLibrary(A6)	;open dos.library
	TST.L	D0			;fatal Error : librarybase = 0?
	BEQ.B	.goend			;instead of BEQ.W ENDE (-2 Byte)
	MOVEA.L	D0,A5				;A5 - DosBase
	TST.L	D7
	BEQ.B	.NoWB
	MOVEA.L	D7,A0
	MOVEA.L	sm_ArgList(A0),A0	;on WB-start get PROGDIR:
	MOVE.L	A0,D0			;instead of CMPA.L #0,A0 (-4 Byte)
.goend	BEQ.W	ENDE
	MOVE.L	(A0),D1			;set PROGDIR as current
	MOVEA.L	A5,A6
	JSR	_LVOCurrentDir(A6)
.NoWB	LEA	$01000000,A3		;Kick-ROM End is every time same!!!
	MOVE.L	-20(A3),D3			;D3 - RomSize
	SUBA.L	D3,A3				;A3 - Romstart
	MOVEQ.L	#0,D4
	MOVE.W	12(A3),D4			;D4 - Romvers
	MULU	#1000,D4
	ADD.W	14(A3),D4			;D4 - Fullversion
.LOOP					;calculate kick..... name
	MOVE.L	D4,D2
	DIVU	#10,D2			;divide with ten
	MOVEQ	#0,D4
	MOVE.W	D2,D4			;store in D4
	SWAP	D2
	ADD.B	D2,-(A2)		;remainder into namefile
	TST.W	D4
	BNE.B	.LOOP
;end of loop					;D4 - not used
	MOVEA.L	D5,A2				;A2 - CLI name or
	TST.L	D5
	BNE.B	.GoOn
	LEA	KICKSTR(PC),A2			;A2 - kick.....
.GoOn	MOVEQ	#0,D6				;D6 - clear (later AslBase)
	MOVEQ	#0,D5				;D5 - clear (later Lock)
	TST.L	D7			;if not WB then overgo asl-requester
	BEQ.B	Open
	MOVEA.L	4.W,A6
**1111**
*	LEA	DOSNAME(PC),A1		;ugly trick : overwrite dos. with asl.
*	MOVE.L	#'asl.',(A1)		;saves some bytes!
	LEA	ASLNAME(PC),A1
**1111**
	MOVEQ	#37,D0
	JSR	_LVOOpenLibrary(A6)	;open asl.library
	MOVE.L	D0,D6				;D6 - AslBase
	BEQ.B	Open			;failed ?, then like CLI
	MOVEA.L	D6,A6
	MOVEQ	#ASL_FileRequest,D0
	LEA	TAGITEM(PC),A0
	JSR	_LVOAllocAslRequest(A6)	;create Request-struct
	MOVEA.L	D0,A4				;A4 - FileRequester
	TST.L	D0
	BEQ.B	Open			;failed ?, then like CLI
	MOVEA.L	A4,A0
	LEA	TAGITEM(PC),A1
	JSR	_LVOAslRequest(A6)	;call requester
	TST.L	D0
	BEQ.B	CLEANUP			;failed?, end!!!, it's user-abort
	MOVE.L	fr_Drawer(A4),D1
	MOVEQ	#SHARED_LOCK,D2		;share the directory lock
	MOVEA.L	A5,A6
	JSR	_LVOLock(A6)		;get lock on directory
	MOVE.L	D0,D5				;D5 - Lock
	BEQ.B	CLEANUP			;failed?, end!!!, else it makes shit
	MOVE.L	D5,D1
	JSR	_LVOCurrentDir(A6)	;make selected directory current
	MOVEA.L	fr_File(A4),A2			;A2 - asl name
Open
**3333**
*	MOVEA.L	4.W,A6
*	MOVE.L	D3,D0			;size of memory
*	MOVEQ	#MEMF_PUBLIC,D1		;memory type
*	JSR	_LVOAllocMem(A6)	;get memory
*	MOVE.L	D0,D4
*	BEQ.B	CLEANUP
*	MOVEA.L	A3,A0			;source buffer
*	MOVEA.L	D4,A1			;destination buffer
*	MOVE.L	D3,D0			;size
*	JSR	_LVOCopyMemQuick(A6)	;copy the memory
*	MOVEA.L	D4,A3			;new source is now RAM
**3333**
	MOVE.L	A2,D1			;name parameter
	MOVE.L	#MODE_NEWFILE,D2	;mode parameter
	MOVEA.L	A5,A6			;set DosBase
	JSR	_LVOOpen(A6)
	MOVE.L	D0,D4				;D4 - FileHandle
	BEQ.B	CLEANUP2
				;quantity parameter D3 has already it's value
	MOVE.L	D4,D1			;filehandle parameter
	MOVE.L	A3,D2			;buffer parameter
	JSR	_LVOWrite(A6)
	MOVE.L	D0,D2				;D2 - returnstate of Write
	MOVE.L	D4,D1			;filehandle parameter
	JSR	_LVOClose(A6)
	CMP.L	D3,D2			;D2 != D3, then delete File
	BEQ.B	ALL_OK
	MOVE.L	A2,D1
	JSR	_LVODeleteFile(A6)
	BRA.B	CLEANUP2
ALL_OK	CLR.L	(A7)			; all ok - clear return code
CLEANUP2
**3333**
*	MOVEA.L	4.W,A6
*	MOVEA.L	A3,A1		;pointer to memory
*	MOVE.L	D3,D0		;size of memory
*	JSR	_LVOFreeMem(A6)	;free allocated memory
*	MOVEA.L	A5,A6		;reset DosBase for UnLock
**3333**
CLEANUP
	MOVE.L	D5,D1
	BEQ.B	.next			;DosBase is already set, when D5 != 0
	JSR	_LVOUnLock(A6)		;free lock
.next	TST.L	D6
	BEQ.B	.clsdos
	MOVE.L	A4,D0		;instead oft CMPA.L #0,A4 (-4 Byte)
	BEQ.B	.clsasl
	MOVEA.L	A4,A0
	MOVEA.L	D6,A6
	JSR	_LVOFreeAslRequest(A6)	;close filerequester
.clsasl	MOVEA.L	4.W,A6
	MOVEA.L	D6,A1
	JSR	_LVOCloseLibrary(A6)	;close asl.library
.clsdos	MOVEA.L	4.W,A6
	MOVEA.L	A5,A1
	JSR	_LVOCloseLibrary(A6)	;close dos.library
ENDE
	TST.L	D7			;come we from Workbench ?
	BEQ.B	.exit			;no - was CLI

	JSR	_LVOForbid(A6)		;make ReplyMsg save
	MOVEA.L	D7,A1			;get Message into A1
	JSR	_LVOReplyMsg(A6)	;end program

.exit	MOVE.L	(A7)+,D0
	RTS	
DOSNAME	DC.B	'dos.library',0
**1111**
ASLNAME	DC.B	'asl.library',0
**1111**
KICKSTR	DC.B	'kick00000'
KICKEND	DC.B	0
TAGITEM	DC.L	ASLFR_InitialFile,KICKSTR
**2222**
	DC.L	ASLFR_TitleText,VERSION
**2222**
	DC.L	0
	DC.B	'$VER: '
VERSION	DC.B	'GrabKick 1.17 (07.01.97) (PD) by SDI'
**2222**
	DC.B	0
**2222**
	; 0 byte of the string is not needed, because in the file the header
	; does same as a 0 byte and in running state the string isn't used

* in the lines between **....** remove stars and set stars to the lines
* without, for:
* **1111** : remove the ugly asl.library name trick
* **2222** : set ASL titletext to programname (needs 12 bytes more)
* **3333** : set ROM->RAM->FILE copy mode
	END

