/* XTools M. Laliberté/ J. Toebes, TransAmiga, dec 1988, p.69 */

#include <exec/types.h>
#include <intuition/intuition.h>
#include <workbench/workbench.h>
#include <workbench/startup.h>
#include <workbench/icon.h>
#include <libraries/dosextens.h>
#include <dos.h>
#define U_ARGS(a) a
#include <proto/exec.h>
#include <proto/intuition.h>
#include <proto/dos.h>
#include <string.h>

#define MAXSTRINGS 26
extern struct WBStartup *WBenchMsg;
struct IntuitionBase *IntuitionBase;
struct Library *IconBase;

struct DiskObject *diskobj;
char **findtools U_ARGS((char *,BPTR));

void main (argc, argv)
int argc;
char **argv;
{
char *(strings[MAXSTRINGS]);
int count, i;
struct WBArg *wbarg;
char **toolptr,*name,lookup[8];
if ((IntuitionBase = (struct IntuitionBase *) 
	OpenLibrary("intuition.library",0)) == NULL ) {
		goto done;
		}
if ((IconBase = (struct IconBase *) 
	OpenLibrary("icon.library",0)) == NULL ) {
		goto done;
		}

if (argc)
	toolptr = findtools(argv[0],
		((struct Process *)FindTask(NULL))->pr_CurrentDir);
	else
	{
		wbarg = &(WBenchMsg->sm_ArgList[WBenchMsg->sm_NumArgs-1]);
		toolptr = findtools(wbarg->wa_Name,wbarg->wa_Lock);	
	}

strcpy(lookup,"A");
count = 0;
for (i=0;i < MAXSTRINGS;i++)
{
	if ((name=FindToolType(toolptr,lookup)) != NULL) {
		strings[count] = name;
		Execute(strings[count++],0,0);
		}
	lookup[0]++;
}

done:
if (diskobj) {
	FreeDiskObject(diskobj);
	diskobj = NULL;
	}
if (IntuitionBase) {
	CloseLibrary ((struct Library *)IntuitionBase);
	IntuitionBase = NULL;
	}
if (IconBase) {
	CloseLibrary(IconBase);
	IconBase = NULL;
	}

}


char **findtools(name,lock)
char *name;
BPTR lock;
{
BPTR olddir;
char **tools = NULL;

if(lock == NULL) return (NULL);
olddir = CurrentDir(lock);
if((diskobj=GetDiskObject(name)) != NULL)
	tools=diskobj->do_ToolTypes;
CurrentDir(olddir);
return(tools);
}

void MemCleanup() {}

