#include <stdio.h>
#include <string.h>
#include "exec/types.h"
#include "intuition/intuition.h"
#include "intuition/preferences.h"

struct IntuitionBase	*IntuitionBase = NULL;
struct IntuiMessage	*Message       = NULL;
struct Window		*Window        = NULL;
struct Preferences	Prefs;

UBYTE Filename[128];

WORD FilenamePts[] =
{
	0,0,
	146,0,
	146,9,
	0,9,
	0,0,
};

struct Border		FilenameBdr =
{
	-1,-1,
	1,0,
	JAM1,
	5,
	FilenamePts,
	NULL,
};

struct StringInfo	FilenameStr =
{
	Filename,
	NULL,
	0,
	128,
	0,
	0,0,0,0,0,
	0,0,
	0,
};

struct Gadget		FilenameGad =
{
	NULL,
	12,14,
	144,11,
	NULL,
	STRINGCENTER|RELVERIFY,
	STRGADGET,
	(APTR)&FilenameBdr,
	NULL,
	NULL,
	NULL,
	(APTR)&FilenameStr,
	NULL,
	NULL,
};

struct NewWindow	WindowDefs =
{
	236,113,
	168,30,
	2,1,
	GADGETUP,
	WINDOWDRAG|ACTIVATE,
	&FilenameGad,
	NULL,
	"PrefSet v1.00",
	NULL,
	NULL,
	168,30,
	168,30,
	WBENCHSCREEN,
};

void CleanUp();
void GetName();

main(int argc,char *argv[])
{
	FILE	*PrefsFile;

	if (((argc == 2) && (*argv[1] == '?')) || (argc > 2))
	{
		printf("PrefSet v1.00  Written in 1991 by Chris Simpson\n");
		printf("Usage: %s [prefsfile]\n",argv[0]);
		CleanUp();
	}

	IntuitionBase=(struct IntuitionBase *)OpenLibrary("intuition.library",LIBRARY_VERSION);
	if (!IntuitionBase)
	{
		printf("Can't open intuition.library\n");
		CleanUp();
	}

	if (argc == 1)
	{
		GetName();
	}
	else
	{
		strcpy(Filename,argv[1]);
	}

	if (*Filename)
	{
		if(!(PrefsFile=fopen(Filename,"r")))
		{
			printf("Can't open \"%s\" for input\n",Filename);
			CleanUp();
		}

		fread((void *)&Prefs,sizeof(Prefs),1,PrefsFile);
		fclose(PrefsFile);

		SetPrefs(&Prefs,sizeof(Prefs),1);
	}

	CleanUp();
}

void GetName()
{
	Window=(struct Window *)OpenWindow(&WindowDefs);
	if (!Window)
	{
		printf("Can't open requester window\n");
		CleanUp();
	}

	ActivateGadget(&FilenameGad,Window,NULL);

	while(!(Message=(struct IntuiMessage *)GetMsg(Window->UserPort)))
	{
	}
	ReplyMsg((struct Message *)Message);

	CloseWindow(Window);
}

void CleanUp()
{
	if (IntuitionBase) CloseLibrary(IntuitionBase);
	exit(0);
}

