#!/usr/bin/make -f
# -*- makefile -*-

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

ifneq (,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
NUMJOBS = $(patsubst parallel=%,%,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
MAKEFLAGS += -j$(NUMJOBS)
endif

INSTALLDIR = $(CURDIR)/debian/tmp

DEB_BUILD_ARCH_CPU ?= $(shell dpkg-architecture -qDEB_BUILD_ARCH_CPU)

configure: configure-stamp
	:

configure-stamp:
	dh_testdir

	CFLAGS="$(CFLAGS)" CXXFLAGS="$(CFLAGS)" FFLAGS="$(CFLAGS)" \
	LDFLAGS="-Wl,-z,defs" \
	./configure \
	   --disable-static \
	   --host=$(DEB_HOST_GNU_TYPE) \
	   --build=$(DEB_BUILD_GNU_TYPE) \
	   --prefix=/usr \
	   --sysconfdir=/etc \
	   --localstatedir=/var \
	   --mandir='$${datadir}/man' \
	   --infodir='$${datadir}/info' \
	   --with-docdir='$${datadir}/doc/nordugrid-arc-compat'

	touch $@

build: build-stamp
	:

build-stamp: configure
	dh_testdir

	$(MAKE)

	touch $@

clean: cleanup
	:

cleanup:
	dh_testdir
	dh_testroot

	if [ -r Makefile ] ; then $(MAKE) distclean ; fi

	rm -f configure-stamp build-stamp

	dh_clean

install: build
	dh_testdir
	dh_testroot
	dh_clean -k

	$(MAKE) DESTDIR=$(INSTALLDIR) install

	rm $(INSTALLDIR)/usr/lib/libarclib.so
	rm $(INSTALLDIR)/usr/share/doc/nordugrid-arc-compat/LICENSE

binary-indep:
	:

binary-arch: build install
	dh_testdir
	dh_testroot
	dh_installchangelogs
	dh_installdocs
	dh_install --fail-missing
	dh_installman
	dh_link
	dh_strip --dbg-package=nordugrid-arc-compat-dbg
	dh_pysupport
	dh_compress
	dh_fixperms
	dh_makeshlibs
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb

binary: binary-indep binary-arch
	:

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