#ifndef CODE_RESOURCE_CXX
#define CODE_RESOURCE_CXX

/*-----------------------------------------------------------------------
 * Xclasses code file
 * ($Id: resource.cxx,v 1.1.1.1 1998/07/16 19:29:27 cvs Exp $)
 * 
 * Copyright (C) 1998 Jürgen Schmitz
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA  02111-1307, USA.
 *-----------------------------------------------------------------------
 */

#include <unistd.h>
#include <pwd.h>
#include <sys/types.h>

#include "types.h"
#include "../include/defaults.h"
#include <Xclasses/Xdisplay.h>
#include <Xclasses/Xclasses_class.h>
#include "../include/XCprogram_p.h"

static XrmDatabase resBase;
static XrmDatabase resCommand;
static XrmDatabase runBase;

void initResource(class Xdisplay *d)
{
	if (!getenv("HOME"))
	{
		uid_t u=getuid();
		struct passwd *pw=getpwuid(u);
		char buf[strlen(pw->pw_dir)+6];
		strcpy(buf,"HOME=");
		strcat(buf,pw->pw_dir);
		putenv(buf);
		warn_printf("This is not a posix conforming system,\nenvironement HOME is missing and set by Xclasses now!",d);
	}
	if (!getenv("USER"))
	{
		uid_t u=getuid();
		struct passwd *pw=getpwuid(u);
		char buf[strlen(pw->pw_name)+6];
		strcpy(buf,"USER=");
		strcat(buf,pw->pw_name);
		putenv(buf);
		warn_printf("This is not a posix conforming system,\nenvironement USER is missing and set by Xclasses now!",d);
	}

	XrmDatabase homeDB, serverDB, appDB, internalDB;
	char *env;
	char name[400];

	if (thisProgram && thisProgram->resources)
	{
		internalDB=XrmGetStringDatabase(thisProgram->resources);
		XrmMergeDatabases(internalDB,&resBase);
	}

	strcpy(name,"/usr/lib/X11/app-defaults/");
	strcat(name,prgname);
	appDB=XrmGetFileDatabase(name);
	XrmMergeDatabases(appDB,&resBase);

	strcpy(name,GLOBAL_CONFIGS);
	strcat(name,"Xresource_");
	strcat(name,prgname);
	appDB=XrmGetFileDatabase(name);
	XrmMergeDatabases(appDB,&resBase);

	{
		char *h;
		if (h=getenv("HOME"))
		{
			strcpy(name,h);
			strcat(name,"/.Xdefaults");
			serverDB=XrmGetFileDatabase(name);
			XrmMergeDatabases(serverDB,&resBase);
		}
	}
	if (XResourceManagerString(d->display()))
	{
		serverDB=XrmGetStringDatabase(XResourceManagerString(d->display()));
		XrmMergeDatabases(serverDB,&resBase);
	}
	
	if ((env=getenv("XENVIRONMENT"))==NULL)
	{
		char *h;
		if (h=getenv("HOME"))
		{
			strcpy(name,h);
			strcat(name,"/.Xdefaults-");
			strncpy(name,xc_gethostname(),sizeof(name));
			name[sizeof(name)-1]=0;
			env=name;
		}
	}
	homeDB=XrmGetFileDatabase(env);
	XrmMergeDatabases(homeDB,&resBase);	

	{
		char *h;
		if (h=getenv("HOME"))
		{
			strcpy(name,h);
			strcat(name,"/.Xresources_Xclasses");
			runBase=XrmGetFileDatabase(name);
			XrmMergeDatabases(runBase,&resBase);
		}
	}

	XrmMergeDatabases(resCommand,&resBase);	
}
static int opTableEntries=1;
static XrmOptionDescRec opTable[]={
{"-xrm",		NULL,			XrmoptionResArg,	(caddr_t)NULL},
};
void parseArgv(int &argc, char *argv[])
{
	int i=1;
	while (i<argc)
	{
		if (!strcasecmp(argv[i],"-xrfile") || !strcasecmp(argv[i],"-xrf"))
		{
			remarg(argc,argv,i);
			argc--;
			if (argc>1)
			{
				XrmDatabase fileDB;
				fileDB=XrmGetFileDatabase(argv[i]);
				XrmMergeDatabases(fileDB,&resBase);
				remarg(argc,argv,i);
				argc--;
			}
		}
		else
		{
			i++;
		}
	}
	XrmParseCommand(&resCommand,opTable,opTableEntries,prgname,&argc,argv);
}
char *getResource(char *name,char *name2)
{
	static char buf[1000];
	char *r=NULL;
	XrmValue ret;
	char *str_type[20];

	if (!name2)
	{
		strcpy(buf,"Xclasses.");
		strcat(buf,name);
		name2=buf;
	}

	if (XrmGetResource(resBase,name,name2,str_type,&ret)==True)
	{
		r=buf;
		strncpy(buf,ret.addr,ret.size);
		buf[ret.size]=0;
	}
	return r;
}
void setResource(char *name,char *val)
{
	XrmPutStringResource(&resBase,name,val);
	XrmPutStringResource(&runBase,name,val);
}

void writeObjectDatabase(void)
{
	char *h;
	if (defaults.writeresbase && (h=getenv("HOME")))
	{
		char name[500];
		strcpy(name,h);
		strcat(name,"/.Xresources_Xclasses");
		writeObjectDatabase(name);
	}
}
void writeObjectDatabase(char *filename)
{
	XrmPutFileDatabase(runBase,filename);
}

#endif
