/*-------------------------------------------------------------------------
	SunClock

	Amiga version by Mark Waggoner, 
	waggoner@ichips.intel.com or wagnell@PDaXcess.techbook.com
	December 1991

	Options.c
	Parse options for workbench or CLI use
-------------------------------------------------------------------------*/
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include <functions.h>
#include <workbench/startup.h>
#include <workbench/workbench.h>

extern int AdjustIcon,FullScreen;
extern struct NewWindow NewIconWindow;
struct Library *IconBase;

/*-------------------------------------------------------------------
	Skip past the next space delimited token in a string
-------------------------------------------------------------------*/
char *
SkipToken(char *s) {
	while(*s &&  isspace(*s)) s++;
	while(*s && !isspace(*s)) s++;
	return(s);
}

/*-------------------------------------------------------------------
	Check an option name for CLI parsing
-------------------------------------------------------------------*/
char *
CheckOption(char *s,char *opt) {
	int l,m;
	char *t;

	l = strlen(opt);

	/* Find the length of the next word */
	m = 0; t = s;
	while(*t && !isspace(*t) && *t != '=') { t++; m++; }

	if (m!=l) 
		s = NULL;
	else
	if (strncmp(s,opt,l))
		s = NULL;
	else {
		s+=l;
		/* Skip past an equal sign */
		while(*s && isspace(*s)) s++;
		if (*s == '=') s++;
	}
	return(s);
}

/*-------------------------------------------------------------------
	I parse the cli parameters before they get to main()
-------------------------------------------------------------------*/
void
_cli_parse(struct Process *pp, long alen, register char *aptr)
{
	char *s,*t;

	s = aptr;

	while(*s) {
		while(*s && isspace(*s)) s++;
		if (t = CheckOption(s,"TOP")) {
			NewIconWindow.TopEdge = atoi(t);
			s = SkipToken(t);
		}
		else
		if (t = CheckOption(s,"LEFT")) {
			NewIconWindow.LeftEdge = atoi(t);
			s = SkipToken(t);
		}
		else
		if (t = CheckOption(s,"DONOTADJUST")) {
			AdjustIcon = 0;
			s = t;
		}
		else
		if (t = CheckOption(s,"FULLSCREEN")) {
			FullScreen = 1;
			s = t;
		}
		else
			s = SkipToken(s);
	}
}


/*-------------------------------------------------------------------
	Parse workbench options
-------------------------------------------------------------------*/
void
_wb_parse(struct Process *pp,struct WBStartup *wbm)
{
	char *tool;
	struct DiskObject *dob;

	CurrentDir(wbm->sm_ArgList->wa_Lock);

	if ((IconBase = OpenLibrary("icon.library", 0L)) == 0)
		return;
	if ((dob = GetDiskObject(wbm->sm_ArgList->wa_Name)) == 0)
		goto NoDiskObj;

	if (tool = FindToolType(dob->do_ToolTypes, "TOP"))
		NewIconWindow.TopEdge = atoi(tool);
	if (tool = FindToolType(dob->do_ToolTypes, "LEFT"))
		NewIconWindow.LeftEdge = atoi(tool);
	if (tool = FindToolType(dob->do_ToolTypes, "DONOTADJUST"))
		AdjustIcon = 0;
	if (tool = FindToolType(dob->do_ToolTypes, "FULLSCREEN"))
		FullScreen = 1;

	FreeDiskObject(dob);
NoDiskObj:
	CloseLibrary(IconBase);
	IconBase = NULL;
}
