	Section	IDer,Code
	OPT	!
	MACHINE	68000

	NOLIST
	IncDir	Include:
	Include	mysystem.i

	Include	libraries/asl.i
	Include	libraries/xpk.i

	Include	lib/asl_lib.i
	Include	lib/icon_lib.i
	Include	lib/wb_lib.i
	Include	lib/xpkmaster_lib.i

	Include	IDer.i
	Include	Applications:Development/lib/wbpath.i

OPENLIBRARY	MACRO	lib,ver
	Lea	(\1Library,PC),A1	;intuition library is needed if we are
	Moveq	#\2,D0		;running from workbench.  It's easier to
	CALL	OpenLibrary,EXEC	;open it now when we can trash registers
	Move.L	D0,_\1Base		;than trying to work around keeping some
	Beq	CloseDown		;values safer later on
	ENDM

CLOSELIBRARY	MACRO	Lib
	Move.L	(_\1Base,PC),A1		;better close this to keep the masses happy
	Cmp.L	#0,A1
	Beq.S	.No\1
	CALL	CloseLibrary,EXEC
.No\1
	ENDM

	LIST

;=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
Start:	Move.L	$4.w,A6
	Move.L	A6,_EXECBase		;for use with FASTMEM

;=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
	Sub.L	A1,A1
	CALL	FindTask
	Move.L	D0,A4
	Tst.L	pr_CLI(A4)
	Bne	.FromCLI

	Include	Routines/WBHandler.s

;=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
.FromCLI	Lea	DOSLibrary(PC),A1
	Moveq	#37,D0		;we need Kickstart 2.04 or better
	CALL	OpenLibrary
	Move.L	D0,_DOSBase
	Beq	BadDOS

	OPENLIBRARY	INTUI,0

	Move.W	#RDARGERR,_ErrorCode
	Move.L	#ArgTemplate,D1		;template of arguments available
	Move.L	#_FileName,D2		;start of table to store argument values
	Moveq	#0,D3		;no custom RDA structure this time
	CALL	ReadArgs,DOS		;process the commandline
	Move.L	D0,_ArgRDA
	Beq	CloseDown		;bug out if the commandline failed

;=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
WBReEnter:	Include	Routines/GetConfig.s
	Include	Routines/GetFileName.s
;=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
	Cmp.W	#ARGS_NOARGS,_ArgMode
	Beq.S	.AppIcon

	Bsr	DoFiles
	Bra	CloseDown

.AppIcon	Tst.B	_UseAppIcon
	Beq	CloseDown

	Include	Routines/AppIcon.s

;=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
CloseDown:	Bsr	ErrorHandler

	Move.L	_ConfigHandle(PC),D1	;was there an error during the config loading
	Tst.L	D1
	Beq.S	.NoConfig
	CALL	Close,DOS
.NoConfig
	Move.L	_FileHandle(PC),D1	;how about during loading our file to view
	Tst.L	D1
	Beq.S	.NoFile
	CALL	Close,DOS
.NoFile
	Move.L	_Requestor(PC),A0	;free up requestor structure
	Cmp.L	#0,A0
	Beq.S	.NoASLReq
	CALL	FreeAslRequest,ASL
.NoASLReq
	Move.L	_OutputBuffer(PC),A1	;clean up the memory allocated to hold the config file
	Move.L	_OutputBufLen(PC),D0
	Cmp.L	#0,A1
	Beq.S	.NoFileMemory
	CALL	FreeMem,EXEC
.NoFileMemory
	Move.L	_ConfigMem(PC),A1	;clean up the memory allocated to hold the config file
	Move.L	_ConfigSize(PC),D0
	Cmp.L	#0,A1
	Beq.S	.NoConfigMemory
	CALL	FreeMem,EXEC
.NoConfigMemory
	Move.L	_ArgRDA(PC),D1		;free up the ReadArgs structure
	Tst.L	D1
	Beq.S	.NoRDA
	CALL	FreeArgs,DOS
.NoRDA
	Move.L	_IconObject(PC),A0	;was there an error while working with the icons/tooltypes
	Cmp.L	#0,A0
	Beq.S	.NoDiskObject
	CALL	FreeDiskObject,ICON
.NoDiskObject
	Move.L	_AppIconActual(PC),A0	;was there an error while working with the icons/tooltypes
	Cmp.L	#0,A0
	Beq.S	.NoAppIcon
	CALL	RemoveAppIcon,WB
.NoAppIcon
	Move.L	_AppMsg(PC),A1
	Cmp.L	#0,A1
	Beq.S	.NoAppMsg
	CALL	ReplyMsg,EXEC
.NoAppMsg
	Move.L	_AppMsgPort(PC),A0	;was there an error while working with the appicon
	Cmp.L	#0,A0
	Beq.S	.NoMsgPort
	CALL	DeleteMsgPort,EXEC
.NoMsgPort
	Move.L	_OldDirectory(PC),D1
	Tst.L	D1
	Beq.S	.NoDirectory
	CALL	CurrentDir,DOS
.NoDirectory
	CLOSELIBRARY	XPK
	CLOSELIBRARY	ASL
	CLOSELIBRARY	WB
	CLOSELIBRARY	ICON
	CLOSELIBRARY	INTUI
	CLOSELIBRARY	DOS

	Tst.L	_WBMessage		;were we launched from Workbench?
	Beq.S	.NoWBMsg
	CALL	Forbid,EXEC
	Move.L	_WBMessage(PC),A1
	CALL	ReplyMsg
.NoWBMsg
	Moveq	#0,D0		;no error codes

	Tst.W	_ErrorCode
	Beq.S	.NoErrors
	Moveq	#RETURN_ERROR,D0
.NoErrors	Rts

;=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
	Include	Routines/DoFiles.s
	Include	Routines/ParseConfig.s
	Include	Routines/ProcessFlags.s
	Include	Routines/Routines.s
	Include	Routines/ErrorHandler.s
	Include	Routines/Cleanup.s
	Include	Routines/BadDOS.s

;=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
IDerPrefs:	Dc.B	'ENV:IDer.prefs',0
DOSLibrary:	DOSNAME
ASLLibrary:	ASLNAME
XPKLibrary:	XPKMASTERNAME
ICONLibrary:	ICONNAME
INTUILibrary:	INTUITIONNAME
WBLibrary:	WORKBENCHNAME
Version:	Dc.B	'$VER: IDer 1.05 (15.1.95)',0
SearchString:	Dc.B	'  =',0
ArgTemplate:	Dc.B	'FILENAME/M,'
CrunchedTT:	Dc.B	'NOCRUNCHED/S,'
ConfigTT:	Dc.B	'CONFIG/K',0
AppIconTT:	Dc.B	'APPICON',0,0
AppNameTT:	Dc.B	'APPNAME',0
Ok:	Dc.B	'OK',0
IDerTxt:	Dc.B	'IDer',0
Info:	Dc.B	'.info',0
FReqTitle:	Dc.B	'Select file to load...',0
ErrorHeader:	Dc.B	'There was an error %s%s%s\n',0
ErrProcessing:	Dc.B	'processing the command line',0
ErrOpening:	Dc.B	'opening \'',0
ErrReading:	Dc.B	'reading \'',0
ErrMemory:	Dc.B	'allocating memory to load',0
ErrASLLib:	Dc.B	'asl.library\' v38+',0
ErrASLReq:	Dc.B	'allocating a file requestor',0
ErrEndOfCfg:	Dc.B	'finding a suitable CLASS entry',0
ErrXPK:	Dc.B	'decrunching using XPK.\n',0
ErrCreating:	Dc.B	'creating the ',0
ErrCustom:	Dc.B	'\ncustom '
ErrAppIcon:	Dc.B	'AppIcon',0
ErrDefault:	Dc.B	'.  Using \'IDer.info\' image',0
ErrCloseQuote:	Dc.B	'\'',0
ErrDOS:	Dc.B	'You need at least Kickstart 2.04 to use IDer.\n',0
AboutText:	Dc.B	'Copyright © 1995  Eclipse Software\n\n'
	Dc.B	'This program is classified as\n'
	Dc.B	'Shareware, and a donation of $10\n'
	Dc.B	'is suggested if used reguarly.',0
AboutGads:	Dc.B	'Select|Quit|Cancel',0
	Even

_EXECBase:	Dc.L	0
_DOSBase:	Dc.L	0
_ASLBase:	Dc.L	0
_XPKBase:	Dc.L	0
_ICONBase:	Dc.L	0
_INTUIBase:	Dc.L	0
_WBBase:	Dc.L	0

_WBMessage:	Dc.L	0
_WBPaths:	Dc.L	0
_IconObject:	Dc.L	0

_ToolTypes:	Dc.L	0
_OldDirectory:	Dc.L	0

_AppIcon:	Dc.L	0
_AppName:	Dc.L	IDerTxt
_AppIconX:	Dc.L	0
_AppIconY:	Dc.L	0
_AppIconActual:	Dc.L	0
_AppMsgPort:	Dc.L	0
_Signal:	Dc.L	0
_AppMsg:	Dc.L	0

_UseAppIcon:	Dc.B	0
	Dc.B	0	;pad

_ArgRDA:	Dc.L	0
_FileName:	Dc.L	0
_Crunched:	Dc.L	0
_Config:	Dc.L	0

_FileHandle:	Dc.L	0
_FileData:	Dc.L	FileData
_FilePart:	Dc.L	0

_CurrentName:	Dc.L	0
_ArgMode:	Dc.W	ARGS_NOARGS
_LoopCount:	Dc.W	0

_XPKTags:	Dc.L	XPK_InFH,0
	Dc.L	XPK_GetOutBuf,_OutputBuffer
	Dc.L	XPK_GetOutLen,_OutputLength
	Dc.L	XPK_GetOutBufLen,_OutputBufLen
	Dc.L	XPK_PassThru,-1
	Dc.L	XPK_GetError,XPKErrorBuffer
	Dc.L	TAG_DONE

_OutputBuffer:	Dc.L	0
_OutputLength:	Dc.L	0
_OutputBufLen:	Dc.L	0

_ErrorCode:	Dc.W	0

ErrTable:	Dc.L	ErrProcessing,0,0		;commandline error
_CfgErr:	Dc.L	ErrOpening,IDerPrefs+4,ErrCloseQuote	;opening config
	Dc.L	ErrMemory,IDerPrefs+4,ErrCloseQuote	;config memory
	Dc.L	ErrReading,IDerPrefs+4,ErrCloseQuote	;reading config
_ErrFN1:	Dc.L	ErrOpening,0,ErrCloseQuote		;opening datafile
_ErrFN2:	Dc.L	ErrReading,0,ErrCloseQuote		;reading datafile
	Dc.L	ErrOpening,ErrASLLib,0		;no asl library
	Dc.L	ErrASLReq,0,0			;no asl requestor
	Dc.L	ErrEndOfCfg,0,0			;no entry in config
	Dc.L	ErrXPK,XPKErrorBuffer,0		;problem with XPK routines
	Dc.L	ErrCreating,ErrAppIcon,ErrDefault	;couldn't create AppIcon
	Dc.L	ErrCreating,ErrCustom,ErrDefault	;couldn't create custom AppIcon

_ConfigMem:	Dc.L	0
_ConfigSize:	Dc.L	0
_ConfigHandle:	Dc.L	0
_ConfigCurPos:	Dc.L	0

_OffsetVal:	Dc.L	0

_Requestor:	Dc.L	0
_ASLTags:	Dc.L	ASLFR_RejectIcons,TRUE
	Dc.L	ASLFR_DoMultiSelect,TRUE
	Dc.L	ASLFR_TitleText,FReqTitle
	Dc.L	TAG_DONE

_esReq:	Dc.L	EasyStruct_SIZEOF
	Dc.L	0
	Dc.L	Version+6
	Dc.L	0
	Dc.L	0

_Mode:	Dc.B	0
_Insensitive:	Dc.B	0
_Quote:	Dc.B	0
_Successful:	Dc.B	0

EntryTable:	Dc.L	_Class,_Suffix,_Name,_Offset,_Flags
	Dc.L	-1,_Stack,_Pri,-1

_Class	Dc.L	0
	Dc.B	'CLASS=',0
	Even
_Suffix	Dc.L	0
	Dc.B	'SUFFIX=',0
	Even
_Name	Dc.L	0
	Dc.B	'NAME=',0
	Even
_Offset	Dc.L	0
	Dc.B	'OFFSET=',0
	Even
_Flags	Dc.L	0
	Dc.B	'FLAGS=',0
	Even
_Stack	Dc.L	0
	Dc.B	'STACK=',0
	Even
_Pri	Dc.L	0
	Dc.B	'PRI=',0
	Even
_Action	Dc.L	0
	Dc.B	'ACTION=',0
	Even

;=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
	Section	IDerData,BSS
CommandLine:	Ds.B	1024

FileName:	Ds.B	1024
FileData:	Ds.B	IDData

ConfigLine:	Ds.B	1024
ParseData:	Ds.B	256
WorkData:	Ds.B	128
XPKErrorBuffer:	Ds.B	XPKERRMSGSIZE

AppIconName:	Ds.B	32
ConfigName:	Ds.B	128

_SystemTags:	Ds.L	(MaxTags*2)+2
