#!/usr/bin/make -f

# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1

# This has to be exported to make some magic below work.
export DH_OPTIONS

# Set the host and build architectures for use with config.cache loading,
# cross-building, etc.
DEB_HOST_GNU_TYPE	:= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
DEB_BUILD_GNU_TYPE	:= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)
DEB_HOST_ARCH_OS	:= $(shell dpkg-architecture -qDEB_HOST_ARCH_OS)

export DEB_HOST_GNU_TYPE
export DEB_BUILD_GNU_TYPE
export DEB_HOST_ARCH_OS

CFLAGS = -g -Wall

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

DESTDIR=$(CURDIR)/debian/tmp

conf_args = \
		--prefix=/usr \
		--sysconfdir=/etc \
		--mandir=/usr/share/man \
		--localstatedir=/var/lib \
		--with-logdir=/var/log/ctdb

configure: configure-stamp
configure-stamp:
	dh_testdir
	CFLAGS="$(CFLAGS)" ./configure $(conf_args)
	touch $@

#Architecture 
build: check-external-libs build-arch build-indep

build-arch: build-arch-stamp
build-arch-stamp: configure-stamp
	[ ! -f Makefile ] || $(MAKE)
	touch $@

build-indep: build-indep-stamp
build-indep-stamp: configure-stamp
	touch $@

clean:
	dh_testdir
	dh_testroot
	rm -f build-arch-stamp build-indep-stamp configure-stamp
	rm -f utils/smnotify/smnotify.h utils/smnotify/gen_smnotify.c
	
	rm -rf $(DESTDIR)
	[ ! -f Makefile ] || $(MAKE) distclean
	
	dh_clean 

install: install-indep install-arch
install-indep:
	dh_testdir
	dh_testroot
	dh_prep -i 
	dh_installdirs -i
	
	dh_install -i

install-arch:
	dh_testdir
	dh_testroot
	dh_prep -s 
	dh_installdirs -s
	
	$(MAKE) DESTDIR=$(DESTDIR) install
	mkdir -p $(DESTDIR)/etc/default
	install -m644 config/ctdb.sysconfig $(DESTDIR)/etc/default/ctdb
	dh_install -s

binary-common:
	dh_testdir
	dh_testroot
	dh_installdocs
ifeq ($(DEB_BUILD_ARCH_OS), hurd)
	dh_installdocs debian/README.hurd
endif
	dh_installchangelogs 
	#patch doc
	sed -i 's|/etc/sysconfig/|/etc/default/|' $(CURDIR)/debian/ctdb/usr/share/doc/ctdb/web/*.html
	#upstream changelog 
	grep -A 10000 '%changelog' packaging/RPM/ctdb.spec.in > $(CURDIR)/debian/ctdb/usr/share/doc/ctdb/changelog
	dh_installexamples
	dh_installman
	#install init script
	DH_OPTIONS= dh_installinit -pctdb --no-start --no-restart-on-upgrade
	dh_installlogrotate
	dh_lintian
	dh_link
	dh_compress 
	dh_fixperms
	dh_strip --dbg-package=ctdb-dbg
	dh_makeshlibs
	dh_shlibdeps
	dh_installdeb
	dh_gencontrol
	dh_md5sums
	dh_builddeb
# Build architecture independant packages using the common target.
binary-indep: build-indep install-indep
	$(MAKE) -f debian/rules DH_OPTIONS=-i binary-common

# Build architecture dependant packages using the common target.
binary-arch: build-arch install-arch
	$(MAKE) -f debian/rules DH_OPTIONS=-s binary-common

binary: binary-arch binary-indep

check-external-libs:
	if [ -e lib/popt ]; then \
              echo " *****************************************"; \
	      echo " **                                     **"; \
	      echo " **  tar contains external libraries!   **"; \
	      echo " **                                     **"; \
              echo " *****************************************"; \
	      echo "The tarball should be made by using uscan or"; \
	      echo "debian/uupdate-wrapper"; \
	      echo ""; \
	      exit 1; \
        fi

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