/*******************************\
* INIT_BULLETIN.C		*
* (C)1986-87, Patrick E. Hughes	*
\*******************************/

#include <exec/types.h>
#include <exec/memory.h>
#include <stdio.h>
#include "Defines.h"
#include "System.h"

extern int Bulletins_Are_Active;
extern struct System System;
extern void *AllocRemember();
extern struct Remember *RememberKey;

int Init_Bulletins();

static int Header_Segment();

/***********************/
int Init_Bulletins()
{
int status;

status=Header_Segment();
if(status==FAILURE)
	{
	Bulletins_Are_Active=0;
	return(SUCCESS);
	}

return(SUCCESS);
}

/***********************/
static int Header_Segment()
{
FILE *fp;
int stat;
struct Bulletin_Header *bh;
char trashcan[132];

Bulletins_Are_Active=0;

fp=fopen("s:Tag_Bulletins","r");
if(fp==NULL) { return(FAILURE); }

System.Bulletins_List=AllocRemember(
	&RememberKey,
	(long)sizeof(struct Bulletin_Header),
	MEMF_CLEAR);
if(System.Bulletins_List==NULL)
	{
	ShutDown("No Memory for Bulletin configuration");
	}

bh=System.Bulletins_List;
FOREVER
	{
	(void)fscanf(fp,"%[^B]",trashcan);
	stat=fscanf(fp, "BULLETIN:%30[^\n]", bh->Filename);
	if(stat!=1)
		{
		if(Bulletins_Are_Active>0)
			{
			bh->Last_Bulletin->Next_Bulletin=NULL;
			return(SUCCESS);
			}
		System.Bulletins_List=NULL;
		fclose(fp);
		return(FAILURE);
		}
	(void)fscanf(fp,"%[^L]",trashcan);

	stat=fscanf(fp, "LOCAT:%80[^\n]", bh->Location);
	if(stat!=1)
		{
		if(Bulletins_Are_Active>0)
			{
			bh->Last_Bulletin->Next_Bulletin=NULL;
			break;
			}
		System.Bulletins_List=NULL;
		fclose(fp);
		return(FAILURE);
		}
	(void)fscanf(fp,"%[^R]",trashcan);

	stat=fscanf(fp, "READ:%hu",&bh->Read_Low);
	if(stat!=1)
		{
		if(Bulletins_Are_Active>0)
			{
			bh->Last_Bulletin->Next_Bulletin=NULL;
			break;
			}
		System.Bulletins_List=NULL;
		fclose(fp);
		return(FAILURE);
		}
	stat=fscanf(fp, ",%hu",&bh->Read_High);
	if(stat!=1)
		{
		if(Bulletins_Are_Active>0)
			{
			bh->Last_Bulletin->Next_Bulletin=NULL;
			break;
			}
		System.Bulletins_List=NULL;
		fclose(fp);
		return(FAILURE);
		}
	++Bulletins_Are_Active;
	bh->Next_Bulletin=AllocRemember(
				&RememberKey,
				(long)sizeof(struct Bulletin_Header)
				,MEMF_CLEAR);
	if(bh->Next_Bulletin==NULL)
		{
		ShutDown("No Memory for Bulletin configuration");
		}
	bh->Next_Bulletin->Last_Bulletin=bh;
	bh=bh->Next_Bulletin;
}

fclose(fp);
return(SUCCESS);
}
