###########################################################################
#
# Makefile -- runtime-src/src/z3950
# 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


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
else
MG_INDEXERS_LIBS =
endif

ENABLE_MGPP = @ENABLE_MGPP@
ifeq ($(ENABLE_MGPP), 1)
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_INDEXERS_LIBS =
MGPP_INCLUDES =
endif

USE_GDBM = @USE_GDBM@
ifeq ($(USE_GDBM), 1)
GDBM_LIBS = $(COMMON_PACKAGES_DIR)/gdbm/lib/libgdbm.a
GDBM_DEFINES = -DUSE_GDBM
else
GDBM_LIBS =
GDBM_DEFINES = 
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_LIBS = $(COMMON_PACKAGES_DIR)/sqlite/lib/libsqlite3.a -ldl -lpthread
SQLITE_DEFINES = -DUSE_SQLITE
else
SQLITE_LIBS = 
SQLITE_DEFINES =
endif

USE_Z3950 = @USE_Z3950@
ifeq ($(USE_Z3950), 1)
AR = ar
CC = @CC@
CPP = @CXX@
CFLAGS = @CFLAGS@
CPPFLAGS = @CXXFLAGS@
DEFS = @DEFS@ -DNZDL -DQUIET -DSHORT_SUFFIX -DPARADOCNUM -DGSDLSERVER $(GDBM_DEFINES) $(JDBM_DEFINES) $(SQLITE_DEFINES)
GSDLOS = @gsdlos@
# Do NOT add the "recpt" directory here: the z39.50 code should be independent of the receptionist!
INCLUDES = -I. -I$(GSDL_DIR) -I$(COMMON_DIR)/src/lib -I$(COLSERVR_DIR) -I$(PROTOCOL_DIR) \
	$(MGPP_INCLUDES) -I$(RUNTIME_PACKAGES_DIR)/yaz/yaz-2.1.56/include \
	-I$(RUNTIME_PACKAGES_DIR)/yaz/yaz-2.1.56/client -I$(RUNTIME_PACKAGES_DIR)/d2m
INSTALL = @INSTALL@
LDFLAGS = @LDFLAGS@
# 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 =" $(RUNTIME_PACKAGES_DIR)/yaz/yaz-2.1.56/Makefile | sed 's/^LIBS =//')
LIBS = @LIBS@ $(GDBM_LIBS) -L$(COMMON_PACKAGES_DIR)/expat/lib -lexpat \
	$(ACCENTFOLD_LIBS) $(SQLITE_LIBS) \
	$(RUNTIME_PACKAGES_DIR)/yaz/lib/libyaz.a $(RUNTIME_PACKAGES_DIR)/d2m/libd2m.a $(YAZ_LIBS) -lz
RANLIB = @RANLIB@


COMPILE = $(CC) -c $(CFLAGS) $(DEFS) $(INCLUDES)
CPPCOMPILE = $(CPP) -c $(CPPFLAGS) $(DEFS) $(INCLUDES)
CPPLINK = $(CPP) $(LDFLAGS) -o $@


.SUFFIXES:
.SUFFIXES: .c .o
.c.o:
	$(COMPILE) $<

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


LIBRARY_SOURCES = \
	yaz_zclient.c \
	z3950proto.cpp \
	z3950proxy.cpp \
	zparse.tab.c \
	zparse.yy.c

LIBRARY_OBJECTS = \
	yaz_zclient.o \
	z3950proto.o \
	z3950proxy.o \
	zparse.tab.o \
	zparse.yy.o

LIBRARY = gsdlz3950.a

SOURCES = \
	z3950explain.cpp \
	z3950parser.cpp \
	z3950server.cpp \
	z3950_to_gsdl.cpp

OBJECTS = \
	z3950explain.o \
	z3950parser.o \
	z3950server.o \
	z3950_to_gsdl.o

GSDL_LIBS = \
	gsdlz3950.a \
	$(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 = z3950server


# Default target: make both gsdlz3950.a and the z3950server executable
all: $(LIBRARY) $(EXECUTABLE)

gsdlz3950.a: $(LIBRARY_OBJECTS)
	rm -f $(LIBRARY)
	$(AR) cru $(LIBRARY) $(LIBRARY_OBJECTS) $(RUNTIME_PACKAGES_DIR)/yaz/yaz-2.1.56/client/tabcomplete.o
	$(RANLIB) $(LIBRARY)

z3950server: $(EXECUTABLE_OBJECTS)
	$(CPPLINK) $(EXECUTABLE_OBJECTS) $(LIBS)

install: $(EXECUTABLE)
	$(INSTALL) -m 755 -s $(EXECUTABLE) $(GSDL_DIR)/bin/$(GSDLOS)

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

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

else
all:
install:
clean:
depend:
endif

distclean: clean
	rm -f Makefile

