#!/usr/bin/make -f
# debian/rules for iucode-tool
# Copyright (c) 2011,2012 by Henrique de Moraes Holschuh
# Published under the GNU GPL license version 2 or any later versions

PACKAGE :=iucode-tool
DEBDIR  :=$(CURDIR)/debian
PKGDIR  :=$(DEBDIR)/$(PACKAGE)

# DebHelper control
export DH_ALWAYS_EXCLUDE=CVS:.svn:.git

# Init Makefile variables.  Usually already set by dpkg-buildpackage
DEB_HOST_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
DEB_BUILD_GNU_TYPE ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)
CFLAGS   ?= $(shell dpkg-buildflags --get CFLAGS || echo "-O2 -g")
CPPFLAGS ?= $(shell dpkg-buildflags --get CPPFLAGS)
LDFLAGS  ?= $(shell dpkg-buildflags --get LDFLAGS)

# Force immediate expansion, so that we do it only once
DEB_HOST_GNU_TYPE  := $(DEB_HOST_GNU_TYPE)
DEB_BUILD_GNU_TYPE := $(DEB_BUILD_GNU_TYPE)
CFLAGS   := $(CFLAGS)
CPPFLAGS := $(CPPFLAGS)
LDFLAGS  := $(LDFLAGS)

# custom DEB_BUILD_OPTIONS...
ifneq (,$(findstring strict,$(DEB_BUILD_OPTIONS)))
	CFLAGS += -Werror
endif

# autotools
confflags=
ifeq ($(DEB_BUILD_GNU_TYPE), $(DEB_HOST_GNU_TYPE))
	confflags += --build $(DEB_HOST_GNU_TYPE)
else
	confflags += --build $(DEB_BUILD_GNU_TYPE) --host $(DEB_HOST_GNU_TYPE)
endif

build-prepare: build-prepare-stamp
build-prepare-stamp:
	@dh_testdir
	./autogen.sh
	touch build-prepare-stamp

configure-stamp: | build-prepare
	dh_testdir

	./configure \
	   CFLAGS="$(CFLAGS) -Wall -pipe" $(confflags) \
	   CPPFLAGS="$(CPPFLAGS)" \
	   LDFLAGS="$(LDFLAGS)" \
	   --prefix=/usr

	touch configure-stamp

build: build-stamp
build-stamp: configure-stamp
	dh_testdir

	$(MAKE)

	touch build-stamp

build-arch: build
build-indep:

clean:
	dh_testdir
	dh_testroot
	rm -f build-stamp configure-stamp
	rm -rf autom4te.cache

	[ ! -f Makefile ] || $(MAKE) distclean

	rm -f config.sub config.guess configure config.log
	rm -fr autom4te.cache

	rm -f Makefile.in aclocal.m4 Makefile
	rm -f compile depcomp install-sh missing iucode_tool_config.h.in

	dh_clean

install: build
	dh_testdir
	dh_testroot
	dh_prep
	dh_installdirs

	$(MAKE) install DESTDIR="$(PKGDIR)"

	dh_install

install-arch: install
install-indep:

binary-indep: build-indep install-indep

binary-arch: build-arch install-arch
	dh_testdir
	dh_testroot
#	dh_installdebconf
	dh_installdocs
#	dh_installexamples
	dh_installmodules
#	dh_installinit --error-handler=true -- start 45 S .
	dh_installman
	dh_installchangelogs ChangeLog
	dh_lintian
	dh_link
	dh_strip
	dh_compress
	dh_fixperms
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb

binary: binary-indep binary-arch

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