/* PR-QWK - QWK Importer for UMS
 * Copyright (C) 1998 J.Ross Nicoll
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

#include <exec/types.h>

#include <dos/dos.h>

#include <proto/alib.h>
#include <proto/dos.h>
#include <proto/exec.h>

#include <stdlib.h>
#include <string.h>

#define CATCOMP_NUMBERS

#include "locale.h"
#include "pr-qwk.h"

#define Prototype extern
#include "PR-QWK-protos.h"

Prototype BOOL SplitControl(STRPTR);
Prototype STRPTR CreateConfList(STRPTR);
Prototype STRPTR ReadControl(VOID);

BOOL SplitControl(STRPTR ControlBuffer)
{
	STRPTR start;
	STRPTR end;
	STRPTR *work;
	UBYTE count;
	UWORD *wordWork;

	start = ControlBuffer;
	work = &ControlData.BBSName;
	for(count = 0; count < 9; count++)
	{
		*work = start;
		end = strstr(start, "\r\n");
		if(end == NULL) break;
		*end = 0;

		start = end + 2;
		work++;
	}

	if(end != NULL)
	{
		wordWork = (UWORD *)work;
		for(count = 0; count < 2; count++)
		{
			end = strstr(start, "\r\n");
			if(end == NULL) break;
			*end = 0;

			*wordWork = atoi(start);

			start = end + 2;
			wordWork++;
		}

		if(end != NULL)
		{
			if(ControlData.TotalMsgs == 0)
			{
				/* For some reason, some QWK doors set the total messages to 0, whether
				 * the packet contains messages or not
				 */
				DisplayError(GetString(REQMSG_NO_MESSAGES_TEXT_FORMAT), REQMSG_MISC_GADGET_FORMAT, REQMSG_CONTROL_DAT_TITLE);
			}

			start = CreateConfList(start);
			if(start != NULL)
			{
				work = &ControlData.BBSName;
				for(count = 0; count < 3; count++)
				{
					*work = start;
					end = strstr(start, "\r\n");
					if(end == NULL) break;
					*end = 0;

					start = end + 2;
					work++;
				}

				if(end != NULL)
				{
					return(TRUE);
				}
			}
		}
	}

	PutStr(GetString(ERRMSG_PROCESSING_CONTROL));

	return(FALSE);
}

/* ----------------------------------------------------------------------- */

STRPTR CreateConfList(STRPTR TextConfList)
{
	STRPTR end;
	STRPTR start;
	STRPTR textConfNumber;
	STRPTR work;
	struct Conference *CurrentConference;
	UWORD count = 0;
	ULONG allocation;

	NewList(&ControlData.ConfList);

	PutStr(GetString(INFOMSG_READING_CONFERENCE));

	ControlData.LowConference = 0xFFFF;
	ControlData.HighConference = 0;

	allocation = (sizeof(struct Conference) * ControlData.TotalConfs);
	ControlData.Conferences = AllocPoolVec(allocation);
	if(ControlData.Conferences != NULL)
	{
		CurrentConference = ControlData.Conferences;
		start = TextConfList;
		do
		{
			end = strchr(start, 10);
			if(end == NULL)
			{
				DisplayError(GetString(REQMSG_READING_CONFERENCE_TITLE_FORMAT), REQMSG_MISC_GADGET_FORMAT, REQMSG_CONTROL_DAT_TITLE);

				return(NULL);
			}

			*end = 0;
			textConfNumber = start;
			start = strchr(textConfNumber, 13);
			if(start != NULL) *start = 0;
			CurrentConference -> ConfNumber = atoi(textConfNumber);
			start = end + 1;

			end = strchr(start, 10);
			if(end == NULL)
			{
				DisplayError(GetString(REQMSG_READING_CONFERENCE_TITLE_FORMAT), REQMSG_MISC_GADGET_FORMAT, REQMSG_CONTROL_DAT_TITLE);

				return(NULL);
			}
			*end = 0;

			allocation = (strlen(start) + BaseGroupLen + 8);
			work = AllocPoolVec(allocation);
			if(work == NULL)
			{
				HandleMemoryError(allocation, MISCMSG_CONFERENCE_NAME);

				return(NULL);
			}

			strcpy(work, BaseGroup);
			strcat(work, textConfNumber);
			strcat(work, ".");
			strcat(work, start);
			start = strchr(work, 13);
			if(start != NULL) *start = 0;
			start = end + 1;
			CurrentConference -> Node.ln_Name = work;

			AddTail(&ControlData.ConfList, (struct Node *)CurrentConference);
			if(CurrentConference -> ConfNumber < ControlData.LowConference) ControlData.LowConference = CurrentConference -> ConfNumber;
			if(CurrentConference -> ConfNumber > ControlData.HighConference) ControlData.HighConference = CurrentConference -> ConfNumber;

			CurrentConference++;
			count++;
		}while(count < ControlData.TotalConfs);

		return(start);
	}
	else
	{
		HandleMemoryError(allocation, MISCMSG_CONFERENCE_LIST);
	}

	return(NULL);
}

/* ----------------------------------------------------------------------- */

STRPTR ReadControl(VOID)
{
	BPTR control;
	STRPTR ControlBuffer;
	ULONG allocation;
	ULONG readIn;

	control = Open(CONTROL_NAME, MODE_OLDFILE);
	if(control != NULL)
	{
		if(ExamineFH(control, FileInfoBlock) != FALSE)
		{
			allocation = (FileInfoBlock -> fib_Size + 1);
			ControlBuffer = AllocPoolVec(allocation);
			if(ControlBuffer != NULL)
			{
				readIn = Read(control, ControlBuffer, FileInfoBlock -> fib_Size);
				if(readIn >= FileInfoBlock -> fib_Size)
				{
					Close(control);
					*(ControlBuffer + FileInfoBlock -> fib_Size) = 0;

					return(ControlBuffer);
				}
				else
				{
					HandleIOError(IOMSG_READ);
					FreePoolVec(ControlBuffer);
				}
			}
			else
			{
				HandleMemoryError(allocation, MISCMSG_CONTROL_BUFFER);
			}
		}
		else
		{
			HandleIOError(IOMSG_EXAMINE);
		}
		Close(control);
	}
	else
	{
		HandleIOError(IOMSG_OPEN);
	}

	return(FALSE);
}
