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

name = globus-libtool
_name = globus_libtool
version = 1.2

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

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

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

ifneq (,$(filter $(DEB_HOST_ARCH_CPU), amd64 alpha ia64 s390x ppc64 sparc64))
flavor = gcc64pthr
else
flavor = gcc32pthr
endif

configure: configure-stamp
	:

configure-stamp:
	dh_testdir

	touch $@

build: build-stamp
	:

build-stamp: configure-stamp
	dh_testdir

	LIBTOOL_VERSION=`libtoolize --version | head -1 | sed 's!.* !!'` ; \
	sed "s!@LIBTOOL_VERSION@!$$LIBTOOL_VERSION!g" \
	    < debian/$(name)-pkg_data_src.gpt.in > pkg_data_src.gpt

	echo /share/globus/packages/$(_name)/$(flavor)_rtl.filelist > \
	  $(flavor)_rtl.filelist
	echo /share/globus/packages/$(_name)/pkg_data_$(flavor)_rtl.gpt >> \
	  $(flavor)_rtl.filelist

	echo /share/globus/packages/$(_name)/$(flavor)_dev.filelist > \
	  $(flavor)_dev.filelist
	echo /share/globus/packages/$(_name)/pkg_data_$(flavor)_dev.gpt >> \
	  $(flavor)_dev.filelist

	/usr/sbin/gpt_generate_bin_pkg_data --flavor=$(flavor) pkg_data_src.gpt

	touch $@

clean: cleanup
	:

cleanup:
	dh_testdir
	dh_testroot

	rm -f *.filelist
	rm -f *.gpt

	rm -f build-stamp configure-stamp

	dh_clean debian/*.install

install: build-stamp
	dh_testdir
	dh_testroot
	dh_clean -k

	mkdir -p $(GLOBUSPACKAGEDIR)/$(_name)

	install -m 644 pkg_data_$(flavor)_rtl.gpt $(flavor)_rtl.filelist \
	  $(GLOBUSPACKAGEDIR)/$(_name)
	install -m 644 pkg_data_$(flavor)_dev.gpt $(flavor)_dev.filelist \
	  $(GLOBUSPACKAGEDIR)/$(_name)

	mkdir -p $(INSTALLDIR)$(_libdir)/pkgconfig
	/usr/share/globus/globus-gpt2pkg-config pkg_data_$(flavor)_dev.gpt > \
	  $(INSTALLDIR)$(_libdir)/pkgconfig/$(name).pc

	# Generate package filelists
	cat $(GLOBUSPACKAGEDIR)/$(_name)/$(flavor)_rtl.filelist \
	  | sed s!^!debian/tmp$(_prefix)! > debian/lib$(name).install
	cat $(GLOBUSPACKAGEDIR)/$(_name)/$(flavor)_dev.filelist \
	  | sed s!^!debian/tmp$(_prefix)! > debian/lib$(name)-dev.install

	echo debian/tmp$(_libdir)/pkgconfig/$(name).pc \
	  >> debian/lib$(name)-dev.install

binary-indep:
	:

binary-arch: install
	dh_testdir
	dh_testroot
	dh_installchangelogs
	dh_installdocs
	dh_install --fail-missing
	dh_installman
	dh_link
	dh_strip
	dh_compress
	dh_fixperms
	dh_perl
	dh_makeshlibs
	dh_installdeb
	dh_shlibdeps

	# Generate dependencies

	LIBLTDLDEV=`dpkg -S /usr/lib/libltdl.so | sed s/:.*//` && \
	LIBLTDLX=`ls -l /usr/lib/libltdl.so | sed 's/.*->.*lib/lib/'` && \
	LIBLTDL=`dpkg -S /usr/lib/$$LIBLTDLX | sed s/:.*//` && \
	echo libltdl:Depends=$$LIBLTDL >> debian/lib$(name).substvars && \
	echo libltdl:Depends=$$LIBLTDLDEV >> debian/lib$(name)-dev.substvars

	dh_gencontrol
	dh_md5sums
	dh_builddeb

binary: binary-indep binary-arch
	:

# The package comes with all that is needed
get-orig-source:
	:

.PHONY: build clean binary-indep binary-arch binary install configure get-orig-source
