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

name = globus-gram-job-manager-fork
_name = globus_gram_job_manager_fork

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)

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

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

	MPIEXEC=no \
	MPIRUN=no \
	./configure \
	   --disable-static \
	   --with-flavor=$(flavor) \
	   --with-docdir=$(_docdir) \
	   --with-globus-state-dir=/var/log/globus

	# 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

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)

	# This library is opened using lt_dlopenext, so the libtool archive
	# (.la file) can not be removed - fix libdir and clear dependency_libs
	# ... and move it to the main package
	for lib in `find $(INSTALLDIR)$(_libdir) -name 'lib*.la'` ; do \
	  sed -e "s!^libdir=.*!libdir=\'$(_libdir)\'!" \
	      -e "s!^dependency_libs=.*!dependency_libs=\'\'!" -i $$lib ; \
	done
	grep 'lib.*\.la$$' $(GLOBUSPACKAGEDIR)/$(_name)/$(flavor)_dev.filelist \
	  >> $(GLOBUSPACKAGEDIR)/$(_name)/$(flavor)_rtl.filelist
	sed '/lib.*\.la$$/d' \
	  -i $(GLOBUSPACKAGEDIR)/$(_name)/$(flavor)_dev.filelist

	# Remove jobmanager-fork from install dir - leave it for admin config
	rm $(INSTALLDIR)/etc/grid-services/jobmanager-fork

	# Move script man pages to progs package
	grep '.[18]$$' $(GLOBUSPACKAGEDIR)/$(_name)/noflavor_doc.filelist \
	  >> $(GLOBUSPACKAGEDIR)/$(_name)/$(flavor)_pgm.filelist
	sed '/.[18]$$/d' -i $(GLOBUSPACKAGEDIR)/$(_name)/noflavor_doc.filelist

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

	# Devel package is redundant
	rm $(INSTALLDIR)$(_libdir)/libglobus_seg_fork.so
	rm $(INSTALLDIR)$(_libdir)/pkgconfig/globus-gram-job-manager-fork.pc
	rm $(GLOBUSPACKAGEDIR)/$(_name)/$(flavor)_dev.filelist
	rm $(GLOBUSPACKAGEDIR)/$(_name)/pkg_data_$(flavor)_dev.gpt

	# List config files in each package - drop the file list
	rm $(GLOBUSPACKAGEDIR)/$(_name)/noflavor_data.filelist
	rm $(GLOBUSPACKAGEDIR)/$(_name)/pkg_data_noflavor_data.gpt

	# Generate package filelists
	cat $(GLOBUSPACKAGEDIR)/$(_name)/$(flavor)_rtl.filelist \
	  | sed -e s!^!debian/tmp$(_prefix)! -e /fork.pm/d \
	  > debian/$(name)-setup-seg.install
	cat $(GLOBUSPACKAGEDIR)/$(_name)/$(flavor)_pgm.filelist \
	    $(GLOBUSPACKAGEDIR)/$(_name)/noflavor_doc.filelist \
	  | sed s!^!debian/tmp$(_prefix)! > debian/$(name).install

	echo debian/tmp$(perl_vendorlib)/Globus >> debian/$(name).install
	echo debian/tmp/etc/globus/globus-fork.conf >> debian/$(name).install
	echo debian/tmp/etc/grid-services/available/jobmanager-fork-poll \
	  > debian/$(name)-setup-poll.install
	echo debian/tmp/etc/grid-services/available/jobmanager-fork-seg \
	  >> debian/$(name)-setup-seg.install
	echo debian/tmp/etc/globus/scheduler-event-generator/available/fork \
	  >> debian/$(name)-setup-seg.install

binary: binary-arch binary-indep

binary-arch: install
	dh_testdir
	dh_testroot
	dh_installdocs -a
	dh_installchangelogs -a
	dh_install -a --fail-missing
	dh_installman -a
	dh_lintian -a
	dh_link -a
	dh_strip -X/bin/ -X/sbin/ --dbg-package=$(name)-setup-dbg
	dh_strip -X/lib/ --dbg-package=$(name)-dbg
	dh_compress -a
	dh_fixperms -a
	dh_perl -a
	dh_makeshlibs -a
	dh_installdeb -a
	dh_shlibdeps -a
	dh_gencontrol -a
	dh_md5sums -a
	dh_builddeb -a

binary-indep: install
	dh_testdir
	dh_testroot
	dh_installdocs -i debian/README
	dh_installchangelogs -i
	dh_install -i --fail-missing
	dh_installman -i
	dh_link -i
	dh_compress -i
	dh_fixperms -i
	dh_perl -i
	dh_installdeb -i
	dh_gencontrol -i
	dh_md5sums -i
	dh_builddeb -i

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