/* 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 <workbench/workbench.h>

#include <proto/dos.h>
#include <proto/exec.h>
#include <proto/icon.h>
#include <proto/ums.h>
#include <proto/utility.h>

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

#define CATCOMP_NUMBERS

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

extern BOOL FatalUMSError;
extern struct ControlData ControlData;
extern UMSAccount Account;

static const TEXT TextFalse[] = {"FALSE"};
static const TEXT TextNo[] = {"NO"};
static const TEXT TextTrue[] = {"TRUE"};
static const TEXT TextYes[] = {"YES"};

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

Prototype BOOL Amiga2Text(ULONG, STRPTR *, STRPTR *);
Prototype BOOL ReadBoolConfig(STRPTR, BOOL);
Prototype BOOL ReadBoolTool(struct DiskObject *, STRPTR, BOOL);
Prototype STRPTR CreateFullName(STRPTR, STRPTR);
Prototype STRPTR ReadConfig(STRPTR, STRPTR, ULONG);
Prototype STRPTR ReadTool(struct DiskObject *, STRPTR, STRPTR);
Prototype struct Conference *FindConference(ULONG);
Prototype __d0 UWORD SwapBytes(__d0 UWORD);
Prototype VOID QWKCpy(STRPTR, STRPTR, UWORD);
Prototype VOID REPCpy(STRPTR, STRPTR, UWORD);
Prototype VOID StripTrailing(STRPTR);

BOOL Amiga2Text(ULONG DateStamp, STRPTR *DateOut, STRPTR *TimeOut)
{
   struct ClockData date;
   static TEXT DateStore[9];
   static TEXT TimeStore[6];
   STRPTR TextDate = DateStore;
   STRPTR TextTime = TimeStore;

   Amiga2Date(DateStamp, &date);

   memset(TextDate, 0, 9);
   memset(TextTime, 0, 9);

   SPrintF(TextDate, "%d", date.month);
   TextDate++;
   if(*TextDate == 0)
   {
      *TextDate = *(TextDate - 1);
      *(TextDate - 1) = '0';
   }
   TextDate++;
   *TextDate = '-';

   TextDate++;
   SPrintF(TextDate, "%d", date.mday);
   TextDate++;
   if(*TextDate == 0)
   {
      *TextDate = *(TextDate - 1);
      *(TextDate - 1) = '0';
   }
   TextDate++;
   *TextDate = '-';

   TextDate++;
   date.year = date.year - 1900;
   while(date.year >= 100) date.year = date.year - 100;
   SPrintF(TextDate, "%d", date.year);
   TextDate++;
   if(*TextDate == 0)
   {
      *TextDate = *(TextDate - 1);
      *(TextDate - 1) = '0';
   }
   TextDate++;
   *TextDate = 0;

   SPrintF(TextTime, "%d", date.hour);
   TextTime++;
   if(*TextTime == 0)
   {
      *TextTime = *(TextTime - 1);
      *(TextTime - 1) = '0';
   }
   TextTime++;
   *TextTime = ':';

   TextTime++;
   SPrintF(TextTime, "%d", date.min);
   TextTime++;
   if(*TextTime == 0)
   {
      *TextTime = *(TextTime - 1);
      *(TextTime - 1) = '0';
   }
   TextTime++;
   *TextTime = 0;

   *DateOut = DateStore;
   *TimeOut = TimeStore;

   return(TRUE);
}

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

BOOL ReadBoolConfig(STRPTR Config, BOOL Default)
{
   BOOL out;
   STRPTR configVal;
   STRPTR textDefault;

   if(Default == TRUE)
   {
      textDefault = TextTrue;
   }
   else
   {
      textDefault = TextFalse;
   }

   configVal = ReadConfig(Config, textDefault, MISCMSG_BOOL_CONFIG);
   if(configVal == NULL) return(Default);

   if(Default == TRUE)
   {
      if(Stricmp(configVal, TextFalse) == 0 || Stricmp(configVal, TextNo) == 0)
      {
         out = FALSE;
      }
      else
      {
         out = TRUE;
      }
   }
   else
   {
      if(Stricmp(configVal, TextTrue) == 0 || Stricmp(configVal, TextYes) == 0)
      {
         out = TRUE;
      }
      else
      {
         out = FALSE;
      }
   }

   FreePoolVec(configVal);
   return(out);
}

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

BOOL ReadBoolTool(struct DiskObject *Obj, STRPTR Tool, BOOL Default)
{
   STRPTR toolVal;

   toolVal = FindToolType(Obj -> do_ToolTypes, Tool);
   if(toolVal == NULL) return(Default);

   if(*toolVal == 0) return(TRUE);

   if(Default == TRUE)
   {
      if(Stricmp(toolVal, TextFalse) == 0 || Stricmp(toolVal, TextNo) == 0)
      {
         return(FALSE);
      }
      else
      {
         return(TRUE);
      }
   }
   else
   {
      if(Stricmp(toolVal, TextTrue) == 0 || Stricmp(toolVal, TextYes) == 0)
      {
         return(TRUE);
      }
      else
      {
         return(FALSE);
      }
   }    

   return(TRUE);
}

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

STRPTR CreateFullName(STRPTR Path, STRPTR FileName)
{
   STRPTR Output_p;
   UWORD OutputLength = strlen(Path) + strlen(FileName) + 1;

   if(Path == NULL || FileName == NULL) return(NULL);

   if(*(Path + strlen(Path) - 1) == '/' || *(Path + strlen(Path) - 1) == ':')
   {
      Output_p = (STRPTR)AllocPoolVec(sizeof(TEXT) * OutputLength);
      if(Output_p == NULL) return(NULL);
      strcpy(Output_p, Path);
   }
   else
   {
      OutputLength++;
      Output_p = (STRPTR)AllocPoolVec(sizeof(TEXT) * OutputLength);
      if(Output_p == NULL) return(NULL);
      strcpy(Output_p, Path);
      strcat(Output_p, "/");
   }
   strcat(Output_p, FileName);

   return(Output_p);
}

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

STRPTR ReadConfig(STRPTR Config, STRPTR Default, ULONG For)
{
   STRPTR out;
   STRPTR work;
   struct TagItem tags[] = {
      UMSTAG_CfgName, (ULONG)Config,
      TAG_DONE, 0
   };
   UMSError errorNum;

   work = UMSReadConfig(Account, tags);
   if(work == NULL)
   {
		
      errorNum = UMSErrNum(Account);
      if(errorNum == UMSERR_NotFound)
		{
	      if(Default != NULL)
   	   {
      	   out = strdupPoolVec(Default);
				if(out == NULL)
				{
					HandleMemoryError(strlen(Default), For);
				}
	      }
   	   else
      	{
         	out = NULL;
	      }
		}
		else
		{
			HandleUMSError();
		}
   }
   else
   {
      out = strdupPoolVec(work);
		if(out == NULL)
		{
			HandleMemoryError(strlen(Default), For);
		}

      UMSFreeConfig(Account, work);
   }

   return(out);
}

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

STRPTR ReadTool(struct DiskObject *Obj, STRPTR Tool, STRPTR Default)
{
   STRPTR out;
   STRPTR work;

   work = FindToolType(Obj -> do_ToolTypes, Tool);
   if(work == NULL)
   {
      out = strdupPoolVec(Default);
   }
   else
   {
      out = strdupPoolVec(work);
   }

   return(out);
}

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

struct Conference *FindConference(ULONG ConfNum)
{
   struct Conference *conference = (struct Conference *)GetHead(&ControlData.ConfList);

   while(conference != NULL && conference -> ConfNumber != ConfNum) conference = (struct Conference *)GetSucc((struct Node *)conference);

   return(conference);
}

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

VOID StripTrailing(STRPTR String)
{
   STRPTR current;

   current = String + strlen(String) - 1;
   while((*current == ' ') && (current >= String)) current--;
   current++;
   *current = 0;

   return();
}
