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

INSTALLDIR = $(CURDIR)/debian/tmp

_prefix = /opt/nordugrid

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

configure: configure-stamp
	:

configure-stamp:
	dh_testdir

	# Using LDFLAGS="-Wl,-z,defs" does not work

	CFLAGS="$(CFLAGS)" CXXFLAGS="$(CFLAGS)" FFLAGS="$(CFLAGS)" \
	./configure \
	   --host=$(DEB_HOST_GNU_TYPE) \
	   --build=$(DEB_BUILD_GNU_TYPE) \
	   --prefix=$(_prefix) --disable-pkgdirs \
	   --with-sysv-scripts-location=/etc/init.d \
	   --with-monitor-prefix=$(_prefix)/share/gridmonitor \
	   --with-cron-scripts-prefix=/etc/cron.d \
	   --with-profile-scripts-prefix=/etc/profile.d \
	   --enable-docs

	touch $@

build: build-stamp
	:

build-stamp: configure
	dh_testdir

	$(MAKE)

	touch $@

clean: cleanup
	:

cleanup:
	dh_testdir
	dh_testroot

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

	rm -f configure-stamp build-stamp

	dh_clean debian/*.install

install: build
	dh_testdir
	dh_testroot
	dh_clean -k

	sourcename=`head -1 debian/changelog|cut -f1 -d\ `; \
	for i in ./build/*.filelist ; do \
	  module=`basename $$i .filelist`; \
	  echo -n "Preparing filelist for module '$$module' " ; \
	  ./build/mkfilelist $$i deb > \
	    ./debian/$$sourcename-$$module.install && \
	    echo ok || echo failed ; \
	done
	echo "Completed all file lists"

	$(MAKE) DESTDIR=$(INSTALLDIR) install

	rm -f $(INSTALLDIR)$(_prefix)/lib/*plugin*.a
	rm -f $(INSTALLDIR)$(_prefix)/lib/*plugin*.la
	rm -f $(INSTALLDIR)$(_prefix)/lib/*logger*.a
	rm -f $(INSTALLDIR)$(_prefix)/lib/*logger*.la
	rm -f $(INSTALLDIR)$(_prefix)/lib/*se*.a
	rm -f $(INSTALLDIR)$(_prefix)/lib/*se*.la
	rm -f $(INSTALLDIR)$(_prefix)/lib/*srm*.a
	rm -f $(INSTALLDIR)$(_prefix)/lib/*srm*.la
	rm -f $(INSTALLDIR)$(_prefix)/lib/*arc-infoindex-slapd-wrapper*.a
	rm -f $(INSTALLDIR)$(_prefix)/lib/*arc-infoindex-slapd-wrapper*.la
	rm -f $(INSTALLDIR)$(_prefix)/lib/python*/site-packages/*arclib*.a
	rm -f $(INSTALLDIR)$(_prefix)/lib/python*/site-packages/*arclib*.la
	rm -f $(INSTALLDIR)$(_prefix)/lib/python*/site-packages/*arclib*.pyc
	rm -f $(INSTALLDIR)$(_prefix)/lib/python*/site-packages/*arclib*.pyo


binary-indep:
	:

binary-arch: build 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_pysupport
	dh_makeshlibs
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb

binary: binary-indep binary-arch
	:

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