###########################################################################
#
# Makefile -- runtime-src/src/recpt
# A component of the Greenstone digital library software
# from the New Zealand Digital Library Project at the
# University of Waikato, New Zealand.
#
# Copyright (C) 1999-2008  The New Zealand Digital Library Project
#
#
# 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., 675 Mass Ave, Cambridge, MA 02139, USA.
#
###########################################################################

GSDL_DIR = ../../..
COMMON_DIR = $(GSDL_DIR)/common-src
RUNTIME_DIR = $(GSDL_DIR)/runtime-src

GSDLOS = @gsdlos@
GSDLARCH = @gsdlarch@

INDEXERS_DIR = $(COMMON_DIR)/indexers
COMMON_PACKAGES_DIR = $(COMMON_DIR)/packages
RUNTIME_PACKAGES_DIR = $(RUNTIME_DIR)/packages
COLSERVR_DIR = $(RUNTIME_DIR)/src/colservr
PROTOCOL_DIR = $(RUNTIME_DIR)/src/protocol
Z3950_DIR = $(RUNTIME_DIR)/src/z3950


ENABLE_ACCENTFOLD = @ENABLE_ACCENTFOLD@
ifeq ($(ENABLE_ACCENTFOLD), 1)
ifeq ($(GSDLOS),darwin)
ACCENTFOLD_LIBS = $(INDEXERS_DIR)/packages/unac/libunac.a -liconv
else
ACCENTFOLD_LIBS = $(INDEXERS_DIR)/packages/unac/libunac.a $(COMMON_PACKAGES_DIR)/iconv/lib/libiconv.a
endif
else
ACCENTFOLD_LIBS = 
endif

ENABLE_MG = @ENABLE_MG@
ifeq ($(ENABLE_MG), 1)
MG_INDEXERS_LIBS = $(INDEXERS_DIR)/mg/src/text/libmgtext.a \
	$(INDEXERS_DIR)/mg/lib/libmglib.a
MG_INCLUDES = -I$(INDEXERS_DIR)/mg -I$(INDEXERS_DIR)/mg/lib
else
MG_INDEXERS_LIBS =
MG_INCLUDES =
endif

ENABLE_MGPP = @ENABLE_MGPP@
ifeq ($(ENABLE_MGPP), 1)
MGPP_SOURCES = phindaction.cpp
MGPP_OBJECTS = phindaction.o
MGPP_INDEXERS_LIBS = $(INDEXERS_DIR)/mgpp/text/libmgpptext.a \
	$(INDEXERS_DIR)/mgpp/lib/libmgpplib.a
MGPP_INCLUDES = -I$(INDEXERS_DIR)/mgpp/text -I$(INDEXERS_DIR)/mgpp/lib
else
MGPP_SOURCES =
MGPP_OBJECTS =
MGPP_INDEXERS_LIBS =
MGPP_INCLUDES =
endif

USE_FASTCGI = @USE_FASTCGI@
ifeq ($(USE_FASTCGI), 1)
FASTCGI_DEFS = -DUSE_FASTCGI
FASTCGI_INCLUDES = -I$(RUNTIME_PACKAGES_DIR)/fcgi/include
FASTCGI_LIBS = $(RUNTIME_PACKAGES_DIR)/fcgi/libfcgi/libfcgi.a
else
FASTCGI_DEFS =
FASTCGI_INCLUDES =
FASTCGI_LIBS = 
endif

USE_GDBM = @USE_GDBM@
ifeq ($(USE_GDBM), 1)
GDBM_DEFINES = -DUSE_GDBM
GDBM_INCLUDES = -I$(COMMON_PACKAGES_DIR)/gdbm/include
GDBM_LIBS = $(COMMON_PACKAGES_DIR)/gdbm/lib/libgdbm.a
else
GDBM_DEFINES = 
GDBM_INCLUDES = 
GDBM_LIBS = 
endif

USE_JDBM = @USE_JDBM@
ifeq ($(USE_JDBM), 1)
JDBM_DEFINES = -DUSE_JDBM
else
JDBM_DEFINES = 
endif

USE_SQLITE = @USE_SQLITE@
ifeq ($(USE_SQLITE), 1)
SQLITE_DEFINES = -DUSE_SQLITE
SQLITE_LIBS = $(COMMON_PACKAGES_DIR)/sqlite/lib/libsqlite3.a -ldl -lpthread
SQL_SOURCES = sqlqueryaction.cpp
SQL_OBJECTS = sqlqueryaction.o
else
SQLITE_DEFINES =
SQLITE_LIBS = 
SQL_SOURCES =
SQL_OBJECTS =
endif

USE_RSS = @USE_RSS@
ifeq ($(USE_RSS), 1)
RSS_DEFINES = -DUSE_RSS
RSS_SOURCES = rssaction.cpp
RSS_OBJECTS = rssaction.o
else
RSS_DEFINES =
RSS_SOURCES =
RSS_OBJECTS =
endif


USE_Z3950 = @USE_Z3950@
ifeq ($(USE_Z3950), 1)
Z3950_DEFS = -DUSE_Z3950
Z3950_INCLUDES = -I$(Z3950_DIR)
# Use GNU specific shell command to capture output.
# Need LIBS to contain all the dependencies for libyaz that yaz had worked out (from yaz's Makefile)
# http://stackoverflow.com/questions/2019989/how-to-assign-the-output-of-a-command-to-a-makefile-variable
YAZ_LIBS = $(shell egrep "^LIBS\s?=" $(RUNTIME_PACKAGES_DIR)/yaz/yaz-2.1.56/Makefile | sed 's/^LIBS = //')
Z3950_LIBS = $(Z3950_DIR)/gsdlz3950.a $(RUNTIME_PACKAGES_DIR)/yaz/lib/libyaz.a $(YAZ_LIBS)
else
Z3950_DEFS =
Z3950_INCLUDES = 
Z3950_LIBS = 
endif

USE_APACHE_HTTPD=@USE_APACHE_HTTPD@
#ifeq ($(USE_APACHE_HTTPD), 1)
ifeq ($(USE_APACHE_HTTPD), "IGNORE_FOR_NOW")
APACHE_OBJS = string_pool.o mod_gsdl.o

APACHE_INSTDIR=$(GSDL_DIR)/apache-httpd/$(GSDLOS)
APACHE_SRCLIBDIR=../../packages/apache-httpd/httpd-2.2.11/srclib

APXS_CFLAGS=-I$(APACHE_INSTDIR)/include \
  -I$(APACHE_SRCLIBDIR)/apr/include -I$(APACHE_SRCLIBDIR)/apr-util/include

APXS_LDFLAGS= -L$(APACHE_INSTDIR)/lib \
  -L$(APACHE_SRCLIBDIR)/apr/include -L$(APACHE_SRCLIBDIR)/apr-util/include

APACHE_MODULE = mod_gsdl.so
else
APACHE_OBJS =
APACHE_MODULE =
endif


# Extension support
EXT_DEFINES =
EXT_INCLUDES =
EXT_LDFLAGS =
EXT_LIBS =



AR = ar
CC = @CC@
CFLAGS=@CFLAGS@ @ARCH_DEP_FLAGS@
CXX = @CXX@
CXXFLAGS = @CXXFLAGS@ @ARCH_DEP_FLAGS@
DEFS = @DEFS@ -DNZDL -DQUIET -DSHORT_SUFFIX -DPARADOCNUM -DHAVE_CONFIG_H $(FASTCGI_DEFS) $(RSS_DEFINES) $(GDBM_DEFINES) $(JDBM_DEFINES) $(SQLITE_DEFINES) $(Z3950_DEFS) $(EXT_DEFINES)
INCLUDES = -I. -I$(GSDL_DIR) -I$(COMMON_DIR)/src/lib \
        -I$(COLSERVR_DIR) -I$(PROTOCOL_DIR) \
	$(MG_INCLUDES) $(MGPP_INCLUDES) \
        -I$(COMMON_PACKAGES_DIR)/expat/include $(FASTCGI_INCLUDES) $(GDBM_INCLUDES) $(Z3950_INCLUDES) $(EXT_INCLUDES)
INSTALL = @INSTALL@
LDFLAGS = @LDFLAGS@ @ARCH_DEP_FLAGS@ $(EXT_LDFLAGS)
LIBS = @LIBS@ $(GDBM_LIBS) \
	$(COMMON_PACKAGES_DIR)/expat/lib/libexpat.a \
	$(ACCENTFOLD_LIBS) $(FASTCGI_LIBS) $(SQLITE_LIBS) $(Z3950_LIBS) $(EXT_LIBS)
RANLIB = @RANLIB@


CXXCOMPILE = $(CXX) -c $(CXXFLAGS) $(DEFS) $(INCLUDES)
CXXLINK = $(CXX) $(LDFLAGS) -o $@


SOURCES = \
	action.cpp \
	argdb.cpp \
	authenaction.cpp \
	browseaction.cpp \
	browseactiontools.cpp \
	browserclass.cpp \
	browsetoolsclass.cpp \
	cgiargs.cpp \
	cgiutils.cpp \
	cgiwrapper.cpp \
	collectoraction.cpp \
	configaction.cpp \
        converter.cpp \
	datelistbrowserclass.cpp \
	depositoraction.cpp \
	documentaction.cpp \
	dynamicclassifieraction.cpp \
	extlinkaction.cpp \
	formattools.cpp \
	formatconverter.cpp \
	gtiaction.cpp \
	highlighttext.cpp \
	historydb.cpp \
	hlistbrowserclass.cpp \
	htmlbrowserclass.cpp \
	htmlutils.cpp \
	invbrowserclass.cpp \
	librarymain.cpp \
	maincfg.cpp \
	pageaction.cpp \
	pagedbrowserclass.cpp \
	$(MGPP_SOURCES) \
	phindbrowserclass.cpp \
	pingaction.cpp \
	ispersistentaction.cpp \
	basequeryaction.cpp \
	queryaction.cpp \
	$(SQL_SOURCES) \
	querytools.cpp \
	receptionist.cpp \
	$(RSS_SOURCES) \
	statusaction.cpp \
	securitytools.cpp \
	summarise.cpp \
	tipaction.cpp \
	userdb.cpp \
	usersaction.cpp \
	vlistbrowserclass.cpp \
	wizardaction.cpp

OBJECTS = \
	action.o \
	argdb.o \
	authenaction.o \
	browseaction.o \
	browseactiontools.o \
	browserclass.o \
	browsetoolsclass.o \
	cgiargs.o \
	cgiutils.o \
	cgiwrapper.o \
	collectoraction.o \
	configaction.o \
        converter.o \
	datelistbrowserclass.o \
	depositoraction.o \
	documentaction.o \
	dynamicclassifieraction.o \
	extlinkaction.o \
	formattools.o \
	gtiaction.o \
	highlighttext.o \
	historydb.o \
	hlistbrowserclass.o \
	htmlbrowserclass.o \
	htmlutils.o \
	invbrowserclass.o \
	librarymain.o \
	maincfg.o \
	pageaction.o \
	pagedbrowserclass.o \
	$(MGPP_OBJECTS) \
	phindbrowserclass.o \
	pingaction.o \
	ispersistentaction.o \
	basequeryaction.o \
	queryaction.o \
	$(SQL_OBJECTS) \
	querytools.o \
	receptionist.o \
	$(RSS_OBJECTS) \
	statusaction.o \
	securitytools.o \
	summarise.o \
	tipaction.o \
	userdb.o \
	usersaction.o \
	vlistbrowserclass.o \
	wizardaction.o 

CONVERTER_OBJECTS = \
	cgiargs.o \
	cgiutils.o \
	formattools.o \
	formatconverter.o \
	summarise.o

LIBRARY = gsdlrecpt.a

GSDL_LIBS = \
	$(PROTOCOL_DIR)/gsdlprotocol.a \
	$(COLSERVR_DIR)/gsdlcolservr.a \
	$(MG_INDEXERS_LIBS) \
	$(MGPP_INDEXERS_LIBS) \
	$(COMMON_DIR)/src/lib/gsdllib.a

EXECUTABLE_OBJECTS = \
	$(OBJECTS) \
	$(GSDL_LIBS)

EXECUTABLE = library.cgi


# Default target: make both gsdlrecpt.a and the library executable
all: $(LIBRARY) $(EXECUTABLE) $(APACHE_MODULE) formatconverter

formatconverter: $(CONVERTER_OBJECTS)
	$(CXXLINK) $(CONVERTER_OBJECTS) $(COMMON_DIR)/src/lib/gsdllib.a $(PROTOCOL_DIR)/gsdlprotocol.a $(COLSERVR_DIR)/gsdlcolservr.a

gsdlrecpt.a: $(OBJECTS)
	rm -f $(LIBRARY)
	$(AR) cru $(LIBRARY) $(OBJECTS)
	$(RANLIB) $(LIBRARY)

$(EXECUTABLE): $(EXECUTABLE_OBJECTS) $(GSDL_LIBS)
	$(CXXLINK) $(EXECUTABLE_OBJECTS) $(LIBS)

mod_gsdl.o: mod_gsdl.cpp
	$(CXX) -c  $(CXXFLAGS) $(DEFS) $(INCLUDES) -D_APACHE_MOD=2 -fPIC $(APXS_CFLAGS) -Wall -o $@ $< 

mod_gsdl.so: $(APACHE_OBJS) $(LIBRARY)
	$(CXX) -fPIC -shared -o $@ $(APACHE_OBJS) $(LIBRARY) $(GSDL_LIBS) $(LIBS)


.SUFFIXES:
.SUFFIXES: .cpp .o
.cpp.o:
	$(CXXCOMPILE) $< 


install: $(EXECUTABLE)
	$(INSTALL) -m 755 -s $(EXECUTABLE) $(GSDL_DIR)/cgi-bin/$(GSDLOS)$(GSDLARCH)
#ifeq ($(USE_APACHE_HTTPD), 1)
ifeq ($(USE_APACHE_HTTPD), "IGNORE_FOR_NOW")
	$(GSDL_DIR)/apache-httpd/$(GSDLOS)/bin/apxs  -i -n gsdl mod_gsdl.so
endif
	if test -e formatconverter; then \
		cp formatconverter $(GSDL_DIR)/bin/$(GSDLOS)/.; \
	fi;

clean:
	rm -f $(OBJECTS) $(LIBRARY) $(EXECUTABLE) $(APACHE_MODULE) $(APACHE_OBJS)

distclean:
	rm -f $(OBJECTS) $(LIBRARY) $(EXECUTABLE) Makefile

depend:
	makedepend -Y -- $(DEFS) $(INCLUDES) $(CXXFLAGS) -- $(SOURCES)
