#!/usr/bin/make -f

export DH_COMPAT = 4

CFLAGS = -Wall -g -D_FILE_OFFSET_BITS=64
INSTALL = install
INSTALL_FILE    = $(INSTALL) -p    -o root -g root  -m  644
INSTALL_PROGRAM = $(INSTALL) -p    -o root -g root  -m  755
INSTALL_SCRIPT  = $(INSTALL) -p    -o root -g root  -m  755
INSTALL_DIR     = $(INSTALL) -p -d -o root -g root  -m  755

ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
CFLAGS += -O0
else
CFLAGS += -O2
endif
ifeq (,$(findstring nostrip,$(DEB_BUILD_OPTIONS)))
INSTALL_PROGRAM += -s
endif

CONFIG	      = $(shell grep -v "^\#" debian/configure.options)

rootdir		:= $(shell pwd)
installdir	:= $(rootdir)/debian/install
tmpdir		:= $(rootdir)/debian/tmp
builddir	:= $(rootdir)/debian/build

$(builddir)/Makefile:
	mkdir -p $(builddir)
	cd $(builddir) && CFLAGS="$(CFLAGS)" $(rootdir)/configure \
		$(CONFIG) 
	perl debian/check_config
	$(MAKE) -C $(builddir) depend

build: build-stamp
build-stamp: $(builddir)/Makefile
	$(MAKE) -C $(builddir) DESTDIR=$(installdir) 
	touch $@

install: install-stamp
install-stamp: build-stamp
	$(MAKE) -C $(builddir) DESTDIR=$(installdir) install
	# Upstream installs schema files in mode 0444 - policy wants 0644
	find $(installdir)/etc -type f|xargs chmod 0644
	touch $@


binary: binary-arch

binary-indep: build install

binary-arch: install-stamp
	dh_testdir
	dh_testroot
	dh_clean -k

	dh_installdirs -a
	cp -al $(installdir) $(tmpdir)
	sh debian/move_files

	dh_installchangelogs -a
	dh_installdocs -a
	dh_installexamples -a
	dh_installinit -Xslapd.prerm -a -- "defaults 19 80"
	dh_installdebconf -a
	dh_installcron -a
	dh_strip -a
	dh_compress -a
	dh_makeshlibs -plibldap2
	dh_installdeb -a
	perl -w debian/dh_installscripts-common -p slapd
	dh_shlibdeps -a
	dh_gencontrol -a
	dh_md5sums -a
	dh_builddeb -a

clean:
	dh_testdir
	dh_testroot
	rm -f install-stamp build-stamp
	rm -rf $(builddir) $(builddir_notls) $(installdir)
	dh_clean

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

