#
# Makefile
#
# Make file for OpenH323 Gatekeeper - The GNU Gatekeeper
#
# Reconstructed by C.W.Huang   Nov 11, 2002
#


PROG	=	gnugk
SOURCES	:=	gk.cxx gkauth.cxx gkDestAnalysis.cxx \
		RasSrv.cxx RasTbl.cxx GkClient.cxx \
		MulticastGRQ.cxx BroadcastListen.cxx \
		SoftPBX.cxx Toolkit.cxx h323util.cxx GkStatus.cxx \
		yasocket.cxx thread.cxx ProxyThread.cxx ProxyChannel.cxx \
		singleton.cxx main.cxx gkacct.cxx radproto.cxx radauth.cxx radacct.cxx

# Accounting support
ifdef HAS_ACCT
STDCCFLAGS += -DHAS_ACCT=1
endif

# RADIUS support
ifndef NO_RADIUS
STDCCFLAGS += -DHAS_RADIUS=1
endif

ifndef ANSI
STDCCFLAGS	+= -DGK_NOANSI
endif

# LDAP support
ifdef HAS_LDAP

SOURCES += gkldap.cxx
STDCCFLAGS += -DLDAP_HAS_CACHE

ifndef LDAP1823DIR
LDAP1823DIR := /usr/include
export LDAP1823DIR
endif
ifndef LDAP1823LIBDIR
LDAP1823LIBDIR := /usr/lib
export LDAP1823LIBDIR
endif
# this is the file name of the lib. To the linker always flag (-l) with
# std. name 'ldap' is passed
ifndef LDAP1823LIBNM
LDAP1823LIBNM := libldap.so
export LDAP1823LIBNM
endif
ifneq (,$(wildcard $(LDAP1823DIR)/ldap.h))
HAVE_LDAP1823_HDRS = 1
endif
ifneq (,$(wildcard $(LDAP1823LIBDIR)/$(LDAP1823LIBNM)))
HAVE_LDAP1823_LIBS = 1
endif

# add test for HAS_LEVEL_TWO_LDAPAPI here
ifneq (,$(wildcard ldap/include/ldapapi.h))
#ifneq (,$(wildcard ldap/lib/libldapapi_$(PLATFORM_TYPE)_$(OBJ_SUFFIX).$(LIB_SUFFIX)))
HAS_LEVEL_TWO_LDAPAPI=1
endif

# This is needed for a locally used encoding lib.
ifdef HAS_MWBB1
ifndef MWBB1DIR
MWBB1DIR := .
endif				# MWBB1DIR
ifndef MWBB1LIBDIR
MWBB1LIBDIR := .
endif				# MWBB1LIBDIR
ifndef MWBB1_TAG
MWBB1_TAG := "{TAG}"
export MWBB1_TAG
endif
STDCCFLAGS	+= -D'MWBB1_TAG=$(MWBB1_TAG)' -I$(MWBB1DIR)
LDFLAGS         += -L$(MWBB1LIBDIR)
ENDLDLIBS	+= -lMWCrypt
ifneq (,(LD_RUN_PATH))
LD_RUN_stub     := $(LD_RUN_PATH)
LD_RUN_PATH     += $(LD_RUN_stub):$(MWBB1LIBDIR)
else
LD_RUN_PATH     += $(MWBB1LIBDIR)
endif
export LD_RUN_PATH
endif				# HAS_MWBB1

ifdef HAS_LEVEL_TWO_LDAPAPI
SOURCES += ldaplink.cxx gk_ldap_interface.cxx
LDAP_LIB = ./ldap/lib/libldapapi_$(PLATFORM_TYPE)_$(OBJ_SUFFIX).$(LIB_SUFFIX)

SUBDIRS += ldap/src
LDLIBS	+= -lldapapi_$(PLATFORM_TYPE)_$(OBJ_SUFFIX) $(TMP_LDLIBS)
LDFLAGS += -L./ldap/lib/
HAS_LDAP	= 1
STDCCFLAGS	+= -I./ldap/include -D"HAS_LDAP=$(HAS_LDAP)" \
                   -D"HAS_LEVEL_TWO_LDAPAPI=$(HAS_LEVEL_TWO_LDAPAPI)" -D"LDAPVERSION=2"
else
ifdef HAVE_LDAP1823_HDRS
ifdef HAVE_LDAP1823_LIBS
SOURCES         += ldaplink.cxx gk_ldap_interface.cxx
ENDLDLIBS	+= -lldap
HAS_LDAP	= 1
export HAS_LDAP
# due to the unwise naming of the libH323 header, the std. header 'ldap.h'
# would be hooded, if the include search path would not PREpended
STDCCFLAGS_stub := $(STDCCFLAGS)
STDCCFLAGS	= -D"HAS_LDAP=$(HAS_LDAP)" -I$(LDAP1823DIR) $(STDCCFLAGS_stub)
LDFLAGS         += -L$(LDAP1823LIBDIR)
ifneq (,(LD_RUN_PATH))
LD_RUN_stub     := $(LD_RUN_PATH)
LD_RUN_PATH     += $(LD_RUN_stub):$(LDAP1823LIBDIR)
else
LD_RUN_PATH     += $(LDAP1823LIBDIR)
endif
export LD_RUN_PATH
endif
endif
endif

endif
# end of LDAP configuration

# MySQL support
# has to be added after LDAP support because order of -I options is crucial
ifndef NO_MYSQL
ifndef MYSQLDIR
ifneq (,$(wildcard /usr/include/mysql/mysql++))
MYSQLDIR := /usr/include/mysql
export MYSQLDIR
endif
endif

ifdef MYSQLDIR
ifneq (,$(wildcard $(MYSQLDIR)))
STDCCFLAGS_stub := $(STDCCFLAGS)
STDCCFLAGS	= -DHAS_MYSQL=1 -I$(MYSQLDIR) $(STDCCFLAGS_stub)
#LDFLAGS	+= -L$(MYSQLDIR)/lib
ENDLDLIBS	+= -lsqlplus
HAS_MYSQL	= 1
endif
endif
endif
# end of MySQL configuration

ifndef NO_WAITARQ
SOURCES += WaitingARQ.cxx
STDCCFLAGS	+= -DHAS_WAITARQ=1
HAS_WAITARQ	= 1
endif

# include the Makefile of OpenH323
ifndef OPENH323DIR
OPENH323DIR=$(HOME)/openh323
endif

VERSION_FILE := $(OPENH323DIR)/version.h

include $(OPENH323DIR)/openh323u.mak

### Remove -fdata-sections gcc option that cause problems during link step
temp_STDCCFLAGS := $(subst -fdata-sections,,$(STDCCFLAGS))
STDCCFLAGS = $(temp_STDCCFLAGS)

STDCCFLAGS	+= -DMAJOR_VERSION=$(MAJOR_VERSION) -DMINOR_VERSION=$(MINOR_VERSION) -DBUILD_NUMBER=$(BUILD_NUMBER)

ifdef LARGE_FDSET
STDCCFLAGS_stub	:= $(STDCCFLAGS)
STDCCFLAGS	= -DLARGE_FDSET=$(LARGE_FDSET) $(STDCCFLAGS_stub)
endif

ifdef HAS_LEVEL_TWO_LDAPAPI
TARGET_LIBS += $(LDAP_LIB)
ifdef DEBUG
$(LDAP_LIB): 
	$(MAKE) -C ldap/src debug
else
$(LDAP_LIB):
	$(MAKE) -C ldap/src opt
endif
$(TARGET):	$(OBJS) $(TARGET_LIBS)
	$(MAKE) $(TARGET_LIBS)
	$(CPLUS) -o $@ $(CFLAGS) $(LDFLAGS) $(OBJS) $(LDLIBS) $(ENDLDLIBS) $(ENDLDFLAGS)
endif

# extra targets
addpasswd: $(OBJDIR)/addpasswd.o
	$(CXX) -o $(OBJDIR)/addpasswd $(CFLAGS) $(OBJDIR)/addpasswd.o $(LDFLAGS) -l$(PTLIB_BASE)$(LIB_TYPE) $(ENDLDLIBS) $(ENDLDFLAGS)

doc:	docs/manual.sgml
	cd docs; sgml2html manual.sgml; \
	which bg5sgml2html > /dev/null 2>&1 && \
	bg5sgml2html manual-zh.sgml || true

# end
