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

name = globus-proxy-utils
_name = globus_proxy_utils

INSTALLDIR = $(CURDIR)/debian/tmp
GLOBUSPACKAGEDIR = $(INSTALLDIR)$(_datadir)/globus/packages

_prefix = /usr
_bindir = $(_prefix)/bin
_sbindir = $(_prefix)/sbin
_libdir = $(_prefix)/lib/$(DEB_HOST_MULTIARCH)
_datadir = $(_prefix)/share
_mandir = $(_datadir)/man
_docdir = $(_datadir)/doc/$(name)

DEB_HOST_ARCH_BITS ?= $(shell dpkg-architecture -qDEB_HOST_ARCH_BITS)
DEB_HOST_MULTIARCH ?= $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)

ifeq ($(DEB_HOST_ARCH_BITS), 64)
flavor = gcc64-$(DEB_HOST_MULTIARCH)
else
flavor = gcc32-$(DEB_HOST_MULTIARCH)
endif

configure: configure-stamp

configure-stamp:
	dh_testdir

	dh_autotools-dev_updateconfig

	/usr/share/globus/globus-bootstrap.sh

	./configure \
	   --disable-static \
	   --with-flavor=$(flavor) \
	   --with-docdir=$(_docdir)

	# Reduce overlinking
	sed 's!CC -shared !CC \\$${wl}--as-needed -shared !g' -i libtool

	touch $@

build: build-arch build-indep

build-arch: build-stamp

build-indep:

build-stamp: configure-stamp
	dh_testdir

	$(MAKE)

	touch $@

clean:
	dh_testdir
	dh_testroot

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

	dh_autotools-dev_restoreconfig

	# Remove autogenerated files

	rm -f aclocal.m4
	rm -f compile
	rm -f configure
	rm -f install-sh
	rm -f ltmain.sh
	rm -f missing
	rm -f mkinstalldirs

	rm -f doxygen/Doxyfile*
	rm -f doxygen/Makefile.am
	rm -f pkgdata/Makefile.am
	rm -f pkgdata/*.filelist
	rm -f pkgdata/*.gpt
	rm -f pkgdata/$(name).pc
	rm -f pkgdata/pkg_data_src.pc.in
	rm -f gptdata.sh
	rm -f globus_automake*
	rm -rf autom4te.cache

	find . -name Makefile.in -exec rm {} ';'

	rm -f build-stamp configure-stamp

	dh_clean debian/*.install

install: build-stamp
	dh_testdir
	dh_testroot
	dh_prep

	$(MAKE) install DESTDIR=$(INSTALLDIR)

	# Remove installed license file
	rm -f $(INSTALLDIR)$(_docdir)/GLOBUS_LICENSE
	sed /GLOBUS_LICENSE/d \
	  -i $(GLOBUSPACKAGEDIR)/$(_name)/noflavor_doc.filelist

	# Generate package filelists
	cat $(GLOBUSPACKAGEDIR)/$(_name)/$(flavor)_pgm.filelist \
	    $(GLOBUSPACKAGEDIR)/$(_name)/noflavor_doc.filelist \
	  | sed s!^!debian/tmp$(_prefix)! > debian/$(name).install

binary: binary-arch binary-indep

binary-arch: install
	dh_testdir
	dh_testroot
	dh_installdocs debian/README
	dh_installchangelogs
	dh_install --fail-missing
	dh_installman
	dh_link
	dh_strip --dbg-package=$(name)-dbg
	dh_compress
	dh_fixperms
	dh_perl
	dh_makeshlibs
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb

binary-indep:

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