/**********************************************************************/
/*                                                                    */
/*      AddTools - Adds menu items to the Workbench Tools menu.       */
/*                                                                    */
/*         Takes one argument, an input file, which contains          */
/*         a line of menu item and a line of command for each         */
/*                         menu item desired.                         */
/*                                                                    */
/*                          by Steve Tibbett                          */
/*                                                                    */
/**********************************************************************/

#include "exec/types.h"
#include "exec/ports.h"
#include "include:/compiler_headers_2.0/workbench/workbench.h"
#include "include:/compiler_headers_2.0/pragmas/wb.h"
#include "include:/compiler_headers_2.0/pragmas/dos.h"
#include "include:/compiler_headers_2.0/pragmas/intuition.h"
#include "intuition/intuition.h"
#include <libraries/dos.h>
#include <exec/memory.h>
#include <string.h>
#include "proto/exec.h"

struct Library *WorkbenchBase;
struct Library *IntuitionBase;

struct AppMenuItem *AppList[32];
char *AppListNames[32];
char *AppListCommands[32];
struct Remember *Remember=0;
struct MsgPort *port;
int NumApps=0;
struct DOSBase *DOSBase;

void MemCleanup(void) { }
void __fpinit(void) { }
void __fpterm(void) { }

#define BREAK (SetSignal(0,0) & SIGBREAKF_CTRL_C)

/**********************************************************************/
/*                     Lattice C Cleanup Routine                      */
/**********************************************************************/

extern void *_ONEXIT;

void
CleanupRoutine(void)
{
while (NumApps>=0)
	RemoveAppMenuItem(AppList[--NumApps]);

if (Remember) FreeRemember(&Remember, TRUE);
if (port) DeletePort(port);
if (WorkbenchBase) CloseLibrary(WorkbenchBase);
if (IntuitionBase) CloseLibrary(IntuitionBase);
if (DOSBase) CloseLibrary(DOSBase);
}

int
fgets(LONG FP, char *buff)
{
int c;

while (TRUE)
	{
	c=fgetc(FP);
	if (c=='\r'||c=='\n'||c==-1)
		{
		*buff=0;
		if (c==-1) return(FALSE);
			else return(TRUE);
		};
	*buff++=c;
	}
}

/**********************************************************************/
/*                        It all happens here                         */
/**********************************************************************/
int
_main(void)
{
LONG fp;
struct AppMessage *msg;

DOSBase=OpenLibrary("dos.library", 0L);

_ONEXIT=CleanupRoutine;

if ((WorkbenchBase = OpenLibrary("workbench.library",36))==0)
	return(0);

if ((IntuitionBase = OpenLibrary("intuition.library", 33))==0)
	return(0);

if ((port = CreatePort("AddTools by Steve Tibbett",0L))==0)
	return(0);

if ((fp=(LONG)Open("S:ToolsList", MODE_OLDFILE))==0)
	return(0);

while (TRUE)
	{
	char buff[255];
	char otherbuff[255];
	if (fgets(fp, buff)==0) break;
	if (fgets(fp, otherbuff)==0) break;
	AppListNames[NumApps]=(char *)AllocRemember(&Remember, strlen(buff)+1, MEMF_CLEAR);
	AppListCommands[NumApps]=(char *)AllocRemember(&Remember, strlen(otherbuff)+1, MEMF_CLEAR);
	if (AppListCommands[NumApps]==0 || AppListNames[NumApps]==0)
		return(0);
	strcpy(AppListCommands[NumApps], otherbuff);
	strcpy(AppListNames[NumApps], buff);
	AppList[NumApps] = (struct AppMenuItem *)AddAppMenuItemA(NumApps,NumApps,AppListNames[NumApps],port,NULL);
	NumApps++;
	}

fclose(fp);

while (TRUE)
	{
	ULONG ID;

	Wait(1<<port->mp_SigBit|SIGBREAKF_CTRL_C);
	msg = (struct AppMessage *) GetMsg(port);
	if (msg==0)			/* If Msg=NULL then SIGBREAKF_CTRL_C */
		break;

	ID=msg->am_ID;
	ReplyMsg((struct Message *) msg);

	if (ID==99)
		return(0);

	System(AppListCommands[ID], 0L);
	}
}
