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

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

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

INSTALLDIR = $(CURDIR)/debian/tmp

_prefix = /usr
_bindir = $(_prefix)/bin
_sbindir = $(_prefix)/sbin
_libdir = $(_prefix)/lib
_includedir = $(_prefix)/include
_sysconfdir = /etc
_localstatedir = /var
_datadir = $(_prefix)/share
_mandir = $(_datadir)/man
_docdir = $(_datadir)/doc

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

configure: configure-stamp

configure-stamp:
	dh_testdir

	touch $@

build: build-indep build-arch

build-indep: build-stamp

build-arch: build-stamp

build-stamp: configure-stamp
	dh_testdir

	cd src && \
	$(MAKE) build prefix=/usr libdir=lib/$(DEB_HOST_MULTIARCH) \
	    CFLAGS="$(CFLAGS) $(CPPFLAGS)" LDFLAGS="$(LDFLAGS)"

	touch $@

clean:
	dh_testdir
	dh_testroot

	cd src && \
	$(MAKE) clean

	rm -rf doc/doxygen
	rm -f doc/*.1.html
	rm -f doc/*.8.html
	rm -f src/doxygen_sqlite3.db

	rm -f build-stamp configure-stamp

	dh_clean

install: build-stamp
	dh_testdir
	dh_testroot
	dh_prep

	cd src && \
	$(MAKE) install install-ws DESTDIR=$(INSTALLDIR) \
		prefix=/usr libdir=lib/$(DEB_HOST_MULTIARCH)

	# Change cgi scripts location
	mkdir -m 0755 -p $(INSTALLDIR)$(_libdir)/gridsite/cgi-bin
	mv $(INSTALLDIR)$(_sbindir)/*.cgi \
	   $(INSTALLDIR)$(_libdir)/gridsite/cgi-bin
	rmdir $(INSTALLDIR)$(_sbindir)

	# Let dh_apache2 handle module installation
	rm -rf $(INSTALLDIR)$(_libdir)/$(DEB_HOST_MULTIARCH)/httpd

	# Let debhelper do the compression
	gunzip $(INSTALLDIR)$(_mandir)/man*/*

	# This is not really useful
	rm $(INSTALLDIR)$(_libdir)/$(DEB_HOST_MULTIARCH)/libgridsite_globus.*
	rm $(INSTALLDIR)$(_libdir)/$(DEB_HOST_MULTIARCH)/libgridsite_nossl.*

	# Remove these - docs are installed using the debian/*.docs files
	rm -rf $(INSTALLDIR)$(_datadir)/doc

	# Set up root area to serve files from
	mkdir -m 0755 -p $(INSTALLDIR)$(_localstatedir)/lib/gridsite
	mkdir -m 0755 -p $(INSTALLDIR)$(_sysconfdir)/gridsite
	install -m 0644 debian/gridsitehead.txt \
	   $(INSTALLDIR)$(_sysconfdir)/gridsite/gridsitehead.txt
	install -m 0644 debian/gridsitefoot.txt \
	   $(INSTALLDIR)$(_sysconfdir)/gridsite/gridsitefoot.txt
	install -m 0644 debian/root-level.gacl \
	   $(INSTALLDIR)$(_sysconfdir)/gridsite/.gacl

	mkdir -m 0755 -p $(INSTALLDIR)$(_sysconfdir)/grid-security/dn-lists
	mkdir -m 0755 -p $(INSTALLDIR)$(_sysconfdir)/grid-security/vomsdir
	mkdir -m 0755 -p $(INSTALLDIR)$(_localstatedir)/cache/mod_gridsite

	mkdir -m 0755 -p $(INSTALLDIR)$(_datadir)/apache2/icons
	uudecode -o $(INSTALLDIR)$(_datadir)/apache2/icons/gridsitelogo.png \
	   debian/gridsitelogo.png.uu

	chown www-data.www-data \
	  $(INSTALLDIR)$(_localstatedir)/lib/gridsite \
	  $(INSTALLDIR)$(_sysconfdir)/gridsite/.gacl \
	  $(INSTALLDIR)$(_sysconfdir)/gridsite/gridsitehead.txt \
	  $(INSTALLDIR)$(_sysconfdir)/gridsite/gridsitefoot.txt \
	  $(INSTALLDIR)$(_sysconfdir)/grid-security/dn-lists \
	  $(INSTALLDIR)$(_localstatedir)/cache/mod_gridsite

binary-indep: install
	dh_testdir
	dh_testroot
	dh_installdocs -i
	dh_installchangelogs -i
	dh_install -i
	dh_missing -i --fail-missing
	dh_lintian -i
	dh_link -i
	dh_compress -i
	dh_fixperms -i
	dh_installdeb -i
	dh_gencontrol -i
	dh_md5sums -i
	dh_builddeb -i

binary-arch: install
	dh_testdir
	dh_testroot
	dh_installdocs -a
	dh_installchangelogs -a
	dh_install -a
	dh_missing -a --fail-missing
	dh_apache2 -a
	dh_installman -a
	dh_lintian -a
	dh_link -a
	dh_strip -a --dbgsym-migration='gridsite-dbg (<< 3.0.0~), libgridsite-dbg (<< 3.0.0~)'
	dh_compress -a
	dh_fixperms -a -X var/lib/gridsite -X etc/gridsite \
		       -X var/cache/mod_gridsite -X etc/grid-security/dn-lists
	dh_makeshlibs -a
	dh_installdeb -a
	dh_shlibdeps -a
	dh_gencontrol -a
	dh_md5sums -a
	dh_builddeb -a

binary: binary-indep binary-arch

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