#!/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 = /usr
_bindir = $(_prefix)/bin
_sbindir = $(_prefix)/sbin
_libdir = $(_prefix)/lib
_includedir = $(_prefix)/include
_sysconfdir = /etc
_localstatedir = /var
_initrddir = $(_sysconfdir)/init.d
_datadir = $(_prefix)/share
_mandir = $(_datadir)/man
_docdir = $(_datadir)/doc

VERSION=1.8.1.2

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

configure: configure-stamp
	:

configure-stamp: $(QUILT_STAMPFN)
	dh_testdir

	for f in config/site.def config/linux.cf \
		 config/Library.tmpl config/Project.tmpl ; do \
	    [ ! -e $$f.save ] && cp -p $$f $$f.save || : ; \
	done

	# The code violates the strict aliasing rules all over the place...
	# Need to use -fnostrict-aliasing so that the -O2 optimization in
	# CFLAGS doesn't try to use them.
	sed 's/^CC +=/& $(CFLAGS) -fno-strict-aliasing -Wl,-z,defs/' \
	    -i config/linux.cf

	gsoapversion=`soapcpp2 -v 2>&1 | grep C++ | sed 's/.* //'` && \
	./configure dpm --with-postgres \
		--libdir=lib \
		--with-gsoap-version=$$gsoapversion \
		--with-dpm-config-file=$(_sysconfdir)/DPMCONFIG \
		--with-id-map-file=$(_sysconfdir)/lcgdm-mapfile \
		--with-ns-config-file=$(_sysconfdir)/DPNSCONFIG \
		--with-sysconf-dir='$$(prefix)/../etc' \
		--with-emi \
	        --without-argus

	sed -e 's/\(BuildDPMClient	*\)YES/\1NO/' \
	    -e 's/\(BuildInterfaces	*\)YES/\1NO/' \
	    -e 's/\(BuildNameServerClient	*\)YES/\1NO/' \
	    -e 's/\(BuildNameServerLibrary	*\)YES/\1NO/' \
	    -e 's/\(BuildRfioClient	*\)YES/\1NO/' \
	    -e 's/\(BuildRfioServer	*\)YES/\1NO/' -i config/site.def

	sed '/^\#define.*YES/d' -i config/Project.tmpl config/Library.tmpl
	sed '/^SECURITYDIR =/d' -i config/Project.tmpl

	make -f Makefile.ini Makefiles

	cd shlib && ln -s /usr/lib/liblcgdm.so* .
	cd shlib && ln -s /usr/lib/libdpm.so* .

	touch $@

build: build-indep build-arch
	:

build-indep: build-stamp
	:

build-arch: build-stamp
	:

build-stamp: configure-stamp
	dh_testdir

	$(MAKE) SOAPFLG="$(shell pkg-config --cflags gsoap)" \
		POSINC=-I/usr/include/postgresql

	touch $@

unpatch: cleanup

clean: unpatch
	:

cleanup:
	dh_testdir
	dh_testroot

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

	for f in config/site.def config/linux.cf \
		 config/Library.tmpl config/Project.tmpl ; do \
	    [ -e $$f.save ] && mv $$f.save $$f || : ; \
	done

	rm -f imake/imake imake/imake.o imake/ccimake

	find . '(' -name Makefile -a '!' -path */scripts/* ')' -exec rm {} ';'

	rm -f config.status

	rm -f dpmcopy/dpm_libpq_ifce.c
	rm -f dpmcopy/dpm_copyfile.c

	rm -f h/patchlevel.h
	rm -f NSCONFIG

	rm -f shlib/liblcgdm.so* shlib/libdpm.so*

	rm -f build-stamp configure-stamp

	dh_clean

install: build-stamp
	dh_testdir
	dh_testroot
	dh_clean -k

	$(MAKE) SOAPFLG="$(shell pkg-config --cflags gsoap)" \
		prefix=$(INSTALLDIR)$(_prefix) install install.man

	mv $(INSTALLDIR)$(_datadir)/DPM $(INSTALLDIR)$(_datadir)/dpm

	mkdir -p $(INSTALLDIR)$(_libdir)/dpm-postgres
	mkdir -p $(INSTALLDIR)$(_sysconfdir)/dpm-postgres

	# dpm startup script
	sed -e 's/LD_LIBRARY_PATH=$$LD_LIBRARY_PATH //' \
	    -e '/LD_LIBRARY_PATH/d' \
	    -e 's!\$$PREFIX/bin!\$$PREFIX/sbin!' \
	    -e 's!\$$PREFIX/etc!/etc!' \
	    $(INSTALLDIR)$(_datadir)/dpm/rc.dpm > \
	    $(INSTALLDIR)$(_sysconfdir)/dpm-postgres/dpm.init
	chmod 755 $(INSTALLDIR)$(_sysconfdir)/dpm-postgres/dpm.init
	rm $(INSTALLDIR)$(_datadir)/dpm/rc.dpm

	# dpm configuration file
	sed -e 's/\(^DPNS_HOST=\).*/\1`hostname -f`/' \
	    -e 's/\(^RUN_DPMDAEMON=\).*/\1"no"/' \
	    $(INSTALLDIR)$(_sysconfdir)/dpm.conf.templ > \
	    $(INSTALLDIR)$(_sysconfdir)/dpm-postgres/dpm.conf
	rm $(INSTALLDIR)$(_sysconfdir)/dpm.conf.templ

	# dpm log directory and log rotation configuration
	mkdir -p $(INSTALLDIR)$(_localstatedir)/log/dpm
	install -m 644 dpm/dpm.logrotate \
	    $(INSTALLDIR)$(_sysconfdir)/dpm-postgres/dpm.logrotate

	# dpm binary and man page
	mv $(INSTALLDIR)$(_bindir)/dpm \
	   $(INSTALLDIR)$(_libdir)/dpm-postgres/dpm
	sed -e 's/\(\.TH [^ ]* \)1/\18/' \
	    -e 's/dpm-shutdown(1)/dpm-shutdown(8)/g' \
	    $(INSTALLDIR)$(_mandir)/man1/dpm.1 | gzip -9 -n -c > \
	    $(INSTALLDIR)$(_libdir)/dpm-postgres/dpm.8.gz
	rm $(INSTALLDIR)$(_mandir)/man1/dpm.1

	mv $(INSTALLDIR)$(_sysconfdir)/DPMCONFIG.templ \
	   $(INSTALLDIR)$(_libdir)/dpm-postgres/DPMCONFIG.templ

	# dpm-shutdown binary and man page
	mv $(INSTALLDIR)$(_bindir)/dpm-shutdown \
	   $(INSTALLDIR)$(_libdir)/dpm-postgres/dpm-shutdown
	sed -e 's/\(\.TH [^ ]* \)1/\18/' \
	    $(INSTALLDIR)$(_mandir)/man1/dpm-shutdown.1 | gzip -9 -n -c > \
	    $(INSTALLDIR)$(_libdir)/dpm-postgres/dpm-shutdown.8.gz
	rm $(INSTALLDIR)$(_mandir)/man1/dpm-shutdown.1

	# dpnsdaemon startup script
	sed -e 's/LD_LIBRARY_PATH=$$LD_LIBRARY_PATH //' \
	    -e '/LD_LIBRARY_PATH/d' \
	    -e 's!\$$PREFIX/bin!\$$PREFIX/sbin!' \
	    -e 's!\$$PREFIX/etc!/etc!' \
	    -e 's!/etc/NSCONFIG!/etc/DPNSCONFIG!g' \
	    $(INSTALLDIR)$(_datadir)/dpm/rc.dpnsdaemon > \
	    $(INSTALLDIR)$(_sysconfdir)/dpm-postgres/dpnsdaemon.init
	chmod 755 $(INSTALLDIR)$(_sysconfdir)/dpm-postgres/dpnsdaemon.init
	rm $(INSTALLDIR)$(_datadir)/dpm/rc.dpnsdaemon

	# dpnsdaemon configuration file
	sed -e 's!/etc/NSCONFIG!/etc/DPNSCONFIG!g' \
	    -e 's/\(^RUN_DPNSDAEMON=\).*/\1"no"/' \
	    $(INSTALLDIR)$(_sysconfdir)/dpnsdaemon.conf.templ > \
	    $(INSTALLDIR)$(_sysconfdir)/dpm-postgres/dpnsdaemon.conf
	rm $(INSTALLDIR)$(_sysconfdir)/dpnsdaemon.conf.templ

	# dpnsdaemon log directory and log rotation configuration
	mkdir -p $(INSTALLDIR)$(_localstatedir)/log/dpns
	install -m 644 ns/dpnsdaemon.logrotate \
	    $(INSTALLDIR)$(_sysconfdir)/dpm-postgres/dpnsdaemon.logrotate

	# dpnsdaemon binary and man page
	mv $(INSTALLDIR)$(_bindir)/dpnsdaemon \
	   $(INSTALLDIR)$(_libdir)/dpm-postgres/dpnsdaemon
	sed -e 's/\(\.TH [^ ]* \)1/\18/' \
	    -e 's!/opt/lcg!!g' \
	    -e 's!/etc/NSCONFIG!/etc/DPNSCONFIG!g' \
	    -e 's/dpns-shutdown(1)/dpns-shutdown(8)/g' \
	    $(INSTALLDIR)$(_mandir)/man1/dpnsdaemon.1 | gzip -9 -n -c > \
	    $(INSTALLDIR)$(_libdir)/dpm-postgres/dpnsdaemon.8.gz
	rm $(INSTALLDIR)$(_mandir)/man1/dpnsdaemon.1

	mv $(INSTALLDIR)$(_sysconfdir)/NSCONFIG.templ \
	   $(INSTALLDIR)$(_libdir)/dpm-postgres/DPNSCONFIG.templ

	# dpns-shutdown binary and man page
	mv $(INSTALLDIR)$(_bindir)/dpns-shutdown \
	   $(INSTALLDIR)$(_libdir)/dpm-postgres/dpns-shutdown
	sed -e 's/\(\.TH [^ ]* \)1/\18/' \
	    $(INSTALLDIR)$(_mandir)/man1/dpns-shutdown.1 | gzip -9 -n -c > \
	    $(INSTALLDIR)$(_libdir)/dpm-postgres/dpns-shutdown.8.gz
	rm $(INSTALLDIR)$(_mandir)/man1/dpns-shutdown.1

	# dpmcopyd startup script
	sed -e 's/LD_LIBRARY_PATH=$$LD_LIBRARY_PATH //' \
	    -e '/LD_LIBRARY_PATH/d' \
	    -e 's!\$$PREFIX/bin!\$$PREFIX/sbin!' \
	    -e 's!\$$PREFIX/etc!/etc!' \
	    $(INSTALLDIR)$(_datadir)/dpm/rc.dpmcopyd > \
	    $(INSTALLDIR)$(_sysconfdir)/dpm-postgres/dpmcopyd.init
	chmod 755 $(INSTALLDIR)$(_sysconfdir)/dpm-postgres/dpmcopyd.init
	rm $(INSTALLDIR)$(_datadir)/dpm/rc.dpmcopyd

	# dpmcopyd configuration file
	sed -e 's/\(^DPNS_HOST=\).*/\1`hostname -f`/' \
	    -e 's/\(^DPM_HOST=\).*/\1`hostname -f`/' \
	    -e 's/\(^RUN_DPMCOPYDAEMON=\).*/\1"no"/' \
	    $(INSTALLDIR)$(_sysconfdir)/dpmcopyd.conf.templ > \
	    $(INSTALLDIR)$(_sysconfdir)/dpm-postgres/dpmcopyd.conf
	rm $(INSTALLDIR)$(_sysconfdir)/dpmcopyd.conf.templ

	# dpmcopyd log directory and log rotation configuration
	mkdir -p $(INSTALLDIR)$(_localstatedir)/log/dpmcopy
	install -m 644 dpmcopy/dpmcopyd.logrotate \
	    $(INSTALLDIR)$(_sysconfdir)/dpm-postgres/dpmcopyd.logrotate

	# dpmcopyd binary and man page
	mv $(INSTALLDIR)$(_bindir)/dpmcopyd \
	   $(INSTALLDIR)$(_libdir)/dpm-postgres/dpmcopyd
	sed -e 's/\(\.TH [^ ]* \)1/\18/' \
	    $(INSTALLDIR)$(_mandir)/man1/dpmcopyd.1 | gzip -9 -n -c > \
	    $(INSTALLDIR)$(_libdir)/dpm-postgres/dpmcopyd.8.gz
	rm $(INSTALLDIR)$(_mandir)/man1/dpmcopyd.1

	for svc in srmv1 srmv2 srmv2.2 ; do \
	    sed -e 's/LD_LIBRARY_PATH=$$LD_LIBRARY_PATH //' \
		-e '/LD_LIBRARY_PATH/d' \
		-e 's!\$$PREFIX/bin!\$$PREFIX/sbin!' \
		-e 's!\$$PREFIX/etc!/etc!' \
		-e "s/$${svc}/dpm-$${svc}/g" \
		$(INSTALLDIR)$(_datadir)/dpm/rc.$${svc} > \
		$(INSTALLDIR)$(_sysconfdir)/dpm-postgres/dpm-$${svc}.init ; \
	    chmod 755 $(INSTALLDIR)$(_sysconfdir)/dpm-postgres/dpm-$${svc}.init ; \
	    rm $(INSTALLDIR)$(_datadir)/dpm/rc.$${svc} ; \
	    sed -e "s/$${svc}/dpm-$${svc}/g" \
		-e 's/\(^DPNS_HOST=\).*/\1`hostname -f`/' \
		-e 's/\(^DPM_HOST=\).*/\1`hostname -f`/' \
		-e 's/\(^RUN_SRMV1DAEMON=\).*/\1"no"/' \
		-e 's/\(^RUN_SRMV2DAEMON=\).*/\1"no"/' \
		$(INSTALLDIR)$(_sysconfdir)/$${svc}.conf.templ > \
		$(INSTALLDIR)$(_sysconfdir)/dpm-postgres/dpm-$${svc}.conf ; \
	    rm $(INSTALLDIR)$(_sysconfdir)/$${svc}.conf.templ ; \
	    mkdir -p $(INSTALLDIR)$(_localstatedir)/log/dpm-$${svc} ; \
	    sed -e "s/$${svc}/dpm-$${svc}/g" $${svc}/$${svc}.logrotate > \
		$(INSTALLDIR)$(_sysconfdir)/dpm-postgres/dpm-$${svc}.logrotate ; \
	    mv $(INSTALLDIR)$(_bindir)/$${svc} \
	       $(INSTALLDIR)$(_libdir)/dpm-postgres/dpm-$${svc} ; \
	    sed -e 's/\.TH \([^ ]*\) 1/.TH DPM-\1 8/' \
		-e 's/dpm(1)/dpm(8)/g' \
		$(INSTALLDIR)$(_mandir)/man1/$${svc}.1 | gzip -9 -n -c > \
		$(INSTALLDIR)$(_libdir)/dpm-postgres/dpm-$${svc}.8.gz ; \
	    rm $(INSTALLDIR)$(_mandir)/man1/$${svc}.1 ; \
	done

	# Create dpm user home and certificate directories
	mkdir -p $(INSTALLDIR)$(_localstatedir)/lib/dpm
	mkdir -p $(INSTALLDIR)$(_sysconfdir)/grid-security/dpmmgr

	# This doesn't quite work...
	sed '/CREATE DATABASE/d' -i \
	    $(INSTALLDIR)$(_datadir)/dpm/create_dpm_tables_postgres.sql \
	    $(INSTALLDIR)$(_datadir)/dpm/create_dpns_tables_postgres.sql

	# Remove files already present in other packages
	rm -rf $(INSTALLDIR)$(_includedir)
	rm -rf $(INSTALLDIR)$(_mandir)/man3
	rm -rf $(INSTALLDIR)$(_mandir)/man4

	# Lintian overrides
	mkdir -p $(INSTALLDIR)/usr/share/lintian/overrides
	for pkg in dpm-postgres dpm-postgres-nameserver dpm-postgres-copyd \
	    dpm-postgres-srmv1 dpm-postgres-srmv2 dpm-postgres-srmv22 ; do \
		install -m 644 debian/$$pkg.lintian-overrides \
			$(INSTALLDIR)/usr/share/lintian/overrides/$$pkg ; \
	done

binary-indep:
	:

binary-arch: install
	dh_testdir
	dh_testroot
	dh_installchangelogs
	dh_installdocs
	dh_install --fail-missing
	dh_installman
	dh_link
	dh_strip
	dh_compress
	dh_fixperms
	dh_makeshlibs
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb

binary: binary-indep binary-arch
	:

get-orig-source:
	dest="../dpm-postgres_$(VERSION).orig.tar.gz" ; \
	if [ -r "$$dest" ] ; then \
	  echo "'$$dest' already exists." ; \
	else \
	  uversion=LCG-DM_R_`echo $(VERSION)|tr "." "_"`_emi ; \
	  echo "Fetching upstream svn version '$$uversion'" ; \
	  rm -rf dpm-postgres-$(VERSION) ; \
	  LANG=C svn co http://svnweb.cern.ch/guest/lcgdm/lcg-dm/tags/$$uversion dpm-postgres-$(VERSION) ; \
	  echo "Packing it up." ; \
	  GZIP=-9 tar --exclude .svn --exclude debian -z -c -f "$$dest" dpm-postgres-$(VERSION) ; \
	  echo "Cleaning up." ; \
	  rm -rf dpm-postgres-$(VERSION) ; \
	fi

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