/*
**	termCapture.c
**
**	File and printer capture support routines
**
**	Copyright © 1990-1993 by Olaf `Olsen' Barthel & MXM
**		All Rights Reserved
*/

#include "termGlobal.h"

	/* Capture(APTR Buffer,LONG Size):
	 *
	 *	Send the buffer contents to the display/disk capture.
	 */

VOID __regargs
Capture(APTR Buffer,LONG Size)
{
		/* Send the filtered data to the capture file. */

	if(Config -> CaptureConfig -> CaptureFilterMode)
		CaptureToFile(Buffer,Size);

		/* Store data in the log book. */

	if(!BufferFrozen)
		StoreBuffer(Buffer,Size);

		/* Send the buffer to the printer. */

	if(PrinterCapture && Size)
	{
		if(FWrite(PrinterCapture,Buffer,Size,1) != 1)
		{
			BlockWindows();

			if(!MyEasyRequest(Window,LocaleString(MSG_CONSOLE_ERROR_WRITING_TO_PRINTER_TXT),LocaleString(MSG_CONSOLE_IGNORE_CLOSE_PRINTER_TXT)))
			{
				Close(PrinterCapture);

				CheckItem(MEN_CAPTURE_TO_PRINTER,FALSE);

				PrinterCapture = NULL;

				ConOutputUpdate();
			}

			ReleaseWindows();
		}
	}
}

	/* ClosePrinterCapture(BYTE Force):
	 *
	 *	Closes printer capture file.
	 */

VOID
ClosePrinterCapture(BYTE Force)
{
	if(PrinterCapture)
	{
		if(ControllerActive && StandardPrinterCapture && !Force)
			FPrintf(PrinterCapture,LocaleString(MSG_CONSOLE_TERMINAL_TRANSCRIPT_ENDING_TXT));

		if(Force)
		{
			Close(PrinterCapture);

			CheckItem(MEN_CAPTURE_TO_PRINTER,FALSE);

			PrinterCapture = NULL;

			ConOutputUpdate();

			StandardPrinterCapture = FALSE;
		}
	}

	ControllerActive = FALSE;

	ConOutputUpdate();
}

	/* OpenPrinterCapture(BYTE Controller):
	 *
	 *	Opens printer capture file.
	 */

BYTE
OpenPrinterCapture(BYTE Controller)
{
	if(PrinterCapture)
	{
		if(Controller && !ControllerActive)
		{
			ControllerActive = TRUE;

			ConOutputUpdate();

			FPrintf(PrinterCapture,LocaleString(MSG_CONSOLE_TERMINAL_TRANSCRIPT_FOLLOWS_TXT));
		}

		return(TRUE);
	}
	else
	{
		if(PrinterCapture = Open("PRT:",MODE_NEWFILE))
			CheckItem(MEN_CAPTURE_TO_PRINTER,TRUE);
		else
		{
			CheckItem(MEN_CAPTURE_TO_PRINTER,FALSE);

			BlockWindows();

			MyEasyRequest(Window,LocaleString(MSG_TERMMAIN_ERROR_OPENING_PRINTER_TXT),LocaleString(MSG_GLOBAL_CONTINUE_TXT),"PRT:");

			ReleaseWindows();
		}

		if(Controller)
		{
			ControllerActive	= TRUE;
			StandardPrinterCapture	= FALSE;
		}
		else
		{
			StandardPrinterCapture = FALSE;

			if(ControllerActive)
				FPrintf(PrinterCapture,LocaleString(MSG_CONSOLE_USER_TERMINAL_TRANSCRIPT_FOLLOWS_TXT));
		}

		ConOutputUpdate();

		if(PrinterCapture)
			return(TRUE);
		else
			return(FALSE);
	}
}

	/* CloseFileCapture():
	 *
	 *	Close the capture file.
	 */

VOID
CloseFileCapture()
{
	if(FileCapture)
	{
		BufferClose(FileCapture);

		FileCapture = NULL;

		if(!GetFileSize(CaptureName))
			DeleteFile(CaptureName);
		else
		{
			AddProtection(CaptureName,FIBF_EXECUTE);

			if(Config -> MiscConfig -> CreateIcons)
				AddIcon(CaptureName,FILETYPE_TEXT,FALSE);
		}
	}

	CheckItem(MEN_CAPTURE_TO_FILE,FALSE);

	ConOutputUpdate();
}

	/* OpenFileCapture():
	 *
	 *	Open a capture file.
	 */

BYTE
OpenFileCapture()
{
	struct FileRequester	*FileRequest;
	UBYTE			 DummyBuffer[MAX_FILENAME_LENGTH],
				*DummyChar;
	BYTE			 Aborted = FALSE;

	CloseFileCapture();

	BlockWindows();

	if(!CaptureName[0])
	{
		strcpy(CaptureName,Config -> CaptureConfig -> CapturePath);

		if(!AddPart(CaptureName,LocaleString(MSG_DIALPANEL_CAPTURE_NAME_TXT),MAX_FILENAME_LENGTH))
			CaptureName[0] = 0;
	}

	strcpy(DummyBuffer,CaptureName);

	DummyChar = PathPart(DummyBuffer);

	*DummyChar = 0;

	if(FileRequest = GetFile(LocaleString(MSG_TERMMAIN_CAPTURE_TO_DISK_TXT),DummyBuffer,FilePart(CaptureName),DummyBuffer,NULL,TRUE,FALSE,FALSE,LocaleString(MSG_GLOBAL_OPEN_TXT),FALSE))
	{
		if(GetFileSize(DummyBuffer))
		{
			switch(MyEasyRequest(Window,LocaleString(MSG_GLOBAL_FILE_ALREADY_EXISTS_TXT),LocaleString(MSG_GLOBAL_CREATE_APPEND_CANCEL_TXT),DummyBuffer))
			{
				case 1:

					FileCapture = BufferOpen(DummyBuffer,"w");
					break;

				case 2:

					FileCapture = BufferOpen(DummyBuffer,"a");
					break;

				case 0:

					FileCapture = NULL;

					Aborted = TRUE;

					break;
			}
		}
		else
			FileCapture = BufferOpen(DummyBuffer,"w");

		if(!Aborted)
		{
			if(!FileCapture)
				MyEasyRequest(Window,LocaleString(MSG_GLOBAL_ERROR_OPENING_FILE_TXT),LocaleString(MSG_GLOBAL_CONTINUE_TXT),DummyBuffer);
			else
				strcpy(CaptureName,DummyBuffer);
		}

		FreeAslRequest(FileRequest);
	}

	if(FileCapture)
		CheckItem(MEN_CAPTURE_TO_FILE,TRUE);
	else
		CheckItem(MEN_CAPTURE_TO_FILE,FALSE);

	ConOutputUpdate();

	ReleaseWindows();

	if(FileCapture)
		return(TRUE);
	else
		return(FALSE);
}

	/* CaptureToFile(APTR Buffer,LONG Size):
	 *
	 *	Send data to the capture file.
	 */

VOID __regargs
CaptureToFile(APTR Buffer,LONG Size)
{
	if(FileCapture && Size)
	{
		if(BufferWrite(FileCapture,Buffer,Size) != Size)
		{
			BlockWindows();

				/* We had an error writing to the file. */

			switch(MyEasyRequest(NULL,LocaleString(MSG_CONSOLE_ERROR_WRITING_TO_CAPTURE_FILE_TXT),LocaleString(MSG_CONSOLE_IGNORE_DISCARD_CLOSE_TXT),CaptureName))
			{
				case 1:

					BufferClose(FileCapture);

					DeleteFile(CaptureName);

					CheckItem(MEN_CAPTURE_TO_FILE,FALSE);

					FileCapture = NULL;

					ConOutputUpdate();

					break;

				case 2:

					BufferClose(FileCapture);

					CheckItem(MEN_CAPTURE_TO_FILE,FALSE);

					FileCapture = NULL;

					if(!GetFileSize(CaptureName))
						DeleteFile(CaptureName);
					else
					{
						AddProtection(CaptureName,FIBF_EXECUTE);

						if(Config -> MiscConfig -> CreateIcons)
							AddIcon(CaptureName,FILETYPE_TEXT,FALSE);
					}

					ConOutputUpdate();

					break;
			}

			ReleaseWindows();
		}
	}
}
