#!/usr/bin/make -f
# Sample debian/rules that uses debhelper. GNU copyright 1997 by Joey Hess.

package = openldap

CFLAGS := -O2
IE := install -m755 -s
ID := install -m644

# Some special build options
ifneq (,$(findstring debug,$(DEB_BUILD_OPTIONS)))
  CFLAGS += -g
  ifneq (,$(findstring nostrip,$(DEB_BUILD_OPTIONS)))
    IE := install -m755
  endif
endif

# the dbs rules
TAR_DIR := openldap-1.2.12
include debian/scripts/dbs-build.mk

# dpkg-arch rules
ifeq (,$(DEB_BUILD_GNU_TYPE))
  include debian/scripts/dpkg-arch.mk
endif

gwdoc	:= debian/openldap-gateways/usr/share/doc/openldap-gateways

# We need this version to make sure debconf works
dh_version  := 2.0.54

# Generate the files that debhelper uses
dh_mak_deps := $(shell perl debian/scripts/dh_split makedeps)
dh_gen_deps := $(shell perl debian/scripts/dh_split gendeps)

# Right now i386 and m68k remain at db1 until I come up with an upgrade
# path. Even though the db formats are compatible, slapd tries to use
# features in db2 that wont work on db2 database files, and thus it will
# fail.
ifneq (,$(findstring $(DEB_HOST_GNU_TYPE),$(patsubst %,%-linux,m68k i386)))
    ldbm_api=db
else
    ldbm_api=db2
endif

build: $(STAMP_DIR)/pre-build-stamp $(STAMP_DIR)/build-stamp
$(STAMP_DIR)/pre-build-stamp: $(patched) $(BUILD_TREE)/configure
	dh_testdir
	cd $(BUILD_TREE) && CFLAGS="$(CFLAGS)" ./configure --enable-shared \
		--prefix=/usr --enable-wrappers --without-kerberos \
		--enable-cldap --enable-passwd --enable-ldbm \
		--enable-aclgroups --localstatedir=/var --with-isode \
		--sysconfdir=/etc --with-ldbm-api=$(ldbm_api) \
		--libexecdir=/usr/sbin --mandir=/usr/share/man --enable-debug \
		--enable-shell \
		--host=$(DEB_HOST_GNU_TYPE) --build=$(DEB_BUILD_GNU_TYPE) && \
		sed < libtool > libtool-2 \
		-e 's/^hardcode_libdir_flag_spec.*$$/hardcode_libdir_flag_spec=" -D__LIBTOOL_IS_A_FOOL__ "/' \
		-e '/^archive_cmds="/s/"$$/ \\$$deplibs"/'; \
		mv -f libtool-2 libtool; \
		chmod 755 libtool; \
		$(MAKE) depend
	touch $(STAMP_DIR)/pre-build-stamp

$(BUILD_TREE)/configure: $(BUILD_TREE)/configure.in
	cd $(BUILD_TREE) && autoconf

$(STAMP_DIR)/build-stamp:
	dh_testdir
	cd $(BUILD_TREE) && $(MAKE)
	touch $(STAMP_DIR)/build-stamp

clean:
	dh_testdir
	rm -rf $(SOURCE_DIR) $(STAMP_DIR)
	perl debian/scripts/dh_split clean
	dh_clean

pre-binary: build $(dh_mak_deps)
	dh_testversion $(dh_version)
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs
	cd $(BUILD_TREE) && $(MAKE) TMPROOT=`pwd`/../../debian/tmp install

	# Name space cleansing
	mv debian/tmp/usr/bin/ud debian/tmp/usr/bin/ud-openldap
	mv debian/tmp/usr/share/man/man1/ud.1 debian/tmp/usr/share/man/man1/ud-openldap.1
	mv debian/tmp/usr/sbin/in.xfingerd debian/tmp/usr/sbin/in.ldapfingerd
	mv debian/tmp/usr/share/man/man8/in.xfingerd.8 debian/tmp/usr/share/man/man8/in.ldapfingerd.8

	dh_movefiles

	# Cleanup
	rmdir debian/tmp/usr/{lib,include/openldap,include,share/{man/man3,openldap}}
	rmdir debian/tmp/var/openldap-ldbm
	rm -f debian/tmp/etc/openldap/*.default
	rm -f debian/tmp/usr/share/man/man8/{edb2ldif.8,ldapd.8}

	# Some special files
	$(ID) debian/local/slapd.conf debian/tmp/etc/openldap/slapd.conf
	$(ID) debian/local/Netscape-Roaming-HOWTO \
		debian/tmp/usr/share/doc/openldapd/
	$(ID) -D debian/local/slapd.conf \
		debian/tmp/usr/share/doc/openldapd/slapd.conf.example
	$(ID) $(BUILD_TREE)/servers/slapd/schema/*.conf \
		debian/tmp/etc/openldap/

	# Lot's of docs for the gateways package, handle manually
	for doc in $(BUILD_TREE)/clients/mail500/{README,sendmail.cf}; do \
		$(ID) -D $${doc} $(gwdoc)/mail500/$${doc}; \
	done
	$(ID) $(BUILD_TREE)/clients/fax500/README ${gwdoc}/README.fax500
	$(ID) $(BUILD_TREE)/clients/gopher/README ${gwdoc}/README.go500
	$(ID) $(BUILD_TREE)/clients/rcpt500/README ${gwdoc}/README.rcpt500

	# Debian specific objectClasses and attributes
	$(ID) debian/local/debian.{at,oc}.conf debian/tmp/etc/openldap/
	$(ID) debian/local/netscape_roaming.{at,oc}.conf debian/tmp/etc/openldap/

binary-indep: pre-binary $(dh_mak_deps)
	dh_testversion $(dh_version)
	dh_testdir -i
	dh_testroot -i
	dh_installdirs -i
	dh_installdocs -plibopenldap-runtime
	dh_installdocs -pldap-rfc $(BUILD_TREE)/doc/rfc
	dh_installdebconf -i
	dh_installchangelogs -i
	dh_compress -i
	dh_fixperms -i
	dh_installdeb -i
	dh_gencontrol -i
	dh_builddeb -i

binary-arch: pre-binary slapd_db_check $(dh_mak_deps)
	dh_testversion $(dh_version)
	dh_testdir -a
	dh_testroot -a
	dh_installdirs -a
	dh_installdocs -a
	dh_installdebconf -a
	dh_installchangelogs -a $(BUILD_TREE)/CHANGES
	dh_undocumented -a
	dh_installexamples -popenldapd $(BUILD_TREE)/servers/slapd/schema/nis_sample.ldif
ifeq (,$(findstring nostrip,$(DEB_BUILD_OPTIONS)))
	dh_strip -a
endif
	dh_installinit -a -n # We use our own routines, but let dh install it
	dh_compress -a
	dh_fixperms -a
	dh_installdeb -a
	dh_shlibdeps -a
	dh_gencontrol -a
	dh_makeshlibs -a
	dh_md5sums -a
	dh_builddeb -a

$(dh_mak_deps): $(dh_gen_deps)
	perl debian/scripts/dh_split

slapd_db_check: $(STAMP_DIR)/slapd_db_check-stamp
$(STAMP_DIR)/slapd_db_check-stamp:
ifneq (,$(findstring $(DEB_HOST_GNU_TYPE),$(patsubst %,%-linux,m68k i386)))
	test=`ldd debian/tmp/usr/sbin/slapd | grep libdb.so.2`; \
	if [ "x$$test" = "x" ]; then \
		echo slapd is not linked to libdb.so.2; \
		exit 1; \
	fi
endif

binary: binary-indep binary-arch
.PHONY: build clean binary-indep binary-arch binary pre-binary
