/* sound effect gimmick player - config routine */

#include <exec/types.h>
#include <functions.h>
#include <exec/memory.h>
#include <exec/nodes.h>
#include <exec/lists.h>
#include <stdio.h>
#include <fcntl.h>

#include "assert.h"

#include "iff.h"
#include "8svx.h"

#include "sample.h"
#include "chatter.h"

#define MATCH 0

Sample *Load8SVX();

int gab_interval = 300;

int total_loaded = 0;

struct gab_info gab_data[N_GAB_TYPES];

int minimum_timer_interval, maximum_timer_interval;

int timer_noload = NO;
char timer_loadopt[20];

InitGabData()
{
	register int i, j;

	for (i = 0; i < N_GAB_TYPES; i++)
	{
		gab_data[i].n_gab_items = 0;
		gab_data[i].next_new_gab_item = 0;
		gab_data[i].next_play_gab_item = 0;

		for (j = 0; j < MAX_GAB_ITEMS; j++)
			gab_data[i].Samples[j] = NULL;
	}
}

Config()
{
	FILE *fp;
	char s[80];
	int gab_type = -1;
	struct gab_info *gabp;
	int nconverted;

 	if ((fp = fopen("ChatterBox:config.data","r")) == NULL)
 		panic("ChatterBox:config.data config file doesn't exist");

	while (fgets(s,80,fp) != NULL)
	{
		s[strlen(s)-1] = '\0';	/* cream the newline */

		if (s[0] != ' ')
		{
			if (strncmp(s,"diskin",6) == MATCH)
				gab_type = GAB_DISKIN;
			else if (strncmp(s,"diskout",7) == MATCH)
				gab_type = GAB_DISKOUT;
			else if (strncmp(s,"timer",5) == MATCH)
			{
				gab_type = GAB_EVERY_SO_OFTEN;

 				nconverted = sscanf(&s[5],"%s %d %d",timer_loadopt,&minimum_timer_interval,&maximum_timer_interval);

 				timer_noload = (strcmp(timer_loadopt,"noload") == MATCH);

 
				if (nconverted != 3)
					sscanf(&s[6],"%d %d",&minimum_timer_interval,&maximum_timer_interval);

				if ((minimum_timer_interval > maximum_timer_interval) 
				  || (minimum_timer_interval <= 0) || (maximum_timer_interval <= 0))
				{
					if ((minimum_timer_interval != 0) || (maximum_timer_interval != 0))
						printf("config file 'timer' line is bad, using 5 minute default value\n");
					minimum_timer_interval = maximum_timer_interval = 300;
				}
			}
			else if (strncmp(s,"preferences",11) == MATCH)
				gab_type = GAB_NEW_PREFERENCES;
			else if (strncmp(s,"activate",8) == MATCH)
				gab_type = GAB_WINDOWACTIVE;
			else if (strncmp(s,"deactivate",10) == MATCH)
				gab_type = GAB_WINDOWINACTIVE;
			else if (strncmp(s,"beep",4) == MATCH)
				gab_type = GAB_BEEP;
			else
			{
				printf("'%s' should be 'diskin', 'diskout', 'timer', 'preferences', 'activate',\n",s);
				printf("'deactivate', or 'beep'\n");
				gab_type = -1;
			}
		}
		else
		{
			if (gab_type == -1)
			{
				printf("ignored '%s'",s);
			}
			else
			{
				gabp = &gab_data[gab_type];
				if ((gab_type == GAB_EVERY_SO_OFTEN) && (timer_noload == YES))
				{
					AddDeferredSound(s);
				}
				else if (gabp->next_new_gab_item < MAX_GAB_ITEMS)
				{
					gabp->Samples[gabp->next_new_gab_item] = Load8SVX(&s[1]);

					if (gabp->Samples[gabp->next_new_gab_item])
					{
						gabp->n_gab_items++;
						gabp->next_new_gab_item++;
						total_loaded++;
					}
				}
				else
				{
					printf("too many entries for one type, '%s' ignored\n",&s[1]);
				}
			}
		}
	}
	fclose(fp);
	if (total_loaded == 0)
		panic("no sample files loaded");
}

/* end of config.c */
