
;*************************************
;*                                   *
;* program: AddIcon                  *
;* author:  ScoTT/Inferno & [BLA]²   *
;*          (Marcin Ochocki)         *
;* version: 1.51                     *
;* date:    96.10.22                 *
;* short:   this is small but useful *
;*          DOS command to adding    *
;*          icons to your files,     *
;*          dirs or devices.         *
;*                                   *
;* ******* BLABLA PRODUCTION ******* *
;*                                   *
;*************************************

	INCDIR	'INCLUDE:'
	include	'exec/exec.i'
	include	'exec/exec_lib.i'
	include	'dos/dos.i'
	include	'dos/dos_lib.i'

_SysBase	=	4
MODE_NEW	=	1006
MODE_OLD	=	1005

	move.l	a0,adress

	move.l	_SysBase,a6
	lea	DosName,a1
	move.l	#37,d0
	jsr	OpenLibrary(a6)
	move.l	d0,_DOSBase
	tst.l	d0
	beq.w	no_DOS

	move.l	#iconname,a1
	jsr	GetArgs
	tst.l	d0
	bne	usage

	move.l	#iconname,a1
find
	tst.b	(a1)+
	bne	find
	sub.l	#1,a1
	lea	fileicon,a2
	jsr	copyinfo

	move.l	_DOSBase,a6
	move.l	#iconpath,d1
	move.l	#ACCESS_READ,d2
	jsr	Lock(a6)
	tst.l	d0
	beq	usage
	move.l	d0,iconlock
readicon
	move.l	_DOSBase,a6
	move.l	#iconpath,d1	
	move.l	#MODE_OLD,d2
	jsr	Open(a6)
	tst.l	d0
	beq	closeDOS	
	move.l	d0,iconhandle

	move.l	iconhandle,d1
	clr.l	d2
	moveq.l	#OFFSET_END,d3
	jsr	Seek(a6)
	move.l	iconhandle,d1
	clr.l	d2
	moveq.l	#OFFSET_BEGINNING,d3
	jsr	Seek(a6)
	move.l	d0,icon_length

	move.l	_SysBase,a6
	move.l	#MEMF_CLEAR+MEMF_PUBLIC,d1
	move.l	icon_length,d0
	jsr	AllocMem(a6)
	tst.l	d0
	beq	closeDOS
	move.l	d0,membase

	move.l	_DOSBase,a6
	move.l	iconhandle,d1
	move.l	membase,d2
	move.l	icon_length,d3
	jsr	Read(a6)

	move.l	#filename,a1
	jsr	GetArgs
	cmp.l	#-1,d0
	beq	usage

next_file
	move.l	_DOSBase,a6
	move.l	#filename,d1
	move.l	#ACCESS_READ,d2
	jsr	Lock(a6)
	tst.l	d0
	beq	closeDOS
	move.l	d0,filelock
	
	lea	filename,a1
copy_dir
	tst.b	(a1)+
	bne	copy_dir
	sub.l	#2,a1
	cmp.b	#':',(a1)
	bne	not_device
	add.l	#1,a1
	lea	dev_icon,a2
copydevicon
	move.b	(a2)+,(a1)+
	bne	copydevicon
	bra	opendest
not_device
	cmp.b	#'/',(a1)
	beq	addinfo
	add.l	#1,a1
addinfo
	lea	fileicon,a2
	jsr	copyinfo

opendest
	move.l	_DOSBase,a6
	move.l	filelock,d1
	jsr	UnLock(a6)

	move.l	#filename,d1
	move.l	#MODE_NEW,d2
	jsr	Open(a6)
	tst.l	d0
	beq	closeDOS
	move.l	d0,dest_iconhandle
	
	move.l	dest_iconhandle,d1
	move.l	membase,d2
	move.l	icon_length,d3
	jsr	Write(a6)

	move.l	dest_iconhandle,d1
	jsr	Close(a6)

	move.l	#filename,a1
	jsr	GetArgs
	cmp.l	#-1,d0
	bne	next_file

closeDOS

	move.l	iconlock,d1
	jsr	UnLock(a6)

	move.l	_SysBase,a6
	move.l	_DOSBase,a1
	jsr	CloseLibrary(a6)

freememory
	move.l	membase,a1
	move.l	icon_length,d0
	jsr	FreeMem(a6)
no_DOS
	moveq	#0,d0
	rts
usage
	move.l	_DOSBase,a6		
	jsr	Output(a6)
	move.l	d0,windowbase

	move.l	#startmsg,d2
	move.l	#endmsg-startmsg,d3
	move.l	windowbase,d1
	jsr	Write(a6)
	bra	closeDOS
copyinfo
	move.b	(a2)+,(a1)+
	bne	copyinfo	
	rts	

	include	trash/getargs.s


version		dc.b	'$VER: AddIcon V1.51 (96.10.22) BLABLA PODUCTION',0
filename	blk.b	2048,0
DosName		dc.b	'dos.library',0
		even
_DOSBase	dc.l	0
iconhandle	dc.l	0
dest_iconhandle
		dc.l	0
icon_length	dc.l	0
membase		dc.l	0
filelock	dc.l	0
iconlock	dc.l	0
iconpath	dc.b	'ENV:AddIcon/'
iconname	blk.b	200,0
fileicon	dc.b	'.info',0
dev_icon	dc.b	'Disk.info',0
windowbase	dc.l	0
startmsg
		dc.b	$0a,$1b,'[33mAddIcon 1.51',$1b,'[0m (c) 1996 by'
		dc.b	' ScoTT/Inferno & BlaBla'
		dc.b	$0A
		dc.b	'Usage: AddIcon iconname (without path'
		dc.b	" and '.info') filename  BLABLA PRODUCTION",$0A,$0A
endmsg
