/* 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/lists.h>

#include <proto/dos.h>
#include <proto/utility.h>

#include <stdio.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 ArchiveFile(STRPTR);
Prototype BOOL FindPacket(VOID);
Prototype BOOL UnarchiveFile(VOID);

BOOL ArchiveFile(STRPTR File)
{
	STRPTR command;
	STRPTR currentPos;
	const TEXT destPlacement[] = {"%d"};
	const TEXT sourcePlacement[] = {"%s"};
	ULONG allocation;

	if(strstr(ArcCmd, destPlacement) != NULL)
	{
		if(strstr(ArcCmd, sourcePlacement) != NULL)
		{
			allocation = (strlen(ArcCmd) + strlen(ArchiveFilename) + strlen(File) + 1);
			command = (STRPTR)AllocPoolVec(allocation);
			if(command != NULL)
			{
				strcpy(command, ArcCmd);
				currentPos = strstr(command, sourcePlacement);
				strcpy(currentPos, currentPos + 2);
				strins(currentPos, File);
				currentPos = strstr(command, destPlacement);
				strcpy(currentPos, currentPos + 2);
				strins(currentPos, ArchiveFilename);

				SystemTagList(command, ioSystemTags);

				FreePoolVec(command);

				return(TRUE);
			}
			else
			{
				HandleMemoryError(allocation, MISCMSG_ARCHIVE_COMMAND);
			}
		}
		else
		{
			SPrintF(FormatBuffer, GetString(ERRMSG_NO_FOUND_IN), sourcePlacement, GetString(MISCMSG_ARCHIVE_COMMAND));
			PutStr(FormatBuffer);
		}
	}
	else
	{
		SPrintF(FormatBuffer, GetString(ERRMSG_NO_FOUND_IN), destPlacement, GetString(MISCMSG_ARCHIVE_COMMAND));
		PutStr(FormatBuffer);
	}

	return(FALSE);
}

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

BOOL FindPacket(VOID)
{
	BOOL found = FALSE;
	BOOL isPattern = FALSE;
	BOOL success;
	STRPTR pattern;
	const TEXT patternEnd[] = {".QWK"};
	UWORD allocation;
	UWORD patternLength;

	if(PacketName == NULL)
	{
		patternLength = strlen(Site) + 5;
		pattern = AllocPoolVec(patternLength);
		if(pattern == NULL)
		{
			HandleMemoryError(patternLength, MISCMSG_PATTERN);
			return(FALSE);
		}

		strcpy(pattern, Site);
		strcat(pattern, patternEnd);
	}
	else
	{
		patternLength = (strlen(PacketName) * 2) + 2;
		pattern = AllocPoolVec(patternLength);
		if(pattern == NULL)
		{
			HandleMemoryError(patternLength, MISCMSG_PATTERN);
			return(FALSE);
		}

		isPattern = ParsePatternNoCase(PacketName, pattern, patternLength);
		if(isPattern == -1)
		{
			HandleIOError(IOMSG_PARSE_PATTERN_NOCASE);
			return(FALSE);
		}

		if(isPattern == FALSE) strcpy(pattern, PacketName);
	}

	success = Examine(InboundDirLock, FileInfoBlock);
	if(success != FALSE)
	{
		success = ExNext(InboundDirLock, FileInfoBlock);
		while(success != FALSE && found == FALSE)
		{
			if(isPattern == FALSE)
			{
				if(Stricmp(pattern, FileInfoBlock -> fib_FileName) == 0)
				{
					found = TRUE;
					allocation = strlen(Inbound) + strlen(FileInfoBlock -> fib_FileName) + 2;
					ArchiveFilename = CreateFullName(Inbound, FileInfoBlock -> fib_FileName);
					break;
				}
			}
			else
			{
				if(MatchPatternNoCase(pattern, FileInfoBlock -> fib_FileName) == TRUE)
				{
					found = TRUE;
					allocation = strlen(Inbound) + strlen(FileInfoBlock -> fib_FileName) + 2;
					ArchiveFilename = CreateFullName(Inbound, FileInfoBlock -> fib_FileName);
					break;
				}
			}
			success = ExNext(InboundDirLock, FileInfoBlock);
		}
	}
	else
	{
		HandleIOError(IOMSG_EXAMINE);
	}

	UnLock(InboundDirLock);		/* InboundDirLock is no longer needed */
	FreePoolVec(pattern);
	InboundDirLock = NULL;

	if(found != FALSE)
	{
		if(ArchiveFilename != NULL)
		{
			return(TRUE);
		}
		else
		{
			HandleMemoryError(allocation, MISCMSG_ARCHIVE_FILENAME);
		}
	}
	else
	{
		PutStr(GetString(ERRMSG_UNABLE_FIND_PACKET));
	}

	return(FALSE);
}																								

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

BOOL UnarchiveFile(VOID)
{
	STRPTR command;
	STRPTR currentPos;
	const TEXT sourcePlacement[3] = {"%s"};
	ULONG allocation;

	if(strstr(DearcCmd, sourcePlacement) != NULL)
	{
		allocation = (strlen(DearcCmd) + strlen(ArchiveFilename) + 1);
		command = (STRPTR)AllocPoolVec(allocation);
		if(command != NULL)
		{
			strcpy(command, DearcCmd);
			currentPos = strstr(command, sourcePlacement);
			strcpy(currentPos, currentPos + 2);
			strins(currentPos, ArchiveFilename);

			SystemTagList(command, ioSystemTags);

			FreePoolVec(command);

			return(TRUE);
		}
		else
		{
			HandleMemoryError(allocation, MISCMSG_DEARCHIVE_COMMAND);
		}
	}
	else
	{
		SPrintF(FormatBuffer, GetString(ERRMSG_NO_FOUND_IN), sourcePlacement, GetString(MISCMSG_DEARCHIVE_COMMAND));
		PutStr(FormatBuffer);
	}

	return(FALSE);
}
