#!/usr/bin/make -f

STAMP_DIR := debian/stampdir

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

# list of flavors we build; each gets a builddir, a configure pass (configure
# args are defined below), a build pass, and an install pass (in two steps)
# Note: the main flavor is required
FLAVORS := main

ABIFLAG_32=-m32
ABIFLAG_64=-m64

ifeq ($(DEB_HOST_ARCH),sparc)
FLAVORS += m64
DEB_BUILD_GNU_TYPE_32 = sparc64-linux-gnu
else ifeq ($(DEB_HOST_ARCH),i386)
FLAVORS += m64
DEB_BUILD_GNU_TYPE_64 = x86_64-linux-gnu
else ifeq ($(DEB_HOST_ARCH),kfreebsd-amd64)
FLAVORS += m32
DEB_BUILD_GNU_TYPE_32 = i486-kfreebsd-gnu
else ifeq ($(DEB_HOST_ARCH),amd64)
FLAVORS += m32
DEB_BUILD_GNU_TYPE_32 = i486-linux-gnu
else ifeq ($(DEB_HOST_ARCH),mips)
ABIFLAG_32=-mabi=n32
ABIFLAG_64=-mabi=64
FLAVORS += m64 m32
# What to put here?
DEB_BUILD_GNU_TYPE_32 = $(DEB_BUILD_GNU_TYPE)
DEB_BUILD_GNU_TYPE_64 = $(DEB_BUILD_GNU_TYPE)
else ifeq ($(DEB_HOST_ARCH),mipsel)
ABIFLAG_32=-mabi=n32
ABIFLAG_64=-mabi=64
FLAVORS += m64 m32
# What to put here?
DEB_BUILD_GNU_TYPE_32 = $(DEB_BUILD_GNU_TYPE)
DEB_BUILD_GNU_TYPE_64 = $(DEB_BUILD_GNU_TYPE)
else ifeq ($(DEB_HOST_ARCH),powerpc)
FLAVORS += m64
DEB_BUILD_GNU_TYPE_64 = powerpc64-linux-gnu
else ifeq ($(DEB_HOST_ARCH),s390)
FLAVORS += m64
DEB_BUILD_GNU_TYPE_64 = s390x-linux-gnu
endif


# build dir for the current flavor; this is only expanded in flavor specific
# targets
# Note: dh_clean will rm -rf debian/tmp, hence all builds
builddir = $(buildbasedir)/$*
buildbasedir = $(CURDIR)/debian/build

common_configure_flags += \
		--prefix=/usr \
		--mandir=/usr/share/man \
		--infodir=/usr/share/info \
		--docdir=/usr/share/doc/libnss-myhostname \
		--sysconfdir=/etc \
		--localstatedir=/var \
		--libexecdir=/lib/nss-myhostname \
		--host=$(DEB_HOST_GNU_TYPE)

main_configure_flags := $(common_configure_flags) --libdir=/lib \
		--build=$(DEB_BUILD_GNU_TYPE)
m32_configure_flags := $(common_configure_flags) --libdir=/lib32 \
		--build=$(DEB_BUILD_GNU_TYPE_32)
m64_configure_flags := $(common_configure_flags) --libdir=/lib64 \
		--build=$(DEB_BUILD_GNU_TYPE_64)

common_cflags := -Wall -g -O$(if $(findstring noopt,$(DEB_BUILD_OPTIONS)),0,2)
main_cflags := $(common_cflags)
m32_cflags := $(common_cflags) $(ABIFLAG_32)
m64_cflags := $(common_cflags) $(ABIFLAG_64)

LDFLAGS += -Wl,-z,defs

$(STAMP_DIR)/configure-stamp-%:
	dh_testdir
	mkdir -p $(builddir)
	cd $(builddir) && \
		CFLAGS="$($*_cflags)" LDFLAGS="$(LDFLAGS)" \
			$(CURDIR)/configure $($*_configure_flags)
	-mkdir -p $(STAMP_DIR)
	touch $@

configure: $(addprefix $(STAMP_DIR)/configure-stamp-, $(FLAVORS))

$(STAMP_DIR)/build-stamp-%: $(STAMP_DIR)/configure-stamp-%
	dh_testdir
	$(MAKE) -C $(builddir)
	touch $@

build: $(addprefix $(STAMP_DIR)/build-stamp-, $(FLAVORS))

$(STAMP_DIR)/install-stamp-%: $(STAMP_DIR)/build-stamp-%
	$(MAKE) -C $(builddir) install DESTDIR=$(CURDIR)/debian/libnss-myhostname
	touch $@

install: $(addprefix $(STAMP_DIR)/install-stamp-, $(FLAVORS))

clean:
	dh_testdir
	dh_testroot
	# remove install and build dirs
	rm -rf $(installbasedir)
	rm -rf $(buildbasedir)
	-rm -rf $(STAMP_DIR)
	dh_clean

binary-indep: build install

binary-arch: build install
	dh_testdir
	dh_testroot
	dh_installchangelogs -s
	dh_installdocs -s
	dh_install -s
	dh_installman -s
	dh_link -s
	dh_strip -s
	dh_compress -s
	dh_fixperms -s
	dh_makeshlibs -s
	dh_installdeb -s
	dh_shlibdeps -s -X/lib32 -X/lib64 
	dh_gencontrol -s
	dh_md5sums -s
	dh_builddeb -s

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