/*
**	termCall.c
**
**	CallInfo-compatible log file maintenance routines
**
**	Copyright © 1990-1994 by Olaf `Olsen' Barthel
**		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","May","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 __regargs
MakeCall(STRPTR Name,STRPTR Number)
{
		/* End previous entry. */

	if(CallFile)
		StopCall(FALSE);

		/* 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;

		/* Call logging enabled? */

	if(Config -> CaptureConfig -> LogCall && Config -> CaptureConfig -> CallLogFileName[0])
	{
			/* Open logfile for writing. */

		if(CallFile = Open(Config -> CaptureConfig -> CallLogFileName,MODE_READWRITE))
		{
				/* Seek to the end of it (append). */

			if(Seek(CallFile,0,OFFSET_END) != -1)
			{
					/* 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 __regargs
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,MAX_FILENAME_LENGTH);

			/* 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)
		{
			AddProtection(SharedBuffer,FIBF_EXECUTE);

			if(Config -> MiscConfig -> CreateIcons)
				AddIcon(SharedBuffer,FILETYPE_TEXT,TRUE);
		}
	}
}
