/*
**	termCall.c
**
**	CallInfo-compatible log file maintenance routines
**
**	Copyright © 1990-1992 by Olaf `Olsen' Barthel & MXM
**		All Rights Reserved
*/

#include "termGlobal.h"

	/* Some local variables. */

STATIC BPTR		CallFile;
STATIC struct timeval	CallTime;

	/* CallDate():
	 *
	 *	Add the current date and time to the logfile.
	 */

STATIC VOID
CallDate(VOID)
{
		/* Days of the week. */

	STATIC STRPTR CallDays[7] =
	{
		"Sun","Mon","Tue","Wed","Thu","Fri","Sat"
	};

		/* Months of the year. */

	STATIC STRPTR CallMonths[12] =
	{
		"Jan","Feb","Mar","Apr","Mai","Jun","Jul","Aug","Sep","Oct","Nov","Dec"
	};

	struct DateStamp __aligned	Date;
	struct ClockData		ClockData;

		/* Obtain current date. */

	DateStamp(&Date);

		/* Convert time and date. */

	Amiga2Date((Date . ds_Days * 86400) + (Date . ds_Minute * 60) + (Date . ds_Tick / TICKS_PER_SECOND),&ClockData);

		/* Add the date line. */

	FPrintf(CallFile,"%s %s %02ld %02ld:%02ld:%02ld %ld\n",CallDays[ClockData . wday],CallMonths[ClockData . month - 1],ClockData . mday,ClockData . hour,ClockData . min,ClockData . sec,ClockData . year);
}

	/* MakeCall(struct PhoneEntry *Entry):
	 *
	 *	Register a new phone call.
	 */

VOID
MakeCall(STRPTR Name,STRPTR Number)
{
		/* End previous entry. */

	if(CallFile)
		StopCall(FALSE);

	if(Config . CaptureConfig . LogCall)
	{
		STRPTR File;

		strcpy(SharedBuffer,Config . CaptureConfig . LogFileName);

		if(File = PathPart(SharedBuffer))
			*File = 0;

		if(AddPart(SharedBuffer,"term-call.log",256))
		{
				/* Open logfile for writing. */

			if(CallFile = Open(SharedBuffer,MODE_READWRITE))
			{
					/* Seek to the end of it (append). */

				if(Seek(CallFile,0,OFFSET_END) != -1)
				{
						/* Get current system time. */

					TimeRequest -> tr_node . io_Command = TR_GETSYSTIME;

					DoIO(TimeRequest);

						/* Remember the starting time, we will need
						 * it later.
						 */

					CallTime = TimeRequest -> tr_time;

						/* Add the title line. */

					FPrintf(CallFile,"%s (%s)\n--------------------------------\nLogin:  ",Name,Number);

						/* Make the line complete. */

					CallDate();
				}
				else
				{
					Close(CallFile);

					CallFile = NULL;
				}
			}
		}
	}
}

	/* StopCall(BYTE Finish):
	 *
	 *	End the current phone call.
	 */

VOID
StopCall(BYTE Finish)
{
		/* Is a call currently being made? */

	if(CallFile)
	{
		struct timeval	StopTime;
		BYTE		GotName;

			/* Get current system time. */

		TimeRequest -> tr_node . io_Command = TR_GETSYSTIME;

		DoIO(TimeRequest);

			/* Remember it. */

		StopTime = TimeRequest -> tr_time;

			/* Subtract the starting time from it. */

		SubTime(&StopTime,&CallTime);

			/* Add the info line. */

		if(Finish)
			FPrintf(CallFile,"*** term exited before logout: ");
		else
			FPrintf(CallFile,"Logout: ");

			/* Make the line complete. */

		CallDate();

			/* Get the file name. */

		GotName = NameFromFH(CallFile,SharedBuffer,256);

			/* Add the online time. */

		FPrintf(CallFile,"Time online: %02ld:%02ld:%02ld\n\n",(StopTime . tv_secs % 86400) / 3600,(StopTime . tv_secs % 3600) / 60,StopTime . tv_secs % 60);

			/* Finis... */

		Close(CallFile);

		CallFile = NULL;

			/* Clear the `executable' bit. */

		if(GotName)
			SetProtection(SharedBuffer,FIBF_EXECUTE);
	}
}
