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

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

export SOURCE_DATE_EPOCH = \
	$(shell date -d "$$(dpkg-parsechangelog -c 1 -S Date)" +%s)

-include /usr/share/dpkg/buildflags.mk

INSTALLDIR = $(CURDIR)/debian/tmp

_prefix = /usr
_datadir = $(_prefix)/share
_docdir = $(_datadir)/doc

configure: configure-stamp

configure-stamp:
	dh_testdir

	[ ! -r src/Makefile.save ] && cp -p src/Makefile src/Makefile.save || :

	# Fix bad permissions
	find . '(' -name '*.h' -o -name '*.c' -o -name '*.cpp' -o \
		   -name '*.cc' ')' -exec chmod 644 {} ';'
	chmod 644 LICENSE RELEASE-NOTES

	# Remove -L/usr/lib and -L/usr/lib64 since they may cause problems
	sed -e 's!-L$$([A-Z_]*)/lib!!' \
	    -e 's!-L$$([A-Z_]*)/$$(LIBDIR)!!' -i src/Makefile

	# Remove gsoap version from library names
	sed -e 's!$$(GSOAP_VERSION)!!g' -i src/Makefile

	touch $@

build: build-indep build-arch

build-indep: build-stamp

build-arch: build-stamp

build-stamp: configure-stamp
	dh_testdir

	. ./VERSION && \
	cd src && \
	$(MAKE) CFLAGS="$(CFLAGS) $(CPPFLAGS) -fPIC -I. `pkg-config --cflags gsoap`" \
		GLOBUS_INCLUDE="-I/usr/include/globus -I/usr/lib/globus/include -I/usr/include/$(DEB_HOST_MULTIARCH)/globus" \
		SHLIBLDFLAGS="$(LDFLAGS) -shared" \
		USE_VOMS=yes WITH_EMI=yes WITH_CPP_LIBS=yes \
		LIBDIR=lib/$(DEB_HOST_MULTIARCH) VERSION=$$VERSION all doc

	touch $@

clean:
	dh_testdir
	dh_testroot

	. ./VERSION && \
	cd src && \
	$(MAKE) CFLAGS="$(CFLAGS) $(CPPFLAGS) -fPIC -I. `pkg-config --cflags gsoap`" \
		GLOBUS_INCLUDE="-I/usr/include/globus -I/usr/lib/globus/include -I/usr/include/$(DEB_HOST_MULTIARCH)/globus" \
		SHLIBLDFLAGS="$(LDFLAGS) -shared" \
		USE_VOMS=yes WITH_EMI=yes WITH_CPP_LIBS=yes \
		LIBDIR=lib/$(DEB_HOST_MULTIARCH) VERSION=$$VERSION clean

	rm -rf src/html
	rm -rf src/man
	rm -f src/doxygen_sqlite3.db

	rm -rf src/*.a
	rm -rf src/*.so

	[ -r src/Makefile.save ] && mv src/Makefile.save src/Makefile || :

	rm -f build-stamp configure-stamp

	dh_clean

install: build-stamp
	dh_testdir
	dh_testroot
	dh_prep

	# Yes, the makefile uses RPM_BUILD_ROOT instead of the normal DESTDIR

	. ./VERSION && \
	cd src && \
	$(MAKE) CFLAGS="$(CFLAGS) $(CPPFLAGS) -fPIC -I. `pkg-config --cflags gsoap`" \
		GLOBUS_INCLUDE="-I/usr/include/globus -I/usr/lib/globus/include -I/usr/include/$(DEB_HOST_MULTIARCH)/globus" \
		SHLIBLDFLAGS="$(LDFLAGS) -shared" \
		USE_VOMS=yes WITH_EMI=yes WITH_CPP_LIBS=yes \
		LIBDIR=lib/$(DEB_HOST_MULTIARCH) VERSION=$$VERSION \
		RPM_BUILD_ROOT=$(INSTALLDIR) install install.man

	mkdir -p $(INSTALLDIR)$(_docdir)/libcgsi-gsoap-dev
	mv $(INSTALLDIR)$(_docdir)/CGSI/html \
	   $(INSTALLDIR)$(_docdir)/libcgsi-gsoap-dev

binary-indep:

binary-arch: install
	dh_testdir
	dh_testroot
	dh_installchangelogs
	dh_installdocs
	dh_install --fail-missing
	dh_installman
	dh_lintian
	dh_link
	dh_strip --dbg-package=libcgsi-gsoap-dbg
	dh_compress
	dh_fixperms
	dh_makeshlibs
	dh_installdeb
	dh_shlibdeps -l debian/libcgsi-gsoap1/usr/lib

	if [ -r /usr/lib/$(DEB_HOST_MULTIARCH)/libgsoap.so ] ; then \
	GSOAPLIB=`readlink /usr/lib/$(DEB_HOST_MULTIARCH)/libgsoap.so` ; else \
	GSOAPLIB=`readlink /usr/lib/libgsoap.so` ; fi && \
	GSOAPPKG=`dpkg -S $$GSOAPLIB | sed 's/:.*//'` && \
	echo "gsoap:Depends=$$GSOAPPKG" >> debian/libcgsi-gsoap1.substvars

	dh_gencontrol
	dh_md5sums
	dh_builddeb

binary: binary-indep binary-arch

VERSION=1.3.8
VERSTAG=1_3_8
get-orig-source:
	git clone https://gitlab.cern.ch/dmc/cgsi-gsoap.git CGSI-gSOAP-$(VERSION)
	( cd CGSI-gSOAP-$(VERSION) ; \
	  git checkout cgsi-gsoap_R_$(VERSTAG) ; \
	  git submodule init ; \
	  git submodule update \
	)
	tar -c -z --exclude-vcs -f ../cgsi-gsoap_$(VERSION).orig.tar.gz CGSI-gSOAP-$(VERSION)
	rm -rf CGSI-gSOAP-$(VERSION)

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