/*
 *	File:					Converter.c
 *	Description:	Program to convert a Recall project to different formats.
 *	Version:			1.1
 *	Author:				Ketil Hunn
 *	Mail:					ketil@brosme.dhmolde.no
 *								hunn@dhmolde.no
 *
 *	Copyright © 1993 Ketil Hunn.  Please read the document for further
 *	information and copyrights.
 *
 *	In order to list the source-files included in this package properly,
 *	the TAB size should be set at 2.
 *
 */

#include <MUI.h>
#include <libraries/locale.h>
#include <clib/locale_protos.h>
#include <pragmas/locale_pragmas.h>

#define  CATCOMP_NUMBERS
#include "ReConvert_locale.h"

struct FileRequester *aslreq=NULL;
extern struct Library *SysBase;
struct Library *LocaleBase;
struct LocaleInfo li;

/* PROTOTYPES */
void CheckReady(char source[256], char destination[256]);
BOOL Convert(long intype, char *source, long outtype, char *destination);
STRPTR __asm GetString(register __a0 struct LocaleInfo *li,
                       register __d0 LONG stringNum);


#include "Escapes.h"
#include "Version.h"
#include "GUI_Converter.h"
#include <exec/memory.h>
#include "GlobalConfig.h"
#include "Exists().h"

void MyRequest(char *buttons, char *format, char *params)
{
	if(MUIMasterBase)
		MUI_Request(app, window, 0, NULL, buttons, format, params);
}

#include "Config14.h"
#include "Config20.h"
#include "ASCII.h"
//#include "Text.h"
#include "GetFile().h"
//#include "CalcField.h"

#define TEMPLATE "INTYPE/A/N,FILE1/A,OUTTYPE/A/N,FILE2/A"
BOOL parameters=FALSE;

void __main(char *AIDS_KILLS)
{
	ULONG	signals;
	BOOL	running=TRUE, getfileflag;
	char  *source, *destination;
	struct RDArgs		*args;
	LONG						arg[4];

	// Using standar AmigaDOS parsing routines
	if(args=ReadArgs(TEMPLATE, arg, NULL))
	{
		LONG intype =*((LONG *)arg[0]);
		LONG outtype=*((LONG *)arg[2]);
		parameters=TRUE;

		Convert(intype, (UBYTE *)arg[1], outtype, (UBYTE *)arg[3]);
		FreeArgs(args);
		exit(0);
	}

	li.li_Catalog=NULL;
	if(LocaleBase=OpenLibrary("locale.library", 0))
	{
		li.li_LocaleBase = LocaleBase;
		li.li_Catalog    = OpenCatalogA(NULL,"Recall/" CONVERTERNAME".catalog",NULL);
	}

	if(app=SetupGUI())
	{
		if(aslreq=MUI_AllocAslRequestTags(ASL_FileRequest,TAG_DONE))
		{
			if((source=malloc(FNSIZE)) && (destination=malloc(FNSIZE)))
			{
				strcpy(source, DEFSOURCE);
				strcpy(destination, DEFDESTINATION);
				set(sourceString, MUIA_String_Contents, source);
				set(destinationString, MUIA_String_Contents, destination);

				CheckReady(source,destination);
				set(window, MUIA_Window_Open, TRUE);
				set(window, MUIA_Window_ActiveObject, sourceString);

				while(running)
				{
					switch(DoMethod(app, MUIM_Application_Input, &signals))
					{
						case MUIV_Application_ReturnID_Quit:
							running=FALSE;
							break;

						case ID_ABOUT:
							MUI_Request(app, window, 0, NULL, "*OK",
													aCENTRE aLIGHT
													CONVERTERNAME " " CONVVERSION " " COPYRIGHT ".\n\n"
													aNORMAL aDARK
													"%s\n\n"
													"%s\n\n"
													"%s Public Domain\n\n"
													"%s " CURRENT_DATE,
													GetString(&li, MSG_ABOUTDESCRIPTION),
													GetString(&li, MSG_ABOUTHELP),
													GetString(&li, MSG_PUBLICDOMAIN),
													GetString(&li, MSG_COMPILED));
							break;

						case ID_GETSOURCE:
							getfileflag=TRUE;

						case ID_SOURCESTRING:
							if(getfileflag)
							{
								if(GetFile(GetWinPtr(window),
												aslreq,
												GetString(&li, MSG_SELECTSOURCE),
												source,
												""))
									set(window, MUIA_Window_ActiveObject, sourceString);
							}
							else
								get(sourceString, MUIA_String_Contents, &source);
							set(sourceString, MUIA_String_Contents, source);
							CheckReady(source, destination);
							getfileflag=FALSE;
							break;

						case ID_GETDESTINATION:
							getfileflag=TRUE;

						case ID_DESTINATIONSTRING:
							if(getfileflag)
							{
								if(GetFile(GetWinPtr(window),
												aslreq,
												GetString(&li, MSG_SELECTDESTINATION),
												destination,
												""))
									set(window, MUIA_Window_ActiveObject, destinationString);
							}
							else
								get(destinationString, MUIA_String_Contents, &destination);
							set(destinationString, MUIA_String_Contents, destination);
							CheckReady(source, destination);
							getfileflag=FALSE;
							break;

						case ID_CONVERT:
							set(window, MUIA_Window_Sleep, TRUE);
							if(!Convert(Active(sourceListView), source,
													Active(destinationListView),destination))
								MUI_Request(app, window, 0, NULL, "*OK",
														GetString(&li, MSG_IFFLOADERROR));
							set(window, MUIA_Window_Sleep, FALSE);
							break;
					}

					if (running && signals)
						Wait(signals);
				}
				set(window,MUIA_Window_Open,FALSE);
			}
			MUI_FreeAslRequest(aslreq);
		}
		fail(app,NULL);
	}
	if(LocaleBase)
	{
		CloseCatalog(li.li_Catalog);
		CloseLibrary(LocaleBase);
	}
}

void CheckReady(char source[256], char destination[256])
{
	if(Exists(source)==TRUE & strlen(destination)>0)
		set(convert, MUIA_Disabled, FALSE);
	else
		set(convert, MUIA_Disabled, TRUE);
}

BOOL Convert(long intype, char *source,
																long outtype, char *destination)
{
	struct List		*list;
	BOOL success;

	if(list=AllocMem(sizeof(struct List), MEMF_CLEAR))
	{
		NewList(list);

		switch(intype)
		{
			case LOAD_ASCII:
				success=ReadASCII(list, source);
				break;

			case LOAD_V1X:
				success=ReadPrefs14(list, source);
				break;

			case LOAD_V2X:
				success=ReadIFF(list, source);
				break;
		}

		if(success)
			switch(outtype)
			{
				case SAVE_ASCII:
					WriteASCII(list, destination);
					break;

				case SAVE_V2X:
					WriteIFF(list, destination);
					break;

/*				case SAVE_TEXT:
					WriteText(list, destination);
					break;
*/
			}
		FreeEventList(list);
		FreeMem(list, sizeof(struct List));
	}
	if(success==TRUE && parameters==FALSE)
		MyRequest("OK", GetString(&li, MSG_SUCCESS), "");
	return success;
}
