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

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

include /usr/share/quilt/quilt.make

configure: configure-stamp
	:

configure-stamp: $(QUILT_STAMPFN)
	dh_testdir

	touch $@

build: build-stamp
	:

build-stamp: configure-stamp
	dh_testdir

	cd src && \
	MYCFLAGS="$(CFLAGS) -DLINUX=2 -D_REENTRANT -D_LARGEFILE64_SOURCE -I../interface -I/usr/include/apache2 -I/usr/include/apr-1.0 -fPIC" $(MAKE)

	touch $@

unpatch: cleanup

clean: unpatch
	:

cleanup:
	dh_testdir
	dh_testroot

	# Makefile has no clean target - need to clean manually
	rm -f src/findproxyfile
	rm -f src/gridsite-copy.cgi
	rm -f src/gridsite-storage.cgi
	rm -f src/gsexec
	rm -f src/htcp
	rm -f src/real-gridsite-admin.cgi
	rm -f src/urlencode
	rm -f src/*.o
	rm -f src/*.a
	rm -f src/*.so
	rm -f src/*.so.*
	rm -rf doc/doxygen
	rm -rf doc/*.1.html
	rm -rf doc/*.8.html
	rm -rf src/doxygen

	rm -f build-stamp configure-stamp

	dh_clean

install: build-stamp
	dh_testdir
	dh_testroot
	dh_clean -k

	cd src && \
	$(MAKE) install prefix=$(INSTALLDIR)$(_prefix)

	mv $(INSTALLDIR)$(_libdir)/httpd $(INSTALLDIR)$(_libdir)/apache2

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

	# This is not really useful
	rm -f $(INSTALLDIR)$(_libdir)/libgridsite_globus.*
	rm -f $(INSTALLDIR)$(_libdir)/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 -p $(INSTALLDIR)$(_localstatedir)/lib/gridsite
	install -m 0644 debian/gridsitehead.txt \
	   $(INSTALLDIR)$(_localstatedir)/lib/gridsite/gridsitehead.txt
	install -m 0644 debian/gridsitefoot.txt \
	   $(INSTALLDIR)$(_localstatedir)/lib/gridsite/gridsitefoot.txt
	install -m 0644 debian/root-level.gacl \
	   $(INSTALLDIR)$(_localstatedir)/lib/gridsite/.gacl

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

	# Copy in apache configuration, we must name it zgridsite.conf
	# so it is loaded after mod_ssl in ssl.conf
	mkdir -p $(INSTALLDIR)$(_sysconfdir)/apache2/mods-available
	install -m 0644 debian/gridsite.conf \
	   $(INSTALLDIR)$(_sysconfdir)/apache2/mods-available/zgridsite.conf
	install -m 0644 debian/gridsite.load \
	   $(INSTALLDIR)$(_sysconfdir)/apache2/mods-available/zgridsite.load
	mkdir -p $(INSTALLDIR)$(_sysconfdir)/apache2/sites-available
	install -m 0644 debian/gridsite.site \
	   $(INSTALLDIR)$(_sysconfdir)/apache2/sites-available/gridsite

	mkdir -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)$(_localstatedir)/lib/gridsite/.gacl \
	  $(INSTALLDIR)$(_localstatedir)/lib/gridsite/gridsitehead.txt \
	  $(INSTALLDIR)$(_localstatedir)/lib/gridsite/gridsitefoot.txt \
	  $(INSTALLDIR)$(_sysconfdir)/grid-security/dn-lists \
	  $(INSTALLDIR)$(_localstatedir)/cache/mod_gridsite
	chown root.www-data $(INSTALLDIR)$(_sbindir)/gsexec
	chmod 4754 $(INSTALLDIR)$(_sbindir)/gsexec

binary-indep:
	:

binary-arch: install
	dh_testdir
	dh_testroot
	dh_installchangelogs
	dh_installdocs
	dh_installexamples
	dh_install --fail-missing
	dh_installman
	dh_lintian
	dh_link
	dh_strip
	dh_compress
	dh_fixperms -X sbin/gsexec -X var/lib/gridsite \
		    -X var/cache/mod_gridsite -X etc/grid-security/dn-lists
	dh_makeshlibs
	dh_installdeb
	dh_shlibdeps -l debian/libgridsite1.7/usr/lib
	dh_gencontrol
	dh_md5sums
	dh_builddeb

binary: binary-indep binary-arch
	:

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