/*
**	$Id: Main.cpp 1.5 1995/09/26 19:45:11 olsen Exp olsen $
**
**	:ts=4
*/

/*
 * Copyright © 1995 by Olaf Barthel, All Rights Reserved
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO
 * EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * This software has not been validated by the ``Bundesamt fuer Zulassungen in
 * der Telekommunikation'' of the ``Deutsche Bundepost Telekom'' and thus
 * must not be used for accessing the BTX-Network of the Telekom in Germany.
 *
 * Diese Software hat keine Zulassung durch das Bundesamt fuer Zulassungen in
 * der Telekommunikation der Deutschen Bundespost Telekom und darf daher nicht
 * am Netz der Deutschen Bundespost Telekom in Deutschland betrieben werden.
 */

#include "Application.hpp"

#include <dos/dosextens.h>
#include <dos/rdargs.h>

#include <clib/dos_protos.h>
#include <clib/utility_protos.h>

#ifdef __SASC
#include <pragmas/dos_pragmas.h>
#include <pragmas/utility_pragmas.h>

extern struct Library		*UtilityBase;
extern struct DosLibrary	*DOSBase;
#endif	// __SASC

#include <stdlib.h>
#include <string.h>
#include <stddef.h>
#include <stdio.h>

#ifdef __SASC
char *verstag = "\0$VER: xBTX 1.1 " __AMIGADATE__ "\r\n";
#else
char *verstag = "\0$VER: xBTX 1.1 (26.09.95)\r\n";
#endif	// __SASC

#define TEMPLATE	"DEVICE/K,UNIT/K/N,BAUD/K/N,RTSCTS/K,INIT/K,DIAL/K,NUMBER/K,HANGUP/K,FILE/K,SCALEX/K/N,SCALEY/K/N,TEXT/S,SCREEN=PUBSCREEN/K,DIRECT/S"

enum	{	ARG_DEVICE,ARG_UNIT,ARG_BAUD,ARG_RTSCTS,ARG_INIT,ARG_DIAL,
			ARG_NUMBER,ARG_HANGUP,ARG_FILE,ARG_SCALEX,ARG_SCALEY,ARG_TEXT,
			ARG_PUBSCREEN,ARG_DIRECT,
			ARGCOUNT
		};

/****** XBTX/XBTX ***********************************************************
*
*   NAME
*       XBTX -- BTX/Datex-J decoder (V39)
*
*   SYNOPSIS
*       DEVICE/K,UNIT/K/N,BAUD/K/N,RTSCTS/K,INIT/K,DIAL/K,NUMBER/K,HANGUP/K,
*           FILE/K,SCALEX/K/N,SCALEY/K/N,TEXT/S,SCREEN=PUBSCREEN/K,DIRECT/S
*
*   FUNCTION
*       XBTX is a BTX/Datex-J decoder developed by Arno Augustin and
*       Frank Höring at the University of Erlangen-Nuremberg, Germany.
*       This Amiga version is based upon the original X-Windows
*       implementation, it adds Telesoftware 3-in-4 format download
*       capabilities.
*
*   REQUIREMENTS
*       XBTX requires Amiga operating system v3.0 or higher, an Amiga
*       equipped with at least an MC68020 CPU and if possible video
*       hardware capable of displaying at least 60 distinctly different
*       colours. If XBTX is unable to allocate at least 60 colours for
*       use, it will fall back to a text-only display. Your Amiga better
*       be real fast, as the BTX font conversion routines are dog slow.
*       XBTX must be started from Shell, it does not run from Workbench yet.
*
*   INPUTS
*       DEVICE/K
*           Amiga device driver to use, default is "serial.device".
*
*       UNIT/K/N
*           Amiga device unit number to use, default is 0.
*
*       BAUD/K/N
*           Baud rate to use; if not specified the default baud rate
*           of the device driver will be used.
*
*       RTSCTS/K
*           Turn RTS/CTS handshaking on or off; if not specified the
*           default hardware handshaking mode of the device driver
*           will be used.
*
*       INIT/K
*           Modem initialization command, default is "ATE1V1Q0^M".
*
*       DIAL/K
*           Modem dialing command to use, default is "~~ATD".
*
*       NUMBER/K
*           Phone number to dial in order to connect to the Datex-J
*           service, default is "01910".
*
*       HANGUP/K
*           Modem hang up command to use, default is "~~+++~~ATH0^M".
*
*       FILE/K
*           Name of prerecorded file to playback.
*
*       SCALEX/K/N
*           Factor by which to scale the display in the horizontal
*           direction, must be between 1 and 8; default is 1. This
*           only works with the graphics display.
*
*       SCALEY/K/N
*           Factor by which to scale the display in the vertical
*           direction, must be between 1 and 8; default is 1. This
*           only works with the graphics display.
*
*       TEXT/S
*           Use the text-only display.
*
*       SCREEN=PUBSCREEN/K
*           Open the display on the named public screen.
*
*       DIRECT/S
*           Build screen characters directly on the screen, do not use
*           a temporary offscreen buffer. Will save some memory and may
*           increase on-screen rendering speed, but then maybe not.
*
*   MODEM COMMANDS
*       You can include simple control sequences in the modem commands.
*       The tilde (~) causes a delay of about a second. Prefixing a
*       character with a caret (^) will turn it into a control code,
*       for example ^M gives a carriage return (or Control-M).
*
*   EXAMPLE
*       Dial into the fast modem service with RTS/CTS handshaking
*       enabled:
*           xbtx rtscts=on number=051119304
*
*       Dial into the slow modem service with RTS/CTS handshaking
*       disabled:
*           xbtx rtscts=off
*
*       Open a text-only display on the `term' public screen:
*           xbtx text screen=term
*
*   NOTES
*       This software has not been validated or approved by the
*       "Bundesamt für Zulassungen in der Telekommunikation" of the
*       "Deutsche Bundepost Telekom" and thus must not be used for
*       accessing the BTX-Network of the Telekom in Germany. It is
*       provided solely for educational purposes only.
*
*       This software is provided by the authors "as is" and any
*       express or implied warranties, including, but not limited
*       to, the implied warranties of merchantability and fitness
*       for a particular purpose are disclaimed. In no event shall
*       the authors be liable for any direct, indirect, incidental,
*       special, exemplary, or consequential damages (including,
*       but not limited to, procurement of substitute goods or
*       services; loss of use, data, or profits; or business
*       interruption) however caused and on any theory of
*       liability, whether in contract, strict liability, or tort
*       (including negligence or otherwise) arising in any way out
*       of the use of this software, even if advised of the
*       possibility of such damage.
*
*       You have been warned, kids.
*
*
*       Incidentally, this is my first major attempt at C++
*       programming. Any changes I made to the code are
*       really © Copyright 1995 by Olaf Barthel.
*
*   BUGS
*       The font decoding and output routines are neither blazingly
*       fast, nor shockingly well designed, but they get the job
*       done. Flashing characters are not supported (yet). The
*       Telesoftware download routines do not support the eight bit
*       transparency mode as the specifications were a bit thin.
*       Concealed characters are always revealed, there is no option
*       to have them concealed.
*
*   AUTHORS
*       Arno Augustin, University of Erlangen-Nuremberg,
*           (aoaugust@cip.informatik.uni-erlangen.de)
*
*       Frank Höring, University of Erlangen-Nuremberg,
*           (fkhoerin@cip.informatik.uni-erlangen.de)
*
*       Olaf Barthel
*           (olsen@sourcery.han.de)
*
*   SEE ALSO
*       "Functional Specification for BTX-Terminals", DBP Telekom
*       xbtx - a Bildschirmtext-decoder for the X-Window system
*
*****************************************************************************
*
*/

void
main(int argc,char **argv)
{
	if(argc)
	{
		extern struct DosLibrary *DOSBase;

		if(DOSBase -> dl_lib . lib_Version >= 39)
		{
			struct RDArgs	*Args;
			STRPTR			 Array[ARGCOUNT];

			memset(Array,0,sizeof(Array));

			if(Args = ReadArgs(TEMPLATE,(LONG *)Array,NULL))
			{
				Application	App;
				STRPTR		Device;
				LONG		Unit;
				LONG		Baud;
				BOOL		RTS_CTS;
				BOOL		TextOnly;
				BOOL		Direct;
				STRPTR		Init;
				STRPTR		Dial;
				STRPTR		Number;
				STRPTR		Hangup;
				STRPTR		File;
				LONG		ScaleX,
							ScaleY;
				STRPTR		Screen;
				STRPTR		Error;

				if(Array[ARG_DEVICE])
					Device = Array[ARG_DEVICE];
				else
					Device = "serial.device";

				if(Array[ARG_UNIT])
					Unit = *(LONG *)Array[ARG_UNIT];
				else
					Unit = 0;

				if(Array[ARG_BAUD])
					Baud = *(LONG *)Array[ARG_BAUD];
				else
					Baud = 0;

				if(Array[ARG_RTSCTS])
				{
					STATIC struct { STRPTR String; BOOL Value; } BoolTable[] =
					{
						"on",	TRUE,
						"true",	TRUE,
						"yes",	TRUE,
						"y",	TRUE,
						"t",	TRUE,

						"off",	FALSE,
						"false",FALSE,
						"no",	FALSE,
						"n",	FALSE,
						"f",	FALSE,

						NULL
					};

					WORD i;

					RTS_CTS = -1;

					for(i = 0 ; BoolTable[i] . String ; i++)
					{
						if(!Stricmp(BoolTable[i] . String,Array[ARG_RTSCTS]))
						{
							RTS_CTS = BoolTable[i] . Value;
							break;
						}
					}
				}
				else
					RTS_CTS = -1;

				if(Array[ARG_INIT])
					Init = Array[ARG_INIT];
				else
					Init = "ATE1V1Q0^M";

				if(Array[ARG_DIAL])
					Dial = Array[ARG_DIAL];
				else
					Dial = "~~ATD";

				if(Array[ARG_NUMBER])
					Number = Array[ARG_NUMBER];
				else
					Number = "01910";

				if(Array[ARG_HANGUP])
					Hangup = Array[ARG_HANGUP];
				else
					Hangup = "~~+++~~ATH0^M";

				if(Array[ARG_FILE])
					File = Array[ARG_FILE];
				else
					File = NULL;

				if(Array[ARG_SCALEX])
					ScaleX = *(LONG *)Array[ARG_SCALEX];
				else
					ScaleX = 1;

				if(Array[ARG_SCALEY])
					ScaleY = *(LONG *)Array[ARG_SCALEY];
				else
					ScaleY = 1;

				if(Array[ARG_TEXT])
					TextOnly = TRUE;
				else
					TextOnly = FALSE;

				if(Array[ARG_DIRECT])
					Direct = TRUE;
				else
					Direct = FALSE;

				if(Array[ARG_PUBSCREEN])
					Screen = Array[ARG_PUBSCREEN];
				else
					Screen = NULL;

				Error = App.Open(Screen,ScaleX,ScaleY,TextOnly,Direct,File ? File : Device,Unit,Baud,RTS_CTS);

				if(Error)
					Printf("xBTX: %s\n",Error);
				else
				{
					if(App.AppModem)
					{
						if(App.AppModem->Connect(Init,Dial,Number,Hangup) == MODEM_Connect)
							App.DoEvent();
						else
							App.WaitForUserInput();
					}
					else
						App.DoEvent();

					App.Close();
				}

				FreeArgs(Args);

				exit(RETURN_OK);
			}
			else
			{
				PrintFault(IoErr(),"xBTX");

				exit(RETURN_FAIL);
			}
		}
		else
			exit(RETURN_FAIL);
	}
}
