##########################################################
# $Id: Makefile.conf,v 1.27 2004/09/10 08:12:39 cvs Exp $
### Only do this, if config/config.in doesn't exist

FILE_CFLAGS:=$(wildcard config/config.in)
ifeq ("$(FILE_CFLAGS)","")

A:=$(shell echo "Starting Check Section" >config/log 2>&1;echo "initialized")

### SOME MORE VARIABLES (PROGRAMS)


### pod

A:=$(warning "finding pod2html")
cfg_pod2html:=$(shell spod2html --version 2>>config/log)
ifeq ("$(cfg_pod2html)","")
	cfg_pod2html:=$(shell pod2html --help 2>>config/log)
	ifeq ("$(cfg_pod2html)","")
		A:=$(warning "NO POD2HTML or SPOD2HTML found, cannot generate documentation")
		cfg_pod2html:=echo NO POD2HTML 
		cfg_pod2htmlout:=
	else
		cfg_pod2html:=pod2html
		cfg_pod2htmlout:=--outfile
		ifneq ("$(POD_CSS)","") 
		  cfg_pod2html+= --css=$(POD_CSS)
		endif
	endif
else
	cfg_pod2html:=spod2html --recognize-comments
	cfg_pod2htmlout:=
	ifneq ("$(POD_CSS)","") 
		cfg_pod2html+= --stylesheet=$(POD_CSS)
	endif
endif


### Document uploading

WEB_DOC_PLACE?=hnmdijkema@shell.sf.net:/home/groups/e/el/elementals/htdocs
WEB_INDEX?=dummy

### DEFAULT VALUES

NEED_PTHREADS?=0
PTHREAD_CXX_EXCEPTIONS?=0
NEED_HARRAY?=0
NEED_RVECTOR?=0
NEED_STL_RVECTOR?=0
NEED_STD_RVECTOR?=0
NEED_HSTACK?=0
NEED_UTEST?=0
NEED_CRITSEC?=0
NEED_WXWIDGETS?=0
NEED_LTDL?=0
NEED_LIBPQ?=0
NEED_HOPTIONS?=0

### DEPENDENCIES

### rvector equals harray

ifneq ("$(NEED_RVECTOR)","0")
  ifneq ("$(NEED_HARRAY)","1")
	NEED_HARRAY:=$(NEED_RVECTOR)
  endif
endif

ifneq ("$(NEED_STL_RVECTOR)","0")
  ifneq ("$(NEED_HARRAY)","1")
  	NEED_HARRAY:=$(NEED_STL_RVECTOR)
  endif
endif

ifneq ("$(NEED_STD_RVECTOR)","0")
  ifneq ("$(NEED_HARRAY)","1")
	NEED_HARRAY:=$(NEED_STD_RVECTOR)
  endif
endif

ifneq ("$(NEED_HSTACK)","0")
  ifneq ("$(NEED_HARRAY)","1")
	NEED_HARRAY:=$(NEED_HSTACK)
  endif
endif

NEED_RVECTOR:=$(NEED_HARRAY)
NEED_STL_RVECTOR:=$(NEED_HARRAY)
NEED_STD_RVECTOR:=$(NEED_HARRAY)
NEED_HSTACK:=$(NEED_HARRAY)

### CONFIGURATION PART

### Checking


	A:=$(shell echo "CFG_CFLAGS=" >config/config.inn)
	A:=$(shell echo "CFG_LDFLAGS=" >>config/config.inn)
	A:=$(shell echo "CFG_LIBS=" >>config/config.inn)
	
	### Initializing
	
	CFL:=$(CFLAGS) $(LOCAL_CFLAGS)
	LDFL:=$(LDFLAGS) $(LOCAL_LDFLAGS)
	LBS:=$(LIBS) $(LOCAL_LIBS)
	
	### Threading	
	
	ifneq ("$(NEED_PTHREADS)","0")
		ifeq ("$(NEED_PTHREADS)","3")
			A:=$(error "You cannot use the check option (3) for NEED_PTHREADS")
		endif
		A:=$(warning "Checking PTHREADS (NEED_PTHREADS=$(NEED_PTHREADS))")
		ifeq ("$(OS)","WIN32")
			CFG_PTHREAD_LIB:=-lpthreadGC
		else
			CFG_PTHREAD_LIB:=-lpthread
		endif
		HAVE_PTHREADS:=$(shell $(CC) $(CFL) -o config/pthreads$(EXE) checks/pthreads.c $(LDFL) $(CFG_PTHREAD_LIB) >>config/log 2>&1;echo $$?)
		ifeq ("$(HAVE_PTHREADS)","0")
			A:=$(warning "HAVE_PTHREADS=true")
			A:=$(warning "PTHREAD_CXX_EXCEPTIONS=$(PTHREAD_CXX_EXCEPTIONS)")
			ifeq ("$(OS)","WIN32")
				ifeq ("$(PTHREAD_CXX_EXCEPTIONS)","1")
					A:=$(shell echo "CFG_LIBS+= -lpthreadGCE" >>config/config.inn)
					PTHREAD_LIB:="-lpthreadGCE"
				else
					A:=$(shell echo "CFG_LIBS+= -lpthreadGC" >>config/config.inn)
					PTHREAD_LIB:="-lpthreadGC"
				endif
			else
				A:=$(shell echo "CFG_LIBS+= -lpthread" >>config/config.inn)
					PTHREAD_LIB:="-lpthread"
			endif
			A:=$(shell echo "HAVE_PTHREADS:=1" >>config/config.inn)
		else
			A:=$(warning "HAVE_PTHREADS=false")
			ifeq ("$(NEED_PTHREADS)","1")
				E:=$(shell $(rm) config/config.inn)
				E:=$(error "I need posix threads (pthreads)")
			endif
		endif
	endif
	
	### harray
	
	ifneq ("$(NEED_HARRAY)","0")
		ifeq ("$(NEED_HARRAY)","3")
			A:=$(error "You cannot use the check option (3) for NEED_HARRAY")
		endif
		A:=$(warning "Checking HARRAY, HSTACK, STD::RVECTOR (NEED_HARRAY=$(NEED_HARRAY))")
		HAVE_HARRAY:=$(shell $(CXX) $(CFL) -o config/harray$(EXE) checks/harray.cxx $(LDFL) >>config/log 2>&1;echo $$?)
		ifneq ("$(HAVE_HARRAY)","0")
			A:=$(warning "HAVE_HARRAY=false")
			ifeq ("$(NEED_HARRAY)","1")
				E:=$(shell $(rm) config/config.inn)
				E:=$(error "I need harray.h")
			endif
		else
			A:=$(warning "HAVE_HARRAY=true")
 			A:=$(shell echo "HAVE_HARRAY:=1" >>config/config.inn)
 			A:=$(shell echo "HAVE_RVECTOR:=1" >>config/config.inn)
 			A:=$(shell echo "HAVE_STD_RVECTOR:=1" >>config/config.inn)
 			A:=$(shell echo "HAVE_HSTACK:=1" >>config/config.inn)
		endif
	endif
	
	### utest
	
	ifneq ("$(NEED_UTEST)","0")
		A:=$(warning "Checking UTEST (NEED_UTEST=$(NEED_UTEST))")
		HAVE_UTEST:=$(shell $(CXX) $(CFL) -o config/utest$(EXE) checks/utest.cxx $(LDFL) -lutest >>config/log 2>&1;echo $$?)
		ifneq ("$(HAVE_UTEST)","0")
			A:=$(warning "HAVE_UTEST=false")
			ifeq ("$(NEED_UTEST)","1")
				E:=$(shell $(rm) config/config.inn)
				E:=$(error "I need the utest C++ unit testing library")
			endif
		else
			A:=$(warning "HAVE_UTEST=true")
			ifneq ("$(NEED_UTEST)","3")
				A:=$(shell echo "CFG_LIBS+= -lutest" >>config/config.inn)
			endif
 			A:=$(shell echo "HAVE_UTEST:=1" >>config/config.inn)
			A:=$(shell echo "UTEST_LIBS=-lutest" >>config/config.inn)
			A:=$(shell echo "UTEST_CFLAGS=" >>config/config.inn)
			A:=$(shell echo "UTEST_LDFLAGS=" >>config/config.inn)
		endif
	endif

	### utest
	
	ifneq ("$(NEED_HOPTIONS)","0")
		A:=$(warning "Checking HOPTIONS (NEED_HOPTIONS=$(NEED_HOPTIONS))")
		HAVE_HOPTIONS:=$(shell $(CXX) $(CFL) -o config/hoptions$(EXE) checks/hoptions.cxx $(LDFL) -lhoptions >>config/log 2>&1;echo $$?)
		ifneq ("$(HAVE_HOPTIONS)","0")
			A:=$(warning "HAVE_HOPTIONS=false")
			ifeq ("$(NEED_HOPTIONS)","1")
				E:=$(shell $(rm) config/config.inn)
				E:=$(error "I need the HOPTIONS C++ options parsing library")
			endif
		else
			A:=$(warning "HAVE_HOPTIONS=true")
			ifneq ("$(NEED_HOPTIONS)","3")
				A:=$(shell echo "CFG_LIBS+= -lhoptions" >>config/config.inn)
			endif
 			A:=$(shell echo "HAVE_HOPTIONS:=1" >>config/config.inn)
			A:=$(shell echo "HOPTIONS_LIBS=-lhoptions" >>config/config.inn)
			A:=$(shell echo "HOPTIONS_CFLAGS=" >>config/config.inn)
			A:=$(shell echo "HOPTIONS_LDFLAGS=" >>config/config.inn)
		endif
	endif
	
	### critsec
	
	ifneq ("$(NEED_CRITSEC)","0")
		A:=$(warning "Checking CRITSEC (NEED_CRITSEC=$(NEED_CRITSEC))")
		HAVE_CRITSEC:=$(shell hcritsec_config --version)
		ifeq ("$(HAVE_CRITSEC)","")
			A:=$(warning "HAVE_CRITSEC=false")
			ifeq ("$(NEED_CRITSEC)","1")
				E:=$(shell $(rm) config/config.inn)
				E:=$(error "I need the critsec C++ CRITICAL SECTION library")
			endif
		else
			A:=$(warning "HAVE_CRITSEC=true")
			ifneq ("$(NEED_CRITSEC)","3")
				A:=$(shell LDS=`hcritsec_config --ldflags`;echo "CFG_LDFLAGS+=$$LDS" >>config/config.inn)
				A:=$(shell LIBS=`hcritsec_config --libs`;echo "CFG_LIBS+=$$LIBS" >>config/config.inn)
				A:=$(shell INCS=`hcritsec_config --cflags`;echo "CFG_CFLAGS+=$$CFLAGS" >>config/config.inn)
			endif
 			A:=$(shell echo "HAVE_CRITSEC:=1" >>config/config.inn)
			A:=$(shell LDS=`hcritsec_config --ldflags`;echo "CRITSEC_LDFLAGS=$$LDS" >>config/config.inn)
			A:=$(shell LIBS=`hcritsec_config --libs`;echo "CRITSEC_LIBS=$$LIBS" >>config/config.inn)
			A:=$(shell INCS=`hcritsec_config --cflags`;echo "CRITSEC_CFLAGS=$$CFLAGS" >>config/config.inn)
		endif
	endif
	
	### wx widgets
	
	ifneq ("$(NEED_WXWIDGETS)","0")
		A:=$(warning "Checking WXWIDGETS (NEED_WXWIDGETS=$(NEED_WXWIDGETS))")
		HAVE_WXWIDGETS:=$(shell wx-config --version)
		ifeq ("$(HAVE_WXWIDGETS)","") 
			A:=$(warning "HAVE_WXWIDGETS=false")
			ifeq ("$(NEED_WXWIDGETS"),"1")
				E:=$(shell $(rm) config/config.inn)
				E:=$(error "I need the wxWidgets libraries/headers")
			endif
		else
			A:=$(warning "HAVE_WXWIDGETS=true")
			ifneq ("$(NEED_WXWIDGETS)","3")
				A:=$(shell LDS=`wx-config --ldflags`;echo "CFG_LDFLAGS+=$$LDS" >>config/config.inn)
				A:=$(shell LIBS=`wx-config --libs`;echo "CFG_LIBS+=$$LIBS" >>config/config.inn)
				A:=$(shell INCS=`wx-config --cflags`;echo "CFG_CFLAGS+=$$CFLAGS" >>config/config.inn)
			endif
 			A:=$(shell echo "HAVE_WXWIDGETS:=1" >>config/config.inn)
			A:=$(shell LDS=`wx-config --ldflags`;echo "WXWIDGETS_LDFLAGS=$$LDS" >>config/config.inn)
			A:=$(shell LIBS=`wx-config --libs`;echo "WXWIDGETS_LIBS=$$LIBS" >>config/config.inn)
			A:=$(shell INCS=`wx-config --cflags`;echo "WXWIDGETS_CFLAGS=$$CFLAGS" >>config/config.inn)
		endif
	endif
	
	### libltdl
	
	ifneq ("$(NEED_LTDL)","0")
		A:=$(warning "Checking for libtool dynamic load library")
		HAVE_LTDL:=$(shell $(CC) $(CFL) -o config/ltdl$(EXE) checks/ltdl.c $(LDFL) -lltdl >>config/log 2>&1;echo $$?)
		ifeq ("$(HAVE_LTDL)","0")
			A:=$(warning "HAVE_LTDL=true")
			ifneq ("$(NEED_LTDL)","3")
				A:=$(shell echo "CFG_LIBS+= -lltdl" >>config/config.inn)
			endif
 			A:=$(shell echo "HAVE_LTDL:=1" >>config/config.inn)
			A:=$(shell echo "LTDL_LIBS=-lltdl" >>config/config.inn)
			A:=$(shell echo "LTDL_CFLAGS=" >>config/config.inn)
			A:=$(shell echo "LTDL_LDFLAGS=" >>config/config.inn)
		else
			A:=$(warning "HAVE_LTDL=false '$(NEED_LTDL)'")
			ifeq ("$(NEED_LTDL)","1")
				E:=$(shell $(rm) config/config.inn)
				E:=$(error "I need the libtool dynamic load libraries/headers")
			endif
		endif
	endif
	
	### libpq
	
	ifneq ("$(NEED_LIBPQ)","0")
		A:=$(warning "Checking for libpq (PostgreSQL C API)")
		HAVE_LIBPQ:=$(shell $(CC) $(CFL) -o config/libpq$(EXE) checks/libpq.c $(LDFL) -lpq >>config/log 2>&1;echo $$?)
		ifeq ("$(HAVE_LIBPQ)","0")
			A:=$(warning "HAVE_LIBPQ=true")
			ifneq ("$(NEED_LIBPQ)","3")
				A:=$(shell echo "CFG_LIBS+= -lpq" >>config/config.inn)
 			endif
			A:=$(shell echo "HAVE_LIBPQ:=1" >>config/config.inn)
			A:=$(shell echo "LIBPQ_LIBS=-lpq" >>config/config.inn)
			A:=$(shell echo "LIBPQ_CFLAGS=" >>config/config.inn)
			A:=$(shell echo "LIBPQ_LDFLAGS=" >>config/config.inn)
		else
			A:=$(warning "HAVE_LIBPQ=false '$(NEED_LIBPQ)'")
			ifeq ("$(NEED_LIBPQ)","1")
				E:=$(shell $(rm) config/config.inn)
				E:=$(error "I need libpq (PostgreSQL C API)")
			endif
		endif
	endif

	# Make config.in from config.inn

	A:=$(warning "Unifying configured CFLAGS, LDFLAGS and LIBS")
	A:=$(shell echo "pod2html=$(cfg_pod2html)" >>config/config.inn)
	A:=$(shell echo "pod2htmlout=$(cfg_pod2htmlout)" >>config/config.inn)
	A:=$(shell echo "WEB_DOC_PLACE=$(WEB_DOC_PLACE)" >>config/config.inn)
	A:=$(shell echo "WEB_INDEX=$(WEB_INDEX)" >>config/config.inn)
	A:=$(shell echo "PACKAGE=$(PACKAGE)" >>config/config.inn)	
	A:=$(shell echo "VERSION=$(VERSION)" >>config/config.inn)	
	A:=$(shell echo "EXE=$(EXE)" >>config/config.inn)	
	A:=$(shell echo "OBJ=$(OBJ)" >>config/config.inn)	
	A:=$(shell echo "LIB=$(LIB)" >>config/config.inn)	
	A:=$(shell echo "SHL=$(SHL)" >>config/config.inn)	
	A:=$(shell echo "CC=$(CC)" >>config/config.inn)
	A:=$(shell echo "LD=$(LD)" >>config/config.inn)
	A:=$(shell echo "cat=$(cat)" >>config/config.inn)
	A:=$(shell echo "grep=$(grep)" >>config/config.inn)
	A:=$(shell echo "sed=$(sed)" >>config/config.inn)
	A:=$(shell echo "OS_CONFIG=$(OS_CONFIG)" >>config/config.inn)
	A:=$(shell echo "CFG_CFLAGS+=$(LOCAL_CFLAGS)" >>config/config.inn)
	A:=$(shell echo "CFG_LDFLAGS+=$(LOCAL_LDFLAGS)" >>config/config.inn)
	A:=$(shell echo "CFG_LIBS+=$(LOCAL_LIBS)" >>config/config.inn)
	A:=$(shell echo "PTHREAD_LIB=$(PTHREAD_LIB)" >>config/config.inn)
	ifneq ("$(PREFIX)","") 
		A:=$(shell echo "PREFIX=$(PREFIX)" >>config/config.inn)
	endif
	A:=$(shell if [ ! -r pconf_unify.c ]; then $(sed) -e 's%@VERSION@%$(VERSION)%' <pconf_unify.c.in >pconf_unify.c; fi)
	A:=$(shell make -f Makefile.unify)

endif

include config/config.in
