/*
 *	LoadLib.c - Copyright © 1990 by S.R.
 *
 *	Created:	04 Aug 1991	18:27:25
 *	Modified:	28 Jan 1992	18:44:29
 * Compiles: 26
 *
 *	Make>> lc -b1 -cf -r1 -M -v -w -ci -O -HINCLUDE:x.sym <file>
 *	Make>> BLink Lib:CStartupRes.o <file>.o TO <file> LIB Lib:String.lib SMALLDATA SMALLCODE VERBOSE
 */

#include "string_lib.h"

extern struct ExecBase *SysBase;
extern struct DosLibrary *DOSBase;

struct IconBase *IconBase = NULL;

struct WBStartup *_wb_parse(struct Process * ThisProcess)
{
	struct WBStartup *WBenchMsg = NULL;

	WaitPort(&ThisProcess->pr_MsgPort);
	WBenchMsg = (struct WBStartup *) GetMsg(&ThisProcess->pr_MsgPort);
	CurrentDir(WBenchMsg->sm_ArgList->wa_Lock);

	return WBenchMsg;
}

void Msg( char * Str)
{
	BPTR fh;

	if ( fh = Open("CON:0/10/640/100/LoadLib Message:", MODE_NEWFILE)) 
		{
			Write( fh, Str, strlen(Str));
			Delay(50L);
			Close( fh);
		}

	return;
}

void LoadLibrary( char *LibraryName)
{
	struct Library *LibraryPtr;

	LibraryPtr = OpenLibrary( LibraryName, 0L);
	if ( LibraryPtr)
		CloseLibrary( LibraryPtr);
}

/* return ptr on next arg. Attention: this function modified the arg line */
char *NextCLIArg( char *lineptr)
{
	char *NextArg;

	while( isspace( *lineptr) )
		lineptr++;										/* skip space, tab, etc... */

	if ( !(*lineptr) )
		return NULL;									/* end of argument */

	if ( *lineptr == '"' )
		{
			NextArg = ++lineptr;								/* arg start after quote */
			while ( *lineptr != '"')
				lineptr++;								/* go to terminating quote */
		}
	else
		{
			NextArg = lineptr++;								/* arg start here */
			while( !isspace( *lineptr) )
				lineptr++;
		}

	*lineptr = '\0';							/* terminate the string */
	return NextArg;
}		

long _main(  long alen, register char *aptr)
{
	struct Process *ThisProcess;
	struct WBStartup *WBenchMsg = NULL;
	char ArgLine[257];
	char *CLIArg;

	struct DiskObject *dop;

	char **ToolTypesArray = NULL;
	long i;

	ThisProcess = (struct Process *) SysBase->ThisTask;
	if (!ThisProcess->pr_CLI)
		{
			/* run from WB */
			WBenchMsg = _wb_parse(ThisProcess);
		}

	if ( WBenchMsg)
		{
			if ( !( IconBase = (struct IconBase *) OpenLibrary("icon.library", 1L)))
				{
					Msg( "You need icon.library V1+ \n");
					return 20;
				}
			/* Tool Types parsing */
			for (i = 1; i < WBenchMsg->sm_NumArgs; i++)
				{
					CurrentDir(WBenchMsg->sm_ArgList[i].wa_Lock);	/* enter in the dir containing the Icon */
					if (dop = GetDiskObject(WBenchMsg->sm_ArgList[i].wa_Name))
						{
							for( ToolTypesArray = dop->do_ToolTypes, i = 0; ToolTypesArray[i]; i++)
								{
									LoadLibrary( ToolTypesArray[i]);
								}
							FreeDiskObject(dop);

						}
					else
						{
							/* no icon: I can't do anything ! */
							Msg( "I can't find the icon");
							return 20;
						}
				}
		}
	else
		{		/* CLI parsing */
			if ( alen < strlen("LoadLib"))
				{
					Write( Output(),"[32mLoadLib[31m V1.0 © 1991 by [33mS.R.[31m\nUsage: LoadLib <dev:dir/Library_name>\n", 86);
					return 5;
				}
			strncpy ( ArgLine, aptr, alen);
			ArgLine[alen] = '\0';
			ArgLine[alen + 1 ] = '\0';
			CLIArg = ArgLine;

			while ( CLIArg = NextCLIArg( CLIArg) )
				{
					LoadLibrary( CLIArg);
					CLIArg = &CLIArg[ strlen( CLIArg) + 1];
				}
		}

	return 0;
}
