/* Euromail - TransAmiga Door Converter V1.0 beta */
/* (c)1991 by Christian Buchner alias Flowerpower */

#include	<stdio.h>
#include	<exec/types.h>
#include	<proto/all.h>
#include	<devices/serial.h>

struct	TADoor
{
	UBYTE	SystemPath[80];
	UBYTE	ConfigPath[80];
	UBYTE	TextFilePath[80];
	UBYTE	BulletinPath[80];
	UBYTE	DoorPath[80];
	UBYTE	CallerType;
	UBYTE	ScreenLength;
	UBYTE	UserName[20];
	UBYTE	Unknown;				/* ??? */
	UBYTE	AccessLevel;
	UWORD	BaudRate;
	UBYTE	OnlineTime;
	UBYTE	UserSetupFile[100];
	UBYTE	DeviceName[32];
	struct	Preferences	Prefs;
};

struct	TADoor	TADoor =
{
	"SYSTEMPATH:",
	"CONFIGPATH:",
	"TEXTFILEPATH:",
	"BULLETINPATH:",
	"DOORPATH:",
	1,
	30,
	"",
	0,			/* ??? */
	0,
	0,
	0,
	"USERSETUPFILE:",
	"serial.device"
};

void	OpenSerial();
void	CloseSerial();

struct	MsgPort		*SerPort;
struct	IOExtSer	*SerIO;

void	main(UWORD argc, UBYTE **argv)
{
	FILE	*emfile;
	FILE	*tafile;
	ULONG	BaudRate;
	UBYTE	UserName[32];
	ULONG	AccessLevel;
	ULONG	UsedOnlineTime;
	ULONG	LeftOnlineTime;
	ULONG	LoginMode;
	UBYTE	Terminal[32];
	UBYTE	CommandBuffer[256];
	int		ErrorCode;
	int		i;
	if (argc == 1)
	{
		printf("EMTA V1.0 (c)1991 by Christian Buchner alias Flowerpower\n");
		printf("--------------------------------------------------------\n");
		printf("Usage: EMTA <Parameters to pass to PTA or PLOC>\n");
		exit(0);
	}
	else
	{
		if (!(emfile = fopen("RAM:USERPARAM","rb")))
		{
			printf("EMTA was not started from EUROMAIL.\n");
			printf("Invoking PLOC for local door test.\n");
			LoginMode = 1;
		}
		else
		{
			if (fscanf(emfile,"%ld\n%s\n%ld\n%ld\n%ld\n%ld\n%s",&BaudRate,&UserName,&AccessLevel,&UsedOnlineTime,&LeftOnlineTime,&LoginMode,&Terminal) != 7)
			{
				printf("Crippled Euromail Door information!\n");
				printf("Aborting...");
				fclose(emfile);
				exit(20);
			}
			fclose(emfile);
		}
	}
	if (LoginMode == 0)
	{
		TADoor.BaudRate = BaudRate;
		strncpy(TADoor.UserName, UserName, 20);
		TADoor.AccessLevel = AccessLevel;
		TADoor.OnlineTime = LeftOnlineTime;
		if (!(tafile = fopen("T:Door.Temp", "wb")))
		{
			printf("Can't open TransAmiga Door information file.\n");
			printf("Aborting...\n");
			exit(20);
		}
		if (fwrite((char *)&TADoor, sizeof(struct TADoor), 1, tafile) != 1)
		{
			printf("Can't write TransAmiga Door information file.\n");
			printf("Aborting...\n");
			fclose(tafile);
			exit(20);
		}
		fclose(tafile);
		OpenSerial();
		sprintf(CommandBuffer,"PTA");
		for (i=1;i<argc;i++)
		{
			strcat(CommandBuffer," ");
			strcat(CommandBuffer,argv[i]);
		}
		ErrorCode = system(CommandBuffer);
		if (ErrorCode < 0)
		{
			printf("Unable to start PTA!\n");
			printf("Aborting...\n");
			CloseSerial();
			exit(20);
		}
		if (ErrorCode > 0)
		{
			printf("Error Code: %d\n",ErrorCode);
			printf("Aborting...");
			CloseSerial();
			exit(ErrorCode);
		}
		CloseSerial();
	}
	if (LoginMode == 1)
	{
		sprintf(CommandBuffer,"PLoc");
		for (i=1;i<argc;i++)
		{
			strcat(CommandBuffer," ");
			strcat(CommandBuffer,argv[i]);
		}
		ErrorCode = system(CommandBuffer);
		if (ErrorCode < 0)
		{
			printf("Unable to start PLoc!\n");
			printf("Aborting...\n");
			exit(20);
		}
		if (ErrorCode > 0)
		{
			printf("PLoc returned errorcode %d\n",ErrorCode);
			printf("Aborting...\n");
			exit(ErrorCode);
		}
	}
	exit(0);
}

void	OpenSerial()
{
	SerPort = CreatePort(0,0);
	if (!(SerIO = (struct IOExtSer*)CreateExtIO(SerPort,sizeof(struct IOExtSer))))
	{
		printf("Can't get serial IO structures.\n");
		printf("Aborting...\n");
		CloseSerial();
		exit(20);
	}
	SerIO->io_SerFlags = SERF_SHARED|SERF_7WIRE;;
	if (OpenDevice("serial.device",0,(struct IORequest*)SerIO,0))
	{
		printf("Can't open serial device.\n");
		printf("Aborting...\n");
		CloseSerial();
		exit(20);
	}
	SerIO->io_RBufLen = 1024;
	SerIO->io_Baud = TADoor.BaudRate;
	SerIO->io_ReadLen = 8;
	SerIO->io_WriteLen = 8;
	SerIO->io_StopBits = 1;
	SerIO->io_SerFlags = SERF_SHARED|SERF_7WIRE|SERF_RAD_BOOGIE;
	SerIO->IOSer.io_Command = SDCMD_SETPARAMS;
	if (DoIO((struct IORequest*)SerIO))
	{
		printf("Can't set serial parameters.\n");
		printf("Aborting...\n");
		CloseSerial();
		exit(20);
	}
}

void	CloseSerial()
{
	if (SerIO) if (SerIO->IOSer.io_Device)
	{
		CloseDevice((struct IORequest*)SerIO);
	}
	if (SerIO) DeleteExtIO((struct IORequest*)SerIO);
	if (SerPort) DeletePort(SerPort);
}
