;========================================================================
;
;		REPICON 1.3
;
;		© 1994 by Timm S. Müller
;
;========================================================================

		include	"workbench/workbench.i"
		include	"dos/dos.i"
		include	"exec/exec.i"
		include	"lvo/exec_lib.i"
		include	"lvo/icon_lib.i"
		include	"lvo/dos_lib.i"

;========================================================================

		section "a",code

;========================================================================

		move.l	4.w,a6

		moveq	#0,d4			; rdarg-Ptr

		moveq	#20,d7			; returncode

;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
;
;		open all


		moveq	#prg_SIZEOF,d0		; so we can stay pure
		move.l	#MEMF_PUBLIC+MEMF_CLEAR,d1
		jsr	_LVOAllocMem(a6)
		move.l	d0,a5
		beq	quit

		lea	iconlibname(pc),a1
		moveq	#0,d0
		jsr	_LVOOpenLibrary(a6)
		move.l	d0,prg_iconbase(a5)
		beq.s	closeall2		; no icon library - bad luck

		lea	doslibname(pc),a1
		moveq	#36,d0			; >=36 should be available
		jsr	_LVOOpenLibrary(a6)
		move.l	d0,(a5)			; = prg_dosbase(a5)
closeall2	beq.w	closeall

;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
;
;		read args


		move.l	d0,a6

		lea	template(pc),a0		; get arguments and check'em
		move.l	a0,d1
		moveq	#prg_argarray1,d2
		add.l	a5,d2
		moveq	#0,d3
		jsr	_LVOReadArgs(a6)

		lea	txt_noargs(pc),a0
		move.l	d0,d4
		beq.b	textoutput

;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
;
;		remove .info from file names


		move.l	prg_iconbase(a5),a6

		move.l	prg_argarray2(a5),a0
		bsr.w	stripinfo
		move.l	a0,a4

		move.l	prg_argarray1(a5),a0
		bsr.w	stripinfo
		move.l	a0,a3


;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
;
;		load icons


		jsr	_LVOGetDiskObject(a6)
		lea	txt_notfound1(pc),a0
		move.l	d0,d5
		beq.s	textoutput		; first icon not found

		move.l	a4,a0
		jsr	_LVOGetDiskObject(a6)
		lea	txt_notfound2(pc),a0
		move.l	d0,d6
		bne.s	loadok

		move.l	d5,a0			; second icon not found,
		jsr	_LVOFreeDiskObject(a6)	; so free first icon

		bra.s	textoutput

loadok

;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
;
;		copy the first icon's gadget structure
;		to the the second icon's gadget structure,
;		then save second icon


		move.w	#do_Gadget,a0
		move.l	a0,a1
		add.l	d5,a0
		add.l	d6,a1

		moveq	#gg_SIZEOF/2-1,d0
coplop		move.w	(a0)+,(a1)+		; copy it
		dbf	d0,coplop

		move.l	a4,a0			; save desticon
		move.l	d6,a1
		jsr	_LVOPutDiskObject(a6)
		move.l	d0,d2

		move.l	d5,a0
		jsr	_LVOFreeDiskObject(a6)	; free both icons
		move.l	d6,a0
		jsr	_LVOFreeDiskObject(a6)		

		lea	txt_notsaved(pc),a0

		moveq	#0,d7

		tst.l	d2			; saving successful?
		bne.b	closeall

		moveq	#20,d7

;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
;
;		print out text


textoutput	move.w	(a0)+,d3
		move.l	a0,d2
		move.l	(a5),a6			; = prg_dosbase(a5),a6
		jsr	_LVOOutput(a6)
		move.l	d0,d1
		jsr	_LVOWrite(a6)

;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
;
;		close down all


closeall	move.l	d4,d1
		beq.b	ca_no0
		move.l	(a5),a6
		jsr	_LVOFreeArgs(a6)

ca_no0		move.l	4.w,a6
		move.l	(a5),d0			; = prg_dosbase(a5)
		beq.s	ca_no1
		move.l	d0,a1
		jsr	_LVOCloseLibrary(a6)

ca_no1		move.l	prg_iconbase(a5),d0
		beq.s	ca_no2
		move.l	d0,a1
		jsr	_LVOCloseLibrary(a6)

ca_no2		move.l	a5,a1
		moveq	#prg_SIZEOF,d0
		jsr	_LVOFreeMem(a6)

quit		move.l	d7,d0
		rts

;-----------------------------------------------------------------------
;
;		remove .info
 

stripinfo	move.l	a0,a2

seekend		tst.b	(a2)+
		bne.s	seekend

		subq.l	#1,a2
		lea	infotext-2(pc),a1

		moveq	#5,d1

advance		subq.w	#1,d1
		bpl.s	cont

		clr.b	(a2)
		rts

cont		move.b	-(a2),d0
		addq.l	#2,a1
		cmp.b	(a1),d0
		beq.s	advance
		cmp.b	1(a1),d0
		beq.s	advance
		rts

;========================================================================

		dc.b	0,'$VER: RepIcon 1.3 (18-01-1994)',0
		even

;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

doslibname	dc.b	'dos.library',0
		even

iconlibname	dc.b	'icon.library',0
		even

;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

		STRUCTURE	prgstruc,0
		APTR		prg_dosbase
		APTR		prg_iconbase
		LONG		prg_argarray1
		LONG		prg_argarray2
		LABEL		prg_SIZEOF

;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

template	dc.b	'SOURCEICON/A,DESTICON/A',0
		even

infotext	dc.b	'oOfFnNiI..'
		even

;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

txt_noargs	dc.w	txt_noargslen				; Length
		dc.b	10," RepIcon 1.3 (18-01-1994)",10
		dc.b	" © 1994 by Timm S. Müller",10,10
		dc.b	" Copies the image data contained in SOURCEICON",10
		dc.b	" to a given DESTICON. All other icon settings",10
		dc.b	" in DESTICON stay untouched.",10,10
		dc.b	" Template: RepIcon SOURCEICON/A,DESTICON/A",10,10
txt_noargslen	=*-txt_noargs-2
		even

txt_notfound1	dc.w	txt_notfound1len
		dc.b	" Couldn't load source icon.",10
txt_notfound1len	=*-txt_notfound1-2
		even

txt_notfound2	dc.w	txt_notfound2len
		dc.b	" Couldn't load destination icon.",10
txt_notfound2len	=*-txt_notfound2-2
		even

txt_notsaved	dc.w	txt_notsavedlen
		dc.b	" Couldn't save destination icon.",10
txt_notsavedlen	=*-txt_notsaved-2
		even
