/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/* ICONEXEC (c) Copyright 1986 John A. Toebes, VIII. All rights reserved */
/*  This program may be used and modified for any purpose so long as     */
/*  this copyright notice remains with the code and the program is not   */
/*  sold and no charge is made for its use.  For permission to           */
/*  incorporate this into a commercial product, contact the author:      */
/*    John A. Toebes, VIII                                               */
/*    120 H Northington Place                                            */
/*    Cary NC 27511                                                      */
/*    (919) 469-4210                                                     */
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

/*-----------------------------------------------------------------------*/
/* This program allows you to associate any series of CLI commands to be */
/* run when you click on an ICON even though there is no CLI window open */
/* One problem associated with it is that a program that does input from */
/* the default input (*) always sees EOF.  I am researching why this     */
/* happens, but believe it is related to an AmigaDos problem.            */
/*-----------------------------------------------------------------------*/

/****************
  To compile this program under Lattice C:
    1) Compile the 
	LC1 -oRAM: -i<includedir> ICONEXEC
	LC2 -v -oICONEXEC.O RAM:ICONEXEC

    2) Create a file 'link_iconexec' with the linker commands (assuming 
       that df0:lib contains the appropriate linking modules):
	FROM     df0:lib/c.o+*
	IconExec.o
	TO       IconExec
	LIBRARY  df0:lib/lc.lib+df0:lib/amiga.lib
	MAP      nil:

    3) link it using alink
	ALINK WITH LINK_ICONEXEC

   To create and execute a user customized Icon Exec

    1) Create an Icon (copy the CLI Icon) for IconExec

    2) Run the SetWindow program to set a the tool window on the Icon
	SETWINDOW ICONEXEC "CON:0/0/100/100/MYWINDOW"
       If you do not do this, then by default ICONEXEC will open a full
       screen window with a title of "ICONEXEC by John A. Toebes, VIII"

    3) Using the Workbench INFO meno option, enter the commands into the
       tool types window.

    4) Click on the Icon and watch it go.
**********/

#include <stdio.h>
#include <exec/types.h>
#include <workbench/workbench.h>
#include <workbench/icon.h>
#include <workbench/startup.h>
#include <libraries/dos.h>
#include <libraries/dosextens.h>

#define DEFAULT_WINDOW "CON:0/1/640/199/ICONEXEC by John A. Toebes, VIII"

LONG IconBase;
extern struct WBStartup *WBenchMsg;

_main(line)
char *line;
{
	char **tools;
	char *thistool;
	struct WBArg *wbarg;
	struct DiskObject *diskobj, *GetDiskObject();
	int olddir;
	char *window;
	struct FileHandle *handle;
	BPTR file;
	struct Process *process;

	if ( (IconBase = OpenLibrary( ICONNAME, 1) ) == NULL)
		{
		diag("Cannot open Icon Library", NULL);
		_exit(2);
		}

	/* Locate the first argument passed from workbench */
	wbarg	= WBenchMsg->sm_ArgList;

	/* change to its home dir */
	olddir	= CurrentDir( wbarg->wa_Lock );

	/* read in its disk object structure */
	diskobj	= GetDiskObject( wbarg->wa_Name );

	/* did we have a default tool window opened for us ? */
	if (WBenchMsg->sm_ToolWindow == NULL)
		{
		/* figure out the window to open */
		if ( (window = diskobj->do_ToolWindow) == NULL)
			window = DEFAULT_WINDOW;

		/* open the desired window if we can */
		if ( (file = Open(window,MODE_OLDFILE)) == NULL)
			{
			diag("Cannot open Icon Window", window);
			CurrentDir( olddir );
			CloseLibrary( IconBase );
			_exit(4);
			}

		/* fool system into thinking we have a CLI based window */
		handle = (struct FileHandle *)(file << 2);
		process = (struct Process *)FindTask(0);
		process->pr_ConsoleTask = (APTR)handle->fh_Type;
		}
	else
		file = Output();

	/* run through the tools and execute them one at a time */
	for (tools = diskobj->do_ToolTypes; thistool=*tools; tools++)
		Execute(thistool, NULL, NULL);

	/* close down the window */
	if (WBenchMsg->sm_ToolWindow == NULL)
		Close(file);

	/* restore the previous directory */
	CurrentDir( olddir );
	CloseLibrary( IconBase );
	_exit(0);
}

diag(msg, parm)
char *msg;
char *parm;
{
	BPTR logfile;
	char c;

	logfile = Open("con:0/0/50/200/ICONEXEC Error", MODE_OLDFILE);
	Write(logfile, "ICONEXEC Error:\n", 16);
	Write(logfile, msg, strlen(msg));
	if (parm != NULL)
		Write(logfile, parm, strlen(parm));
	Write(logfile, "Press <RETURN> to continue\n", 27);
	Read(logfile, &c, 1);
	Close(logfile);
}
