#!/usr/bin/make -f

#export DH_VERBOSE=1

DEB_HOST_GNU_TYPE   ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
DEB_BUILD_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)

CFLAGS = -Wall -g
 
ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
        CFLAGS += -O0
else
        CFLAGS += -O2
endif

configure: configure-stamp
configure-stamp:
	dh_testdir
	# Add here commands to configure the package.
	ln -sf /usr/share/misc/config.sub config.sub
	ln -sf /usr/share/misc/config.guess config.guess
	./configure --host=$(DEB_HOST_GNU_TYPE) --build=$(DEB_BUILD_GNU_TYPE) --prefix=/ --mandir=\$${prefix}/usr/share/man --infodir=\$${prefix}/usr/share/info --sbindir=\$${prefix}/usr/sbin --sysconfdir=\$${prefix}/etc --libdir=\$${prefix}/usr/lib --datadir=\$${prefix}/usr/share --disable-rpath CFLAGS="$(CFLAGS)"
	touch $@

build: build-stamp
build-stamp: configure-stamp
	$(MAKE) 
	touch $@

clean:
	dh_testdir
	dh_testroot
	rm -f build-stamp configure-stamp

	[ ! -f Makefile ] || $(MAKE) distclean
	rm -f config.sub config.guess

	dh_clean 

install:
	dh_testdir
	dh_testroot
	dh_clean -k -s 
	dh_installdirs -s

	$(MAKE) DESTDIR=$(CURDIR)/debian/tmp install
	[ -d debian/tmp/etc ] || install -d -m 700 debian/tmp/etc
	install -m 644 debian/dsyslog.conf debian/tmp/etc
	[ -d debian/tmp/usr/share/dsyslog ] || install -d -m 700 debian/tmp/usr/share/dsyslog
	install -m 644 schemas/tables.mysql debian/tmp/usr/share/dsyslog
	install -m 644 schemas/tables.pqsql debian/tmp/usr/share/dsyslog

	dh_install -s --sourcedir=debian/tmp

binary-arch: install
	dh_testdir
	dh_testroot
	dh_installchangelogs 
	dh_installdocs
	dh_installlogrotate	
	dh_installinit
	dh_installman
	dh_installexamples
	dh_link
	dh_strip --dbg-package=dsyslog-dbg
	dh_compress 
	dh_fixperms
	dh_makeshlibs
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb

binary: binary-arch

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