	incdir	kar:
	include	minimac.i

dos	equr	d7			; Dos base
int	equr	d6			; Intuition base
exe	equr	d5			; Exec base
ret	equr	d4			; Here is always kept actual Returncode
arg	equr	a5			; Arg returned by ReadArgs
mem	equr	a4			; Operating mem


	moveq	#20,ret

	move.l	4.w,exe
	move.l	exe,a6

	openlib	dos,37			; Open dos
	move.l	d0,dos
	beq	deaddos

	move.l	#4096,d0		; Allocating memory for tags
	move.l	#MEMF_CLEAR,d1
	call	AllocVec
	tst.l	d0
	beq	deadalo
	move.l	d0,mem

	openlib	intuition,36		; Open intuition
	move.l	d0,int
	beq.s	deadint

	lea	templ(PC),a0		; Reading args
	move.l	a0,d1
	move.l	mem,a0			; with use of our allocated mem
	move.l	a0,d2
	moveq	#0,d3
	call	dos,ReadArgs
	move.l	d0,arg
	beq.s	badarg

	move.l	mem,a3			; Testing for CLOSE switch
	tst.l	(a3)+
	bne.s	close

	lea	12(mem),a2		; Place for tags
	move.l	a2,a1

	move.l	#SA_ErrorCode,(a2)+	; Error code if open not succesful
	lea	4064(mem),a0		; Place for error code is beyond tags
	move.l	a0,(a2)+		; Give them pointer to that place

	move.l	#SA_PubName,(a2)+	; Setting name of the screen (tag)
	move.l	(a3)+,(a2)+		; Specify the name
	move.l	(a3),a3			; TAGS/N/M -> A3
	move.l	a3,d0			; Are there any?
	beq.s	tagsok

taglop	move.l	(a3)+,a0		; Address of the next tag
	move.l	a0,d0			; if exists...
	beq.s	tagsok
	move.l	(a0),(a2)+		; And add the tag to the others
	bra.s	taglop


tagsok	move.l	d0,(a2)+		; We finnished tag copying
	move.l	d0,(a2)+		; So add terminating TAG_END
	call	int,OpenScreenTagList
	tst.l	d0
	bne.s	scropen

	moveq	#10,ret			; Decreasing fail number
	add.l	4064(mem),ret		; Adding errorcode to returncode
	bra.s	deadscr			; And bailing out

scropen	move.l	d0,a0
	moveq	#0,d0
	call	PubScreenStatus		; It will make the screen public

okend	moveq	#0,ret

deadscr	move.l	arg,d1
	call	dos,FreeArgs

deadarg	move.l	int,a1
	call	exe,CloseLibrary

deadint	move.l	mem,a1
	call	exe,FreeVec

deadalo	move.l	dos,a1
	call	exe,CloseLibrary

deaddos	move.l	ret,d0
	rts

badarg	lea	argerr(PC),a0
	move.l	a0,d1
	call	PutStr
	bra.s	deadarg

close	moveq	#10,ret
	move.l	(a3)+,a0
	call	int,LockPubScreen
	move.l	d0,d2
	beq.s	deadscr
	move.l	d0,a1
	sub.l	a0,a0
	call	UnlockPubScreen
	move.l	d2,a0
	call	CloseScreen
	bne.s	okend
	bra.s	deadscr


	LIBN	dos
	LIBN	intuition

argerr	dc.b	'Bad args.',10,0
templ	dc.b	'CLOSE/S,NAME/A,TAGS/M/N',0
