
/* OpenWebSpider
 *
 *  Authors:     Stefano Alimonti AND Stefano Fantin
 *  Version:     0.8
 *  E-Mails:     shen139 [at] openwebspider (dot) org AND stefanofantinguz@yahoo.it
 *
 *
 * This file is part of OpenWebSpider
 *
 * 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
 *
 */

int ReadConfFile(char* confPath)
{
FILE* fConf;
char sLine[120];
int iLine=0;
char key[MAXCONFKEYSIZE];
char argument[MAXCONFARGSIZE];
char* tmpP;
unsigned int KeyNArgAreOK;


	DB1[0]=0;
	DB2[0]=0;
	MYSQLSERVER1[0]=0;
	MYSQLSERVER2[0]=0;
	USERDB1[0]=0;
	USERDB2[0]=0;
	PASSDB1[0]=0;
	PASSDB2[0]=0;
	OWS_SERVER_PASSWORD[0]=0;

	if(confPath[0]==0)
		strcpy(confPath,"openwebspider.conf");

	printf("+ Trying to load %s...",confPath);
	fConf=fopen(confPath,"r");
	if(fConf==NULL)
	{
		printf("file not found\n\n");
		return 0;
	}
	printf("OK\n");

	while(!feof(fConf))
	{
		memset(sLine,0,sizeof(sLine));
		
		if(fgets(sLine,100,fConf)==NULL)
			continue;

		iLine++;

		if(sLine[0]=='#' || sLine[0]=='\r' || sLine[0]=='\n' || sLine[0]==0)
			continue;


		KeyNArgAreOK = 0;	/*false*/

		tmpP=strchr( strtrim(sLine, sLine) ,'=');

		if( tmpP && tmpP-sLine<MAXCONFKEYSIZE && tmpP-sLine>1 )
		{
			memset(key,0,MAXCONFKEYSIZE);

			strncpy(key,sLine,MIN(tmpP-sLine,MAXCONFKEYSIZE) );

			strtrim(key,key);

			if( strlen(key)>1 && sLine+strlen(sLine) - tmpP - 1 < MAXCONFARGSIZE )
			{
				strcpy(argument, tmpP +1);
				strtrim(argument,argument);

				ReplaceChr(argument,'\r',0);
				ReplaceChr(argument,'\n',0);

				if(strlen(argument)>=1)
					KeyNArgAreOK = 1;
			}
		}
		
		if(KeyNArgAreOK == 0)
		{
			printf("Error while parsing %s (Line: %i)\n",confPath,iLine);
			
			fclose(fConf);

		return 0;
		}
		
		if(stricmp(key,"mysqlserver1")==0)
		{
			strcpy(MYSQLSERVER1,argument);
			printf(" - Server1: %s\n",MYSQLSERVER1);
		}
		else
		if(stricmp(key,"mysqlserver2")==0)
		{
			strcpy(MYSQLSERVER2,argument);
			printf(" - Server2: %s\n",MYSQLSERVER2);
		}
		else
        if(stricmp(key,"port1")==0)
		{
			MYSQLSERVER_PORT1 = atoi( strtrim(argument,argument) );
			printf(" - Server1 Port: %i\n",MYSQLSERVER_PORT1);
		}
		else
        if(stricmp(key,"port2")==0)
		{
			MYSQLSERVER_PORT2 = atoi( strtrim(argument,argument) );
			printf(" - Server2 Port: %i\n",MYSQLSERVER_PORT2);
		}
		else
		if(stricmp(key,"db1")==0)
		{
			strcpy(DB1,argument);
			printf(" - Database1: %s\n",DB1);
		}
		else
		if(stricmp(key,"db2")==0)
		{
			strcpy(DB2,argument);
			printf(" - Database2: %s\n",DB2);
		}
		else
		if(stricmp(key,"userdb1")==0)
		{
			strcpy(USERDB1,argument);
			printf(" - Username DB1: %s\n",USERDB1);

		}
		else
		if(stricmp(key,"userdb2")==0)
		{
			strcpy(USERDB2,argument);
			printf(" - Username DB2: %s\n",USERDB2);
		}
		else
		if(stricmp(key,"passdb1")==0)
		{
			strcpy(PASSDB1,argument);
			printf(" - Password DB1: *****\n");
		}
		else
		if(stricmp(key,"passdb2")==0)
		{
			strcpy(PASSDB2,argument);
			printf(" - Password DB2: *****\n");
		}
		else
		if(stricmp(key,"ows_server_password")==0)
		{
			strcpy(OWS_SERVER_PASSWORD,argument);
			printf(" - OWS Server Password: *****\n");
		}
		else
		{
			printf("Error while parsing %s (Line: %i)\n",confPath,iLine);
			
			fclose(fConf);
			return 0;
		}
	}

	if(DB1[0]==0 ||	DB2[0]==0 || MYSQLSERVER1[0]==0 || MYSQLSERVER2[0]==0 || USERDB1[0]==0 || USERDB2[0]==0 )
	{
		fclose(fConf);
		
		printf("  - Field in %s missing\n\n",confPath);

		return 0;
	}

	fclose(fConf);
return 1;
}
