#!gmake
#
CC = @CC@
prefix = @prefix@
exec_prefix = @exec_prefix@
datarootdir = @datarootdir@
includedir = @includedir@
libdir = @libdir@
libexecdir = @libexecdir@
bindir = @bindir@
sbindir = @sbindir@
mandir = @mandir@
localstatedir = @localstatedir@
initrddir = @initrddir@
VPATH = @srcdir@:@libreplacedir@
srcdir = @srcdir@
builddir = @builddir@
sharedbuilddir = @sharedbuilddir@
INSTALLCMD = @INSTALL@
EXTRA_OBJ=@EXTRA_OBJ@
SSSD_LIBEXEC_PATH = @SSSD_LIBEXEC_PATH@
SSSD_LIBDIR = $(libdir)/@PACKAGE_NAME@
LDB_LIBDIR = $(libdir)/ldb
SSSD_INTROSPECT_PATH = @SSSD_INTROSPECT_PATH@
PACKAGE_VERSION = @PACKAGE_VERSION@
srvdir = $(srcdir)
pipepath = @pipepath@
dbpath = @dbpath@

TALLOC_LIBS = @TALLOC_LIBS@
TALLOC_CFLAGS = @TALLOC_CFLAGS@

TDB_LIBS = @TDB_LIBS@
TDB_CFLAGS = @TDB_CFLAGS@

TEVENT_LIBS = @TEVENT_LIBS@
TEVENT_CFLAGS = @TEVENT_CFLAGS@

POPT_LIBS = @POPT_LIBS@
POPT_CFLAGS = @POPT_CFLAGS@

LDB_LIBS = @LDB_LIBS@
LDB_CFLAGS = @LDB_CFLAGS@

DBUS_LIBS = @DBUS_LIBS@
DBUS_CFLAGS = @DBUS_CFLAGS@

CHECK_LIBS = @CHECK_LIBS@
CHECK_CFLAGS = @CHECK_CFLAGS@

NSS_LIBS = @NSS_LIBS@
NSS_CFLAGS = @NSS_CFLAGS@

PAM_LIBS = @PAM_LIBS@

OPENLDAP_LIBS = @OPENLDAP_LIBS@

MOZLDAP_CFLAGS = @MOZLDAP_CFLAGS@
MOZLDAP_LIBS = @MOZLDAP_LIBS@

LDAP_CFLAGS = $(MOZLDAP_CFLAGS) $(OPENLDAP_CFLAGS)
LDAP_LIBS = $(MOZLDAP_LIBS) $(OPENLDAP_LIBS)

LIBDL = @LIBDL@

SHLIBEXT = @SHLIBEXT@

LD_EXPORT_DYNAMIC = @LD_EXPORT_DYNAMIC@
SHLD = @SHLD@
SHLD_FLAGS = @SHLD_FLAGS@
SONAMEFLAG = @SONAMEFLAG@

LDFLAGS += @LDFLAGS@ -L$(srcdir)/lib
LIBS = @LIBS@ $(TALLOC_LIBS) $(TDB_LIBS) $(TEVENT_LIBS) $(POPT_LIBS) $(LDB_LIBS) $(DBUS_LIBS)

PICFLAG = @PICFLAG@
CFLAGS += -g -I$(srcdir)/include -Iinclude -I$(srcdir) -I$(srcdir)/.. \
       $(POPT_CFLAGS) $(TALLOC_CFLAGS) $(TDB_CFLAGS) $(TEVENT_CFLAGS) $(LDB_CFLAGS) $(DBUS_CFLAGS) $(CHECK_CFLAGS)\
	-DLIBDIR=\"$(libdir)\" -DSHLIBEXT=\"$(SHLIBEXT)\" -DSSSD_LIBEXEC_PATH=\"$(SSSD_LIBEXEC_PATH)\" -DSSSD_INTROSPECT_PATH=\"$(SSSD_INTROSPECT_PATH)\"\
	-DUSE_MMAP=1 @CFLAGS@

MDLD = @MDLD@
MDLD_FLAGS = @MDLD_FLAGS@

HAVE_INFOPIPE = @HAVE_INFOPIPE@
HAVE_POLICYKIT = @HAVE_POLICYKIT@
HAVE_TESTS = @HAVE_TESTS@

MEMBEROF_SOBASE=memberof.$(SHLIBEXT)
MEMBEROF_SONAME=$(MEMBEROF_SOBASE).0
MEMBEROF_SOLIB=$(MEMBEROF_SOBASE).$(PACKAGE_VERSION)

PROXY_BE_SOBASE=libsss_proxy.$(SHLIBEXT)
PROXY_BE_SONAME=$(PROXY_BE_SOBASE).0
PROXY_BE_SOLIB=$(PROXY_BE_SOBASE).$(PACKAGE_VERSION)

LDAP_BE_SOBASE=libsss_ldap.$(SHLIBEXT)
LDAP_BE_SONAME=$(LDAP_BE_SOBASE).0
LDAP_BE_SOLIB=$(LDAP_BE_SOBASE).$(PACKAGE_VERSION)

default: all

include $(srvdir)/rules.mk
include $(srvdir)/server.mk

OBJS = $(SERVER_OBJ) $(EXTRA_OBJ)

headers =

DBUS_SYSBUS_POLICY_DIR = @sysconfdir@/dbus-1/system.d

LIBEXECBINS =  sbin/sssd_nss sbin/sssd_dp sbin/sssd_be sbin/sssd_pam
ifneq (x$(HAVE_INFOPIPE), x)
    LIBEXECBINS += sbin/sssd_info
    DBUS_SYSBUS_POLICIES = infopipe/org.freeipa.sssd.infopipe.conf
    INFP_INTROSPECT_XML = infopipe/org.freeipa.sssd.infopipe.Introspect.xml
endif
ifneq (x$(HAVE_POLICYKIT), x)
    LIBEXECBINS += sbin/sssd_pk
endif
TOOLSBINS = sbin/sss_useradd sbin/sss_userdel sbin/sss_groupadd sbin/sss_groupdel sbin/sss_usermod
BINS = sbin/sssd $(LIBEXECBINS) $(TOOLSBINS)

ifneq (x$(HAVE_TESTS), x)
    TESTS = tests/sysdb-tests tests/infopipe-tests
    TESTS_DATA = tests/tests.ldb tests/tests_conf.ldb tests/introspect.ref
endif

SOLIBS = lib/$(PROXY_BE_SOLIB) lib/$(LDAP_BE_SOLIB)
LDBLIBS = lib/$(MEMBEROF_SOLIB)
SONAMELIBS = lib/$(PROXY_BE_SONAME) lib/$(MEMBEROF_SONAME) lib/$(LDAP_BE_SONAME)
SOBASELIBS = lib/$(PROXY_BE_SOBASE) lib/$(MEMBEROF_SOBASE) lib/$(LDAP_BE_SOBASE)

DIRS = sbin lib

all: showflags dirs $(OBJS) $(BINS) $(SOBASELIBS)

shared-build: all

tests: all $(TESTS)
	cp $(INFP_INTROSPECT_XML) tests/introspect.ref

dirs:
	@mkdir -p $(DIRS)

clean:: testclean
	rm -f $(OBJS) $(BINS) $(MODULES)
	rm -f *.o */*.o */*/*.o
	rm -f $(BINS)
	rm -f $(SOBASELIBS) $(SONAMELIBS) $(SOLIBS) $(LDBLIBS)

distclean:: clean
	rm -rf $(DIRS)
	rm -f config.log config.status config.cache config.h
	rm -f Makefile

realdistclean:: distclean
	rm -f configure config.h.in

testclean::
	rm -f $(TESTS) $(TESTS_DATA)

install:: all installdirs installheaders installlibs installbin installsupport
	${INSTALLCMD} -d $(DESTDIR)$(sbindir)
	${INSTALLCMD} -m 755 sbin/sssd $(DESTDIR)$(sbindir)
	${INSTALLCMD} -m 755 $(TOOLSBINS) $(DESTDIR)$(sbindir)
	${INSTALLCMD} -m 755 sysv/sssd $(DESTDIR)$(initrddir)
	${INSTALLCMD} -d $(DESTDIR)$(SSSD_LIBEXEC_PATH)
	${INSTALLCMD} -m 755 $(LIBEXECBINS) $(DESTDIR)$(SSSD_LIBEXEC_PATH)

installdirs::
	mkdir -p $(DESTDIR)$(includedir) \
	          $(DESTDIR)$(libdir) \
	          $(DESTDIR)$(sbindir) \
		  $(DESTDIR)$(initrddir) \
		  $(DESTDIR)$(SSSD_LIBDIR) \
		  $(DESTDIR)$(LDB_LIBDIR) \
	          $(DESTDIR)$(DBUS_SYSBUS_POLICY_DIR) \
	          $(DESTDIR)$(SSSD_INTROSPECT_PATH)/infopipe \
		  $(DESTDIR)$(pipepath)/private \
		  $(DESTDIR)$(dbpath)

installheaders:: installdirs
ifneq (x$(headers), x)
	cp $(headers) $(DESTDIR)$(includedir)
endif

installlibs:: installdirs
ifneq (x$(STATICLIB)$(LIBSOLIB), x)
	cp $(STATICLIB) $(LIBSOLIB) $(DESTDIR)$(SSSD_LIBDIR)
endif
	${INSTALLCMD} -m 755 $(SOLIBS) $(DESTDIR)$(SSSD_LIBDIR)
	${INSTALLCMD} -m 755 $(LDBLIBS) $(DESTDIR)$(LDB_LIBDIR)
	ln -fs $(PROXY_BE_SOLIB) $(DESTDIR)$(SSSD_LIBDIR)/$(PROXY_BE_SONAME)
	ln -fs $(PROXY_BE_SOLIB) $(DESTDIR)$(SSSD_LIBDIR)/$(PROXY_BE_SOBASE)
	ln -fs $(LDAP_BE_SOLIB) $(DESTDIR)$(SSSD_LIBDIR)/$(LDAP_BE_SONAME)
	ln -fs $(LDAP_BE_SOLIB) $(DESTDIR)$(SSSD_LIBDIR)/$(LDAP_BE_SOBASE)
	ln -fs $(MEMBEROF_SOLIB) $(DESTDIR)$(LDB_LIBDIR)/$(MEMBEROF_SONAME)
	ln -fs $(MEMBEROF_SOLIB) $(DESTDIR)$(LDB_LIBDIR)/$(MEMBEROF_SOBASE)

installbin:: installdirs

installsupport:: installdirs
ifneq (x$(HAVE_INFOPIPE), x)
	cp $(DBUS_SYSBUS_POLICIES) $(DESTDIR)$(DBUS_SYSBUS_POLICY_DIR)
	cp $(INFP_INTROSPECT_XML) $(DESTDIR)$(SSSD_INTROSPECT_PATH)/infopipe
endif