	INCDIR 	INCLUDE:
	INCLUDE	libraries/xpk.i
	INCLUDE	lvo/exec_lib.i	
;-----------------------------------------------------------------------
;		open xpk libraries
;-----------------------------------------------------------------------
	move.l	4,a6
	lea	 dosname,a1
	moveq	#0,d0			; version egal
	jsr	_LVOOpenLibrary
	LEA 	cannotopendos.msg,a0
	move.l	d0,dosbase		;store dos library base
	beq 	cleanup			; Error =>Exit
	
	lea	xpkname,a1
	moveq	#0,d0			; version egal
	jsr	_LVOOpenLibrary		; open xpkmaster.library
	lea	cannotopenxpk.msg,a0
	move.l	d0,xpkbase		;store xpk library base
	beq cleanup			; Error => exit
;-------------------------------------------------------------------------
;                 Loading & decrunching file
;-------------------------------------------------------------------------
	move.l	xpkbase,a6		; xpkbase to a6
	lea	tags,a0
	jsr	_LVOXpkUnpack
	
	tst.l	d0
	beq	noerrorloadingfile
;-------------------------------------------------------------------------
;		Error while loading file
;-------------------------------------------------------------------------
	lea	errbuf,a0
	move.l	a0,a1

lxpk1:	tst.b	(a1)+
	bne lxpk1
	move.b	#10,-(a1)	;convert it to LF
	bra	cleanup
	
;-------------------------------------------------------------------------
;		No Error loading file
;-------------------------------------------------------------------------
noerrorloadingfile:
	move.l	outbuf,a0
	moveq	#0,d0
	move.l	outlen,d7
	
lxpk2:	moveq	#0,d1
	move.b	(a0)+,d1
	add.l	d1,d0		;calculate sum ...
	subq.l	#1,d7
	bne	lxpk2
;------------- number is in d0 ---------------------------------------
	;bsr	printnumber	;output
	suba.l	a0,a0		;no error msg	
;---------------------------------------------------------------------
; cleanup by error& Exit
;---------------------------------------------------------------------
cleanup:	jsr	_LVOFreeMem
		move.l	xpkbase,a0
		beq	closedoslib		;xpk was not open
		move.l	d0,a1
		jsr	_LVOCloseLibrary
closedoslib:	move.l	dosbase,d0
		beq	doswasnotopen
		move.l	d0,a1
		jsr	_LVOCloseLibrary
doswasnotopen:	moveq	#0,d0			; no error code!
		rts	
;---------------------------------------------------------------------
; variablen
;---------------------------------------------------------------------
	SECTION xpkdata,data
	
dosname:	dc.b	'dos.library',0
xpkname:	XPKNAME
cannotopendos.msg:	dc.b	'can not open dos.library',10,0
cannotopenxpk.msg:	dc.b	'can not open xpkmaster.library',10,0

outbuf:		ds.l	1
outlen:		ds.l	1
outbuflen:	ds.l	1
errbuf:		ds.b	XPKERRMSGSIZE+1		;+1 to make room for LF
namebuffer:	ds.b	12			; space for 10digits+LF+0
dosbase:	dc.l	1
xpkbase:	dc.l	1

;powertentable:	dc.l	1000000000
;		dc.l	100000000
;		dc.l	10000000
;		dc.l	1000000
;		dc.l	100000
;		dc.l	10000
;		dc.l	1000
;		dc.l	100
;		dc.l	10
;		dc.l	1
tags:		dc.l	XPK_InName
		dc.l	0			; Name to be read
		dc.l	XPK_GetError,errbuf	;pointer to error message buffer
		dc.l	XPK_GetOutBuf
		dc.l	outbuf
		dc.l	XPK_GetOutLen