/* PR-QWK - QWK Importer for UMS
 * Copyright (C) 1998 J.Ross Nicoll
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

#include <dos/dostags.h>
#include <exec/libraries.h>
#include <exec/memory.h>
#include <exec/types.h>
#include <intuition/intuition.h>
#include <utility/tagitem.h>
#include <utility/utility.h>
#include <workbench/startup.h>

#include <proto/dos.h>
#include <proto/ums.h>

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

#define CATCOMP_NUMBERS

#include "locale.h"
#include "pr-qwk.h"

/* Config data */
BOOL ShowDebug;
BOOL Keep;
BOOL MailBulletins;
BOOL MailGoodbye;
BOOL MailNews;
BOOL MailNewFiles;
BOOL MailSession;
BOOL MailWelcome;
BOOL NoExport;
BOOL NoImport;
BOOL ShowBulletins;
BOOL ShowGoodbye;
BOOL ShowNews;
BOOL ShowNewFiles;
BOOL ShowSession;
BOOL ShowWelcome;
BPTR InboundDirLock = NULL;
BPTR TempDirLock = NULL;
STRPTR ArcCmd;
STRPTR ArchiveFilename = NULL;
STRPTR BaseGroup;
STRPTR DearcCmd;
STRPTR Inbound;
STRPTR MailFolder;
STRPTR MailTo = NULL;
STRPTR Outbound;
STRPTR PacketName;
STRPTR Password;
STRPTR Server;
STRPTR Site;
STRPTR TempDir;
STRPTR User;
ULONG BaseGroupLen;
ULONG BufferSize;

/* DOS variables */
BPTR   InputHandle;
BPTR   OldOutputHandle = NULL;
BPTR   OutputHandle = NULL;
struct FileInfoBlock *FileInfoBlock = NULL;
struct TagItem ioSystemTags[3] =
{
   SYS_Input, 0,
   SYS_Output, 0,
   TAG_DONE, NULL
};

/* Exec variables */
APTR   MainPool = NULL;
struct Library *AsyncIOBase = NULL;
extern struct DOSBase *DOSBase;
struct Library *IconBase = NULL;
struct Library *IntuitionBase = NULL;
struct Library *LocaleBase = NULL;
extern struct Library *SysBase;
struct Library *UMSBase = NULL;
struct Library *UtilityBase = NULL;
struct WBStartup *WBStartup;

/* Internal data */
STRPTR SiteAddr;
struct ControlData ControlData;
struct EasyStruct EasyStruct;
struct TagItem BlankTag = {TAG_DONE, NULL};
TEXT FormatBuffer[FORMAT_BUFFER_SIZE];
ULONG IDCMPFlags = NULL;

/* Locale variables */
struct Catalog *Catalog = NULL;
struct Locale *Locale = NULL;

/* Permanent stuff */
const TEXT Blank[128] = 0;
const TEXT Text_Version[] = {"$VER: PR-QWK 0.4 (29.07.98)"};

/* UMS variables */
BOOL FatalUMSError = FALSE;
UMSAccount Account = NULL;

#define Prototype extern
#include "PR-QWK-protos.h"

Prototype BOOL Main(void);
Prototype int main(int argc, char *argv);
Prototype int wbmain(struct WBStartup *wbs);

int main(int argc, char *argv)
{
   if(CLIinit() == FALSE)
   {
      return(10);
   }

   if(Main() == FALSE)
   {
      Cleanup();
      return(20);
   }

   CLICleanup();
   return(0);
}

/* ----------------------------------------------------------------------- */

int wbmain(struct WBStartup *wbs)
{
   WBStartup = wbs;

   if(WBInit() == FALSE)
   {
      WBCleanup();
      return(10);
   }

   if(Main() == FALSE)
   {
      WBCleanup();
      return(20);
   }

   WBCleanup();
   return(0);
}

/* ----------------------------------------------------------------------- */

BOOL Main(void)
{
   BPTR currentDir;

   SPrintF(FormatBuffer, GetString(INFOMSG_LOGGING_IN), User, Server);
   PutStr(FormatBuffer);
   Account = UMSRLogin(Server, User, Password);
   if(Account != NULL)
	{
	   if(GetConfig() != FALSE)
		{
	   	if(CheckConfig() != FALSE)
			{
			   currentDir = CurrentDir(TempDirLock);
   			if(NoImport == 0 && FatalUMSError == FALSE)
			   {
   			   if(Import() == FALSE) Keep = 1;
		   	}

		   	if(NoExport == 0 && FatalUMSError == FALSE)
			   {
   			   if(Export() == FALSE) Keep = 1;
			   }

   			if(Examine(TempDirLock, FileInfoBlock) != FALSE)
	   		{
	   		   while(ExNext(TempDirLock, FileInfoBlock) == DOSTRUE)
	   	   	{
		   	      if(FileInfoBlock -> fib_DirEntryType < 0) DeleteFile(FileInfoBlock -> fib_FileName);
	   		   }
		   	}
	   		CurrentDir(currentDir);

			   return(TRUE);
			}
		}
	}

	return(FALSE);
}
