#!/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

include /usr/share/quilt/quilt.make

name = globus-core
_name = globus_core
version = 5.17

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

perl_vendorlib = $(shell eval "`perl -V:installvendorlib`"; echo $$installvendorlib)

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
enable64 = yes
else
flavor = gcc32pthr
enable64 = no
endif

configure: configure-stamp
	:

configure-stamp: $(QUILT_STAMPFN)
	dh_testdir

	./bootstrap

	for f in config.sub config.guess ; do \
	  if [ -e /usr/share/misc/$$f -a ! -e config/$$f.dist ] ; then \
	    mv config/$$f config/$$f.dist ; \
	    cp -f /usr/share/misc/$$f config/$$f ; \
	  fi ; \
	done

	CFLAGS="$(CFLAGS)" CXXFLAGS="$(CFLAGS)" FFLAGS="$(CFLAGS)" \
	LDFLAGS="$(LDFLAGS) -Wl,-z,defs" ./configure \
	   --host=$(DEB_HOST_GNU_TYPE) \
	   --build=$(DEB_BUILD_GNU_TYPE) \
	   --prefix=$(_prefix) \
	   --exec-prefix='$${prefix}' \
	   --mandir='$${datadir}/man' \
	   --infodir='$${datadir}/info' \
	   --includedir='$${prefix}/include/globus' \
	   --libexecdir='$${datadir}/globus' \
	   --with-threads=pthreads --with-thread-library=$(_libdir) \
	   --with-flavor=$(flavor) \
	   --enable-64bit=$(enable64) \
	   --with-setupdir='$${datadir}/globus/setup' \
	   --with-testdir='$${datadir}/globus/test/$${PACKAGE}' \
	   --with-flavorincludedir='$${libdir}/globus/include' \
	   --with-perlmoduledir=$(perl_vendorlib) \
	   --with-doxygendir='$${datadir}/globus/doxygen' \
	   --with-newgpt

	touch $@

build: build-stamp
	:

build-stamp: configure-stamp
	dh_testdir

	$(MAKE)

	touch $@

unpatch: cleanup

clean: unpatch
	:

cleanup:
	dh_testdir
	dh_testroot

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

	for f in config.sub config.guess ; do \
	  if [ -e config/$$f.dist ] ; then \
	    mv -f config/$$f.dist config/$$f ; \
	  fi ; \
	done

	# Remove autogenerated files

	rm -f aclocal.m4
	rm -f configure
	rm -f config/config.guess
	rm -f config/config.sub
	rm -f config/ltmain.sh
	rm -f config/mkinstalldirs

	rm -f pkgdata/*.filelist
	rm -f pkgdata/*.gpt
	rm -f aclocal/globus_automake*
	rm -rf autom4te.cache

	rm -f scripts/globus-build-env-*.sh

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

	rm -f build-stamp configure-stamp

	dh_clean debian/*.install

install: build-stamp
	dh_testdir
	dh_testroot
	dh_clean -k

	$(MAKE) install DESTDIR=$(INSTALLDIR)

	# These scripts are intended to be sourced, not executed
	chmod -x $(INSTALLDIR)$(_datadir)/globus/globus-build-env-*.sh

	# Add -Wl,--as-needed to the libtool script
	sed 's! -shared ! \\$${wl}--as-needed&!g' \
	  -i $(INSTALLDIR)$(_sbindir)/globus-libtool-$(flavor)

	# Install the GPT metadata to pkg-config file converter
	install debian/globus-gpt2pkg-config \
	  $(INSTALLDIR)$(_datadir)/globus/globus-gpt2pkg-config

	# Generate pkg-config file from GPT metadata
	mkdir -p $(INSTALLDIR)$(_libdir)/pkgconfig
	$(INSTALLDIR)$(_datadir)/globus/globus-gpt2pkg-config \
	  pkgdata/pkg_data_$(flavor)_dev.gpt > \
	  $(INSTALLDIR)$(_libdir)/pkgconfig/$(name).pc

	# Don't use /usr/bin/env
	sed 's!/usr/bin/env perl!/usr/bin/perl!' \
	  -i $(INSTALLDIR)$(_sbindir)/globus-*

	# Remove license file installed directly in the buildroot
	rm -f $(INSTALLDIR)/GLOBUS_LICENSE
	sed /GLOBUS_LICENSE/d \
	  -i $(GLOBUSPACKAGEDIR)/$(_name)/noflavor_data.filelist

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

	echo debian/tmp$(_datadir)/globus/globus-gpt2pkg-config \
	  >> debian/$(name).install

	echo debian/tmp$(_libdir)/pkgconfig/$(name).pc \
	  >> debian/$(name).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_lintian
	dh_makeshlibs
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb

binary: binary-indep binary-arch
	:

get-orig-source:
	if [ -r $(name)_$(version).orig.tar.gz ] ; then \
	  echo "$(name)_$(version).orig.tar.gz already exists." ; \
	else \
	  if [ ! -r gt5.0.2-all-source-installer.tar.bz2 ] ; then \
	    echo "Downloading Globus Toolkit release." ; \
	    echo "This should be necessary only once for all globus packages." ; \
	    wget -N http://www-unix.globus.org/ftppub/gt5/5.0/5.0.2/installers/src/gt5.0.2-all-source-installer.tar.bz2 ; \
	  fi ; \
	  echo "Extracting relevant parts of the Globus Toolkit release." ; \
	  tar -xjf gt5.0.2-all-source-installer.tar.bz2 \
	    gt5.0.2-all-source-installer/source-trees/core/source ; \
	  mv gt5.0.2-all-source-installer/source-trees/core/source \
	    $(name)-$(version) ; \
	  echo "Packing it up again." ; \
	  GZIP=-9 tar --exclude=CVS -czf $(name)_$(version).orig.tar.gz \
	    $(name)-$(version) ; \
	  echo "Cleaning up." ; \
	  rm -rf gt5.0.2-all-source-installer $(name)-$(version) ; \
	fi

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