/*
 *
 *  AM --- AmigaMail
 *  (C) 1991, 1992 by Christian Riede
 *
 *  AM is distributed in the hope that it will be useful, but WITHOUT ANY
 *  WARRANTY.  No author or distributor accepts responsibility to anyone
 *  for the consequences of using it or for whether it serves any
 *  particular purpose or works at all, unless he says so in writing.
 *  Refer to the GNU General Public License, Version 1, for full details.
 *  
 *  Everyone is granted permission to copy, modify and redistribute AM,
 *  but only under the conditions described in the GNU General Public
 *  License, Version 1.  A copy of this license is supposed to have been 
 *  given to you along with AM so you can know your rights and responsi-
 *  bilities.  It should be in a file named COPYING.  Among other things,
 *  the copyright notice and this notice must be preserved on all copies.
 *
 *  
 *
 */


#include "am.h"

/* entries in am.config and UULIB:Config*/

char NodeName[CONFIG_LENGTH+1];
char DomainName[CONFIG_LENGTH+1];
char MailEditor[CONFIG_LENGTH+1];
char RFilter[CONFIG_LENGTH+1];
char AMPubScreen[CONFIG_LENGTH+1]="\0";
int AMCTE=2; /* default is 8BIT */
char TimeZone[CONFIG_LENGTH+1]="GMT";
char ReplyTo[CONFIG_LENGTH+1]="";
ULONG AMDisplayID;
int LocalHOffset=0;
int LocalMOffset=0;
struct TextAttr AMFont;
UWORD AMColors[5]={4,0x0cb4,0x0000,0x0fff,0x0679}; /* first word is number of colors loaded (max 4)*/
static char FontName[CONFIG_LENGTH+1];
char Signature[CONFIG_LENGTH]="UULIB:.signature";
char AMFlags[CONFIG_LENGTH+1];

/* read interesting entries in UULIB:Config and in file denoted by s */
int ReadConfig(char *s)
{
	FILE *Config;
	static char InputBuffer[MAIL_FIELDLENGTH+1];
	static char Name[MAIL_FIELDLENGTH+1];
	static char Body[MAIL_FIELDLENGTH+1];
	int YSize,Style,Flags;
	int i;

	AMFont.ta_Name = (STRPTR) FontName;

	if (!(Config = fopen("UULIB:Config","r")))
		return(FALSE);

	while (fgets(InputBuffer,MAIL_FIELDLENGTH,Config))
	{
		if (InputBuffer[0]=='#') continue;           /* comment line */
		if (isspace((int)InputBuffer[0])) continue;  /* comment line */
		if (InputBuffer[0]=='\n') continue;          /* empty line */

		i=sscanf(InputBuffer,"%s %s",Name,Body);

		if (i!=2 && i!=1) continue;

		if (!strcmp(Name,"NodeName"))
			strncpy(NodeName,(i==2)?(Body):(""),CONFIG_LENGTH);

		else if (!strcmp(Name,"DomainName"))
			strncpy(DomainName,(i==2)?(Body):(""),CONFIG_LENGTH);

		else if (!strcmp(Name,"MailEditor"))
			strncpy(MailEditor,(i==2)?(Body):(""),CONFIG_LENGTH);

		else if (!strcmp(Name,"RFilter"))
			strncpy(RFilter,(i==2)?(Body):(""),CONFIG_LENGTH);

		else if (!strcmp(Name,"AMPubScreen"))
			strncpy(AMPubScreen,(i==2)?(Body):(""),CONFIG_LENGTH);

		else if (!strcmp(Name,"AMDisplayID"))
			sscanf(Body,"0x%lx",&AMDisplayID);

		else if (!strcmp(Name,"AMCTE"))
			sscanf(Body,"%d",&AMCTE);

		else if (!strcmp(Name,"Signature"))
			strncpy(Signature,(i==2)?(Body):(""),CONFIG_LENGTH);

		else if (!strcmp(Name,"TimeZone"))
		{
			strncpy(TimeZone,(i==2)?(Body):(""),CONFIG_LENGTH);
			ParseTimeZone(Body,&LocalHOffset,&LocalMOffset);
		}

		else if (!strcmp(Name,"ReplyTo"))
			strncpy(ReplyTo,(i==2)?(Body):(""),CONFIG_LENGTH);

		else if (!strcmp(Name,"AMFont"))
		{
			if (sscanf(InputBuffer,"%*s %s %d %d %d",AMFont.ta_Name,&YSize,&Style,&Flags)!=4)
			{
				*AMFont.ta_Name = 0;
				AMFont.ta_YSize = 0;
			}
			else
			{
				AMFont.ta_YSize = YSize;
				AMFont.ta_Style = Style;
				AMFont.ta_Flags = Flags;
			}
		}

		else if (!strcmp(Name,"AMFlags"))
			strncpy(AMFlags,(i==2)?(Body):(""),CONFIG_LENGTH);
	}

	fclose(Config);

	if (!s) return(TRUE);

	if (!(Config = fopen(s,"r")))
		return(FALSE);

	while (fgets(InputBuffer,MAIL_FIELDLENGTH,Config))
	{
		if (InputBuffer[0]=='#') continue;      /* comment line */
		if (isspace(InputBuffer[0])) continue;  /* comment line */
		if (InputBuffer[0]=='\n') continue;     /* empty line */

		i=sscanf(InputBuffer,"%s %s",Name,Body);

		if (i!=2 && i!=1) continue;

		if (!strcmp(Name,"MailEditor"))
			strncpy(MailEditor,(i==2)?(Body):(""),CONFIG_LENGTH);

		else if (!strcmp(Name,"RFilter"))
			strncpy(RFilter,(i==2)?(Body):(""),CONFIG_LENGTH);

		else if (!strcmp(Name,"AMPubScreen"))
			strncpy(AMPubScreen,(i==2)?(Body):(""),CONFIG_LENGTH);

		else if (!strcmp(Name,"AMDisplayID"))
			sscanf(Body,"0x%lx",&AMDisplayID);

		else if (!strcmp(Name,"AMCTE"))
			sscanf(Body,"%d",&AMCTE);

		else if (!strcmp(Name,"Signature"))
			strncpy(Signature,(i==2)?(Body):(""),CONFIG_LENGTH);

		else if (!strcmp(Name,"AMColors"))
			AMColors[0] = sscanf(InputBuffer,"%*s 0x%hx 0x%hx 0x%hx 0x%hx",
				AMColors+1,AMColors+2,AMColors+3,AMColors+4);

		else if (!strcmp(Name,"ReplyTo"))
			strncpy(ReplyTo,(i==2)?(Body):(""),CONFIG_LENGTH);

		else if (!strcmp(Name,"AMFont"))
		{
			if (sscanf(InputBuffer,"%*s %s %d %d %d",AMFont.ta_Name,&YSize,&Style,&Flags)!=4)
			{
				*AMFont.ta_Name = 0;
				AMFont.ta_YSize = 0;
			}
			else
			{
				AMFont.ta_YSize = YSize;
				AMFont.ta_Style = Style;
				AMFont.ta_Flags = Flags;
			}
		}

	}

	fclose(Config);
	
	return(TRUE);
}
