	include	'libraries/allofit_lvo.i'
	include	'dos/dos.i'

; This source is the source for each and every '-----BEGIN' file created.
; Returns RETURN_ERROR if something went wrong or the check failed.
; Will check that there indeed IS a blank line JUST after the ----BEGIN
; line first in the sumfile.
; There is still some room for optimizing, but doing things this way keeps
; the file more readable and easier to understand

; The Code hunk is equal from file to file, but the REXX script modifies
; the Data hunk according to the name of the sumfile. Some magic, but
; it's not difficult at all, and it works...:)

; © 1994 Jens T. Berger Thielemann
; Distributed under the GNU General Public License.
; To contact the author:
; <jensthi@ifi.uio.no>
; or
;   Jens Berger
;   Spektrumveien 4
;   N-0666 Oslo
;   Norway
; or
; Send   a   message   in   POST   to   J.    BERGER   on  Trashcan  (A)BBS
; [(+47) 22 25 74 78 or (+47) 22 25 88 22].
; NO WARRANTIES AT ALL FOR FUNCTIONALITY OR FITNESS FOR A SPECIFIC PURPOSE

; This will make the program more easy to read

SumName		equr	d6
ErrorCode	equr	d7
FileHandle	equr	d4

StackNeeded	equ	40

	SECTION	"TEXT",CODE

Begin
	moveq	#RETURN_FAIL,ErrorCode	; Our current error status
	move.l	4.w,a6			; Execbase to a6
	lea	dosname(pc),a1		; Want to open dos.library
	callib	OldOpenLibrary		; Any version OK

	tst.l	d0			; Did we get it?
	beq.w	DosLibError		; If no, return to shell

	move.l	d0,a6			; DOSBase to a6

	moveq	#RETURN_ERROR,ErrorCode	; At least we've got dos...


	  ***********   Get ptr to name of sumfile    ***********

					; A bit tricky, but only way to
	move.l	Begin-4(pc),d1		; have more than one hunk without
	add.l	d1,d1			; reloc hunks. Relies on SegLists &
	add.l	d1,d1			; BPTR's from LoadSeg. Gives a ptr
	addq.l	#4,d1			; to the sumfile's name in d1/d6.
	move.l	d1,SumName		; Look in DOS manuals to learn
					; more.

	move.l	#MODE_OLDFILE,d2	; Open non-destructively
	callib	Open			; Get a filehandle to the sumfile

	move.l	d0,FileHandle		; Store it in d4
	beq.b	OpenError		; Abort if we didn't get it


	  ***********     Read the first lines        ***********

	lea	-StackNeeded(sp),sp	; Reserve space on stack
	move.l	d0,d1			; Filehandle to d1
	move.l	sp,d2			; Stack space reserved to d2
	moveq	#36,d3			; We'll read 36 bytes
	callib	Read			; Read'em
	cmp.l	d0,d3			; Check that we actually GOT
	bne.b	ReadError		; 36 bytes, if not: Abort

	move.l	sp,a0			; ptr to data read in a0
	lea	begtxt(pc),a1		; data to compare with in a1


	  ***********        Compare the texts        ***********

	moveq	#begend-begtxt-1,d2	; This is how far we'll check
CmpLoop
	move.b	(a0)+,d0		; get a 'read' byte into d0
	cmp.b	(a1)+,d0		; check it with the comparison
	bne.b	ChkError		; if not equal, abort and report
	dbf	d2,CmpLoop		; error, else, continue

	moveq	#RETURN_OK,ErrorCode	; if they were equal, status = OK
	bra.b	AllOK			; close everything


	  ***********  Print eventual error messages  ***********

OpenError
	lea	ErrTxt_FileOpen(pc),a0	; text relevant into a0
	pea	CloseDosLib(pc)		; we wish to resume here
	bra.b	PrintError		; print it...
ReadError
	lea	ErrTxt_FileRead(pc),a0	; da capo!
	pea	CloseFH(pc)		; we wish to resume here
	bra.b	PrintError
ChkError
	lea	ErrTxt_Check(pc),a0	; da capo once more...
	pea	RestoreStack(pc)	; we wish to resume here
PrintError
	bsr.b	PrintTxt		; print nullterm. text
	move.l	SumName,a0		; name of sumfile in a0
	bsr.b	PrintTxt		; print it
	lea	NewLine(pc),a0		; simple linefeed
	bra.b	PrintTxt		; note that we'll return according
					; to the pea above

AllOK					; no errors during execution


	  ***********        Close everything         ***********

RestoreStack
	lea	StackNeeded(sp),sp	; restore stack

CloseFH
	move.l	FileHandle,d1		; filehandle to d1
	callib	Close			; close the sumfile

CloseDosLib
	move.l	a6,a1			; DOSBase to a1
	move.l	4.w,a6			; ExecBase to a6
	callib	CloseLibrary		; Close dos.library


DosLibError
	move.l	ErrorCode,d0		; return status to Shell
	rts				; bye!


	  ***********       Printing Subroutine       ***********

PrintTxt
	move.l	a0,d2			; ptr to text in d2
	moveq	#-1,d3			; count number of chars
PrCountLoop				; to be printed, works for
	tst.b	(a0)+			; text < 32kB
	dbeq	d3,PrCountLoop		; loop till NULL
	not.l	d3			; not it
	callib	Output			; get output handle
	move.l	d0,d1			; put it in d1
	jmp	_LVOWrite(a6)		; write & return

; A valid PGP clearsigned message MUST begin with the text below and TWO
; linefeeds (as here). Used to compare with above

begtxt
	dc.b	'-----BEGIN PGP SIGNED MESSAGE-----',10,10
begend

; name of dos.library.

dosname
	dc.b	'dos.library',0

; Text output if we can't open the sumfile

ErrTxt_FileOpen
	dc.b	"Can't open sumfile ",'"',0

; Text output if we can't read from the sumfile

ErrTxt_FileRead
	dc.b	"Can't read from sumfile ",'"',0

; Text output if the check fails

ErrTxt_Check
	dc.b	'CHECK FAILED IN SUMFILE "',0

; As all the error messages end with a '"', to which we concatenate the
; name of the sumfile, we must end it with an '"' and a linefeed.

NewLine
	dc.b	'"',10,0


	SECTION	"NAME",DATA

; only for testing purposes, the REXX script automagically inserts a the
; name of the currently used sumfile. The hex output in the REXX script
; is without the contents of this hunk.

Name
	dc.b	"foobar.sum",0
	END
