/*************************************/
/*                                   */
/* AUoH Progressive Door Prize Lotto */
/*                                   */
/*  Michael D. Groshart - 16 Aug 89  */
/*                                   */
/*************************************/

#include <intuition/intuition.h>
#include <functions.h>
#include <stdio.h>

#include "lotto.h"

struct IntuitionBase	*IntuitionBase;
struct GfxBase		*GfxBase;
struct Screen		*scr;
struct ViewPort		*vp;
struct Window		*win;
struct RastPort		*rp;
struct TmpRas		tr;
PLANEPTR		plane;

unsigned long rand();
void srand();

#define GetIMsg(p) ((struct IntuiMessage *) GetMsg(p))

#define ESC 0x1b
#define SPC 0x20
#define MAX 1000

#define ARRAYSIZE 400

struct user_data
{
	short user;
	char *name;
} array[ARRAYSIZE];

int nent = 0;

init_screen()
{
	long sec,mic;

	IntuitionBase = (struct IntuitionBase *) OpenLibrary("intuition.library",0L);
	GfxBase = (struct GfxBase *) OpenLibrary("graphics.library",0L);

	if (!(scr = OpenScreen(&NewScreenStructure))) quit();

	NewWindowStructure1.Screen = scr;

	if (!(win = OpenWindow(&NewWindowStructure1))) quit();

	vp = &(scr->ViewPort);
	rp = win->RPort;

	if (!(plane = AllocRaster(320L,200L))) quit();

	InitTmpRas(&tr,plane,RASSIZE(320,200));
	rp->TmpRas = &tr;

	LoadRGB4(vp,&Palette,32L);
	SetMenuStrip(win,&Menu1);

	CurrentTime(&sec,&mic);
	srand(sec + mic);
}

quit()
{
	exit_beep();
	if (plane) FreeRaster(plane,320L,200L);
	if (win)
	{
		ClearMenuStrip(win);
		CloseWindow(win);
	}
	if (scr) CloseScreen(scr);

	CloseLibrary(GfxBase);
	CloseLibrary(IntuitionBase);

	exit(0);
}

pick()
{
	register struct IntuiMessage *im;
	register int i,num;

	erase();

	while (!(im = GetIMsg(win->UserPort)))
	{
		for (i = 0; i < 21; i++)
			flash();
		beep((int)rand(7168L),3);
	}
	ReplyMsg(im);

	num = rand((long) nent);

	printf("%4d %s\n",array[num].user,array[num].name);

	display(array[num].user);
	scroll(array[num].name);
}

void *malloc();

char *strdup(s)
char *s;
{
	register char *p;

	if (p = (char *) malloc(strlen(s) + 1))
	{
		strcpy(p, s);
	}
	return p;
}

add(num,nam)
char *num, *nam;
{
	register int i;

	if (nent < ARRAYSIZE)
	{
		if ((i = atoi(num)) < 400)
		{
			if (array[nent].name = strdup(nam))
			{
				array[nent++].user = i;
			}
		}
	}
}

main(argc,argv)
int argc;
char *argv[];
{
	struct IntuiMessage *im;
	USHORT code;
	ULONG class;
	char *datafile;

	if (argc == 1)
		datafile = "members.sbf";
	else
		datafile = argv[1];

	if (*datafile == '?')
	{
		fprintf(stderr,"Usage: %s [members.sbf]\n",argv[0]);
		exit(5);
	}

	if (read_db(datafile,add) == 0)
	{
		fprintf(stderr,"Error: can't open %s\n",datafile);
		exit(10);
	}

	init_screen();
	init_digits();
	init_beep();
	display(0);

	for (;;)
	{
		Wait(1L << win->UserPort->mp_SigBit);
		while (im = GetIMsg(win->UserPort))
		{
			class = im->Class;
			code = im->Code;
			ReplyMsg(im);

			if (class == MENUPICK)
			{
				if (ITEMNUM(code) == 0) quit();
			}
			else if (class == VANILLAKEY)
			{
				if (code == SPC) pick();
				if (code == ESC) quit();
			}
		}
	}
}

_wb_parse() {}
