#!/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
_initrddir = $(_sysconfdir)/init.d
_datadir = $(_prefix)/share
_mandir = $(_datadir)/man
_docdir = $(_datadir)/doc
_archlib = $(shell perl -MConfig -e 'print $$Config{vendorarch}')

VERSION=1.8.7

configure: configure-stamp
	:

configure-stamp:
	dh_testdir

	chmod 644 security/globus_gsi_gss_constants.h \
		  security/globus_i_gsi_credential.h \
		  security/gssapi_openssl.h
	chmod 644 doc/lfc/INSTALL-*

	# LFC with mysql backend

	mkdir lfc-mysql
	cp -pr Imakefile Makefile.ini config configure VERSION scripts \
	    h lib shlib common ns rfio dpm \
	    srmv1 srmv2 srmv2.2 security dli dicomcopy dpmcopy \
	    test lfc-mysql

	# 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 +=/& $(CPPFLAGS) $(CFLAGS) -fno-strict-aliasing/' \
	    -i lfc-mysql/config/linux.cf

	sed 's/^CXXFLAGS =/& $(CPPFLAGS) $(CXXFLAGS)/' \
	    -i lfc-mysql/common/Imakefile

	cd lfc-mysql && \
	gsoapversion=`soapcpp2 -v 2>&1 | grep C++ | sed 's/.* //'` && \
	./configure lfc --with-mysql \
		--libdir=lib \
		--with-gsoap-version=$$gsoapversion \
		--with-id-map-file=$(_sysconfdir)/lcgdm-mapfile \
		--with-ns-config-file=$(_sysconfdir)/NSCONFIG \
		--with-etc-dir='$$(prefix)/../etc' \
		--with-sysconf-dir=EtcDir/default \
		--with-initrc-dir=EtcDir/init.d \
		--with-emi \
		--without-argus

	sed 's!$$(prefix)/var!$$(prefix)/../var!' -i lfc-mysql/config/site.def

	cd lfc-mysql && \
	make -f Makefile.ini Makefiles

	# LFC with postgres backend

	mkdir lfc-postgres
	cp -pr Imakefile Makefile.ini config configure VERSION scripts \
	    h lib shlib common ns rfio dpm \
	    srmv1 srmv2 srmv2.2 security dli dicomcopy dpmcopy \
	    test lfc-postgres

	# 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 +=/& $(CPPFLAGS) $(CFLAGS) -fno-strict-aliasing/' \
	    -i lfc-postgres/config/linux.cf

	sed 's/^CXXFLAGS =/& $(CPPFLAGS) $(CXXFLAGS)/' \
	    -i lfc-postgres/common/Imakefile

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

	sed 's!$$(prefix)/var!$$(prefix)/../var!' -i lfc-postgres/config/site.def

	sed -e 's/\(BuildDLI	*\)YES/\1NO/' \
	    -e 's/\(BuildInterfaces	*\)YES/\1NO/' \
	    -e 's/\(BuildNameServerClient	*\)YES/\1NO/' \
	    -e 's/\(BuildNameServerLibrary	*\)YES/\1NO/' \
	    -i lfc-postgres/config/site.def
	sed '/^\#define.*YES/d' -i lfc-postgres/config/Project.tmpl \
				   lfc-postgres/config/Library.tmpl
	sed '/^SECURITYDIR =/d' -i lfc-postgres/config/Project.tmpl
	sed 's/\(install:\).*/\1/' -i lfc-postgres/h/Imakefile

	cd lfc-postgres && \
	make -f Makefile.ini Makefiles

	# DPM with mysql backend

	mkdir dpm-mysql
	cp -pr Imakefile Makefile.ini config configure VERSION scripts \
	    h lib shlib common ns rfio dpm \
	    srmv1 srmv2 srmv2.2 security dli dicomcopy dpmcopy \
	    test dpm-mysql

	# 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 +=/& $(CPPFLAGS) $(CFLAGS) -fno-strict-aliasing/' \
	    -i dpm-mysql/config/linux.cf

	sed 's/^CXXFLAGS =/& $(CPPFLAGS) $(CXXFLAGS)/' \
	    -i dpm-mysql/common/Imakefile

	cd dpm-mysql && \
	gsoapversion=`soapcpp2 -v 2>&1 | grep C++ | sed 's/.* //'` && \
	./configure dpm --with-mysql \
		--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-etc-dir='$$(prefix)/../etc' \
		--with-sysconf-dir=EtcDir/default \
		--with-initrc-dir=EtcDir/init.d \
		--with-emi \
		--without-argus

	sed 's!$$(prefix)/var!$$(prefix)/../var!' -i dpm-mysql/config/site.def

	sed -e '/^SECURITYDIR =/d' \
	    -e '/^COMMONDIR =/d' -i dpm-mysql/config/Project.tmpl
	sed -e 's/:.*(lcgdm).*/:/' -i dpm-mysql/lib/Imakefile
	sed -e 's/:.*(lcgdm).*/:/' -i dpm-mysql/shlib/Imakefile

	cd dpm-mysql && \
	make -f Makefile.ini Makefiles

	# DPM with postgres backend

	mkdir dpm-postgres
	cp -pr Imakefile Makefile.ini config configure VERSION scripts \
	    h lib shlib common ns rfio dpm \
	    srmv1 srmv2 srmv2.2 security dli dicomcopy dpmcopy \
	    test dpm-postgres

	# 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 +=/& $(CPPFLAGS) $(CFLAGS) -fno-strict-aliasing/' \
	    -i dpm-postgres/config/linux.cf

	sed 's/^CXXFLAGS =/& $(CPPFLAGS) $(CXXFLAGS)/' \
	    -i dpm-postgres/common/Imakefile

	cd dpm-postgres && \
	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-etc-dir='$$(prefix)/../etc' \
		--with-sysconf-dir=EtcDir/default \
		--with-initrc-dir=EtcDir/init.d \
		--with-emi \
		--without-argus

	sed 's!$$(prefix)/var!$$(prefix)/../var!' -i dpm-postgres/config/site.def

	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 dpm-postgres/config/site.def
	sed '/^\#define.*YES/d' -i dpm-postgres/config/Project.tmpl \
				-i dpm-postgres/config/Library.tmpl
	sed '/^SECURITYDIR =/d' -i dpm-postgres/config/Project.tmpl
	sed 's/\(install:\).*/\1/' -i dpm-postgres/h/Imakefile

	cd dpm-postgres && \
	make -f Makefile.ini Makefiles

	touch $@

build: build-indep build-arch
	:

build-indep: build-stamp
	:

build-arch: build-stamp
	:

build-stamp: configure-stamp
	dh_testdir

	cd lfc-mysql && \
	$(MAKE) SOAPFLG="$(shell pkg-config --cflags gsoap)" \
		DEB_HOST_MULTIARCH=$(DEB_HOST_MULTIARCH) \
		SYSCONFDIR=$(_sysconfdir) \
		LDFLAGS="$(LDFLAGS) -Wl,-z,defs" \
		SHLIBLDFLAGS="-shared $(LDFLAGS) -Wl,-z,defs"

	cd lfc-postgres/shlib && ln -s ../../lfc-mysql/shlib/liblcgdm.so* .
	cd lfc-postgres/shlib && ln -s ../../lfc-mysql/shlib/liblfc.so* .

	cd lfc-postgres && \
	$(MAKE) SOAPFLG="$(shell pkg-config --cflags gsoap)" \
		DEB_HOST_MULTIARCH=$(DEB_HOST_MULTIARCH) \
		SYSCONFDIR=$(_sysconfdir) \
		LDFLAGS="$(LDFLAGS) -Wl,-z,defs" \
		SHLIBLDFLAGS="-shared $(LDFLAGS) -Wl,-z,defs" \
		POSINC=-I/usr/include/postgresql

	cd dpm-mysql/shlib && ln -s ../../lfc-mysql/shlib/liblcgdm.so* .

	cd dpm-mysql && \
	$(MAKE) SOAPFLG="$(shell pkg-config --cflags gsoap)" \
		DEB_HOST_MULTIARCH=$(DEB_HOST_MULTIARCH) \
		SYSCONFDIR=$(_sysconfdir) \
		LDFLAGS="$(LDFLAGS) -Wl,-z,defs" \
		SHLIBLDFLAGS="-shared $(LDFLAGS) -Wl,-z,defs"

	cd dpm-postgres/shlib && ln -s ../../lfc-mysql/shlib/liblcgdm.so* .
	cd dpm-postgres/shlib && ln -s ../../dpm-mysql/shlib/libdpm.so* .

	cd dpm-postgres && \
	$(MAKE) SOAPFLG="$(shell pkg-config --cflags gsoap)" \
		DEB_HOST_MULTIARCH=$(DEB_HOST_MULTIARCH) \
		SYSCONFDIR=$(_sysconfdir) \
		LDFLAGS="$(LDFLAGS) -Wl,-z,defs" \
		SHLIBLDFLAGS="-shared $(LDFLAGS) -Wl,-z,defs" \
		POSINC=-I/usr/include/postgresql

	touch $@

clean:
	dh_testdir
	dh_testroot

	rm -rf lfc-mysql
	rm -rf lfc-postgres
	rm -rf dpm-mysql
	rm -rf dpm-postgres

	rm -f debian/libdpm-perl.install debian/liblfc-perl.install

	rm -f build-stamp configure-stamp

	dh_clean

install: build-stamp
	dh_testdir
	dh_testroot
	mkdir -p debian/liblcgdm-dev$(_docdir)
	ln -s liblcgdm1 debian/liblcgdm-dev$(_docdir)/liblgcdm-dev
	mkdir -p debian/liblfc-dev$(_docdir)
	ln -s liblfc1 debian/liblfc-dev$(_docdir)/liblfc-dev
	mkdir -p debian/libdpm-dev$(_docdir)
	ln -s libdpm1 debian/libdpm-dev$(_docdir)/libdpm-dev
	dh_prep

	mkdir -p debian/tmp/etc/default

	# LFC with mysql backend

	cd lfc-mysql && \
	$(MAKE) SOAPFLG="$(shell pkg-config --cflags gsoap)" \
		DEB_HOST_MULTIARCH=$(DEB_HOST_MULTIARCH) \
		SYSCONFDIR=$(INSTALLDIR)$(_sysconfdir) \
		LDFLAGS="$(LDFLAGS) -Wl,-z,defs" \
		SHLIBLDFLAGS="-shared $(LDFLAGS) -Wl,-z,defs" \
		prefix=$(INSTALLDIR)$(_prefix) install install.man

	# lfcdaemon startup script
	sed -e 's/LD_LIBRARY_PATH=$$LD_LIBRARY_PATH //' \
	    -e '/LD_LIBRARY_PATH/d' \
	    -e 's!\$$PREFIX/etc!/etc!' \
	    -e 's!mysqld!mysql!' \
	    -i $(INSTALLDIR)$(_sysconfdir)/lfc-mysql/lfcdaemon.init

	# lfcdaemon configuration file
	sed -e 's/\(RUN_LFCDAEMON=\).*/\1"no"/' \
	    -e 's!/opt/lcg!!' -e 's!export SEMSGCONFIGFILE!#&!' \
	    -i $(INSTALLDIR)$(_sysconfdir)/lfc-mysql/lfcdaemon.conf

	# lfcdaemon man page
	sed -e 's!/opt/lcg!!g' \
	    -e 's/lfc-shutdown(1)/lfc-shutdown(8)/g' \
	    -i $(INSTALLDIR)$(_libdir)/lfc-mysql/lfcdaemon.8
	gzip -9 $(INSTALLDIR)$(_libdir)/lfc-mysql/lfcdaemon.8

	# lfc-shutdown man page
	gzip -9 $(INSTALLDIR)$(_libdir)/lfc-mysql/lfc-shutdown.8

	# lfc-dli startup script
	sed -e 's/LD_LIBRARY_PATH=$$LD_LIBRARY_PATH //' \
	    -e '/LD_LIBRARY_PATH/d' \
	    -e 's!\$$PREFIX/etc!/etc!' \
	    -e 's!/var/log/dli!/var/log/lfc-dli!g' \
	    -i  $(INSTALLDIR)$(_sysconfdir)/init.d/lfc-dli

	# lfc-dli configuration file
	sed -e 's!/var/log/dli!/var/log/lfc-dli!g' \
	    -e 's/\(LFC_HOST=\).*/\1`hostname -f`/' \
	    -e 's/\(RUN_DLIDAEMON=\).*/\1"no"/' \
	    -i $(INSTALLDIR)$(_sysconfdir)/lfc-dli
	mv $(INSTALLDIR)$(_sysconfdir)/lfc-dli \
	   $(INSTALLDIR)$(_sysconfdir)/default/lfc-dli

	# lfc-dli log rotation configuration
	sed -e 's!/var/log/dli!/var/log/lfc-dli!g' \
	    -i $(INSTALLDIR)$(_sysconfdir)/logrotate.d/lfc-dli

	# lfc-dli man page
	sed -e 's/\(\.TH [^ ]* \)1/\18/' \
	    -e 's!/var/log/dli!/var/log/lfc-dli!g' \
	    -e 's/lfcdaemon(1)/lfc-server(8)/g' \
	    -i $(INSTALLDIR)$(_mandir)/man8/lfc-dli.8

	# Add the LFC info provider script
	sed 's/\(^LFC_VERSION=\).*/\1$(VERSION)/' \
	    scripts/lcg-info-provider-lfc > \
	    $(INSTALLDIR)$(_datadir)/lcgdm/lcg-info-provider-lfc
	chmod 755 $(INSTALLDIR)$(_datadir)/lcgdm/lcg-info-provider-lfc

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

	# Remove due to name conflict
	rm $(INSTALLDIR)$(_mandir)/man3/log.3

	# LFC with postgres backend

	cd lfc-postgres && \
	$(MAKE) SOAPFLG="$(shell pkg-config --cflags gsoap)" \
		DEB_HOST_MULTIARCH=$(DEB_HOST_MULTIARCH) \
		SYSCONFDIR=$(INSTALLDIR)$(_sysconfdir) \
		LDFLAGS="$(LDFLAGS) -Wl,-z,defs" \
		SHLIBLDFLAGS="-shared $(LDFLAGS) -Wl,-z,defs" \
		prefix=$(INSTALLDIR)$(_prefix) install install.man

	# lfcdaemon startup script
	sed -e 's/LD_LIBRARY_PATH=$$LD_LIBRARY_PATH //' \
	    -e '/LD_LIBRARY_PATH/d' \
	    -e 's!\$$PREFIX/etc!/etc!' \
	    -i $(INSTALLDIR)$(_sysconfdir)/lfc-postgres/lfcdaemon.init

	# lfcdaemon configuration file
	sed -e 's/\(RUN_LFCDAEMON=\).*/\1"no"/' \
	    -e 's!/opt/lcg!!' -e 's!export SEMSGCONFIGFILE!#&!' \
	    -i $(INSTALLDIR)$(_sysconfdir)/lfc-postgres/lfcdaemon.conf

	# lfcdaemon man page
	sed -e 's!/opt/lcg!!g' \
	    -e 's/lfc-shutdown(1)/lfc-shutdown(8)/g' \
	    -i $(INSTALLDIR)$(_libdir)/lfc-postgres/lfcdaemon.8
	gzip -9 $(INSTALLDIR)$(_libdir)/lfc-postgres/lfcdaemon.8

	# lfc-shutdown man page
	gzip -9 $(INSTALLDIR)$(_libdir)/lfc-postgres/lfc-shutdown.8

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

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

	# DPM with mysql backend

	cd dpm-mysql && \
	$(MAKE) SOAPFLG="$(shell pkg-config --cflags gsoap)" \
		DEB_HOST_MULTIARCH=$(DEB_HOST_MULTIARCH) \
		SYSCONFDIR=$(INSTALLDIR)$(_sysconfdir) \
		LDFLAGS="$(LDFLAGS) -Wl,-z,defs" \
		SHLIBLDFLAGS="-shared $(LDFLAGS) -Wl,-z,defs" \
		prefix=$(INSTALLDIR)$(_prefix) install install.man

	sed 's!/usr/bin/env python!/usr/bin/python!' \
	    -i $(INSTALLDIR)$(_bindir)/dpm-listspaces

	# dpm startup script
	sed -e 's/LD_LIBRARY_PATH=$$LD_LIBRARY_PATH //' \
	    -e '/LD_LIBRARY_PATH/d' \
	    -e 's!\$$PREFIX/etc!/etc!' \
	    -e 's!mysqld!mysql!' \
	    -i $(INSTALLDIR)$(_sysconfdir)/dpm-mysql/dpm.init

	# dpm configuration file
	sed -e 's/\(^DPNS_HOST=\).*/\1`hostname -f`/' \
	    -e 's/\(^RUN_DPMDAEMON=\).*/\1"no"/' \
	    -e 's!/opt/lcg!!' -e 's!export SEMSGCONFIGFILE!#&!' \
	    -i $(INSTALLDIR)$(_sysconfdir)/dpm-mysql/dpm.conf

	# dpm man page
	sed -e 's/dpm-shutdown(1)/dpm-shutdown(8)/g' \
	    -i $(INSTALLDIR)$(_libdir)/dpm-mysql/dpm.8
	gzip -9 $(INSTALLDIR)$(_libdir)/dpm-mysql/dpm.8

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

	# dpm-shutdown man page
	gzip -9 $(INSTALLDIR)$(_libdir)/dpm-mysql/dpm-shutdown.8

	# dpm-buildfsv man page
	gzip -9 $(INSTALLDIR)$(_libdir)/dpm-mysql/dpm-buildfsv.8

	# dpnsdaemon startup script
	sed -e 's/LD_LIBRARY_PATH=$$LD_LIBRARY_PATH //' \
	    -e '/LD_LIBRARY_PATH/d' \
	    -e 's!\$$PREFIX/etc!/etc!' \
	    -e 's!mysqld!mysql!' \
	    -i $(INSTALLDIR)$(_sysconfdir)/dpm-mysql/dpnsdaemon.init

	# dpnsdaemon configuration file
	sed -e 's/\(^RUN_DPNSDAEMON=\).*/\1"no"/' \
	    -e 's!/opt/lcg!!' -e 's!export SEMSGCONFIGFILE!#&!' \
	    -i $(INSTALLDIR)$(_sysconfdir)/dpm-mysql/dpnsdaemon.conf

	# dpnsdaemon man page
	sed -e 's!/opt/lcg!!g' \
	    -e 's!/etc/NSCONFIG!/etc/DPNSCONFIG!g' \
	    -e 's/dpns-shutown(1)/dpns-shutdown(8)/g' \
	    -i $(INSTALLDIR)$(_libdir)/dpm-mysql/dpnsdaemon.8
	gzip -9 $(INSTALLDIR)$(_libdir)/dpm-mysql/dpnsdaemon.8

	# dpns-shutdown man page
	gzip -9 $(INSTALLDIR)$(_libdir)/dpm-mysql/dpns-shutdown.8

	# dpmcopyd startup script
	sed -e 's/LD_LIBRARY_PATH=$$LD_LIBRARY_PATH //' \
	    -e '/LD_LIBRARY_PATH/d' \
	    -e 's!\$$PREFIX/etc!/etc!' \
	    -e 's!mysqld!mysql!' \
	    -i $(INSTALLDIR)$(_sysconfdir)/dpm-mysql/dpmcopyd.init

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

	# dpmcopyd man page
	gzip -9 $(INSTALLDIR)$(_libdir)/dpm-mysql/dpmcopyd.8

	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/etc!/etc!' \
		-e "s/$${svc}/dpm-$${svc}/g" \
		-e 's!mysqld!mysql!' \
		-i $(INSTALLDIR)$(_sysconfdir)/dpm-mysql/$${svc}.init ; \
	    mv $(INSTALLDIR)$(_sysconfdir)/dpm-mysql/$${svc}.init \
	       $(INSTALLDIR)$(_sysconfdir)/dpm-mysql/dpm-$${svc}.init ; \
	    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"/' \
		-i $(INSTALLDIR)$(_sysconfdir)/dpm-mysql/$${svc}.conf ; \
	    mv $(INSTALLDIR)$(_sysconfdir)/dpm-mysql/$${svc}.conf \
	       $(INSTALLDIR)$(_sysconfdir)/dpm-mysql/dpm-$${svc}.conf ; \
	    rmdir $(INSTALLDIR)$(_localstatedir)/log/$${svc} ; \
	    mkdir -p $(INSTALLDIR)$(_localstatedir)/log/dpm-$${svc} ; \
	    sed -e "s/$${svc}/dpm-$${svc}/g" \
		-i $(INSTALLDIR)$(_sysconfdir)/dpm-mysql/$${svc}.logrotate ; \
	    mv $(INSTALLDIR)$(_sysconfdir)/dpm-mysql/$${svc}.logrotate \
	       $(INSTALLDIR)$(_sysconfdir)/dpm-mysql/dpm-$${svc}.logrotate ; \
	    mv $(INSTALLDIR)$(_libdir)/dpm-mysql/$${svc} \
	       $(INSTALLDIR)$(_libdir)/dpm-mysql/dpm-$${svc} ; \
	    sed -e "s/$${svc}/dpm-$${svc}/g" \
		-e 's/dpm(1)/dpm(8)/g' \
		-i $(INSTALLDIR)$(_libdir)/dpm-mysql/$${svc}.8 ; \
	    mv $(INSTALLDIR)$(_libdir)/dpm-mysql/$${svc}.8 \
	       $(INSTALLDIR)$(_libdir)/dpm-mysql/dpm-$${svc}.8 ; \
	    gzip -9 $(INSTALLDIR)$(_libdir)/dpm-mysql/dpm-$${svc}.8 ; \
	done

	# rfiod startup script
	sed -e 's/LD_LIBRARY_PATH=$$LD_LIBRARY_PATH //' \
	    -e '/LD_LIBRARY_PATH/d' \
	    -e 's!\$$PREFIX/etc!/etc!' \
	    -e 's/rfiod/dpm-rfiod/g' \
	    -e 's!/var/log/rfio!/var/log/dpm-rfiod!g' \
	    -i $(INSTALLDIR)$(_sysconfdir)/init.d/rfiod
	mv $(INSTALLDIR)$(_sysconfdir)/init.d/rfiod \
	   $(INSTALLDIR)$(_sysconfdir)/init.d/dpm-rfiod

	# rfiod configuration file
	sed -e 's/rfiod/dpm-rfiod/g' \
	    -e 's!/var/log/rfio!/var/log/dpm-rfiod!g' \
	    -e 's/\(^DPNS_HOST=\).*/\1`hostname -f`/' \
	    -e 's/\(^DPM_HOST=\).*/\1`hostname -f`/' \
	    -e 's/\(^RUN_RFIOD=\).*/\1"no"/' \
	    -i $(INSTALLDIR)$(_sysconfdir)/rfiod
	mv $(INSTALLDIR)$(_sysconfdir)/rfiod \
	   $(INSTALLDIR)$(_sysconfdir)/default/dpm-rfiod

	# rfiod log directory and log rotation configuration
	rmdir $(INSTALLDIR)$(_localstatedir)/log/rfio
	mkdir -p $(INSTALLDIR)$(_localstatedir)/log/dpm-rfiod
	sed -e 's!/var/log/rfio!/var/log/dpm-rfiod!g' \
	    -i $(INSTALLDIR)$(_sysconfdir)/logrotate.d/rfiod
	mv $(INSTALLDIR)$(_sysconfdir)/logrotate.d/rfiod \
	   $(INSTALLDIR)$(_sysconfdir)/logrotate.d/dpm-rfiod

	# rfiod binary and man page
	mv $(INSTALLDIR)$(_sbindir)/rfiod \
	   $(INSTALLDIR)$(_sbindir)/dpm-rfiod
	sed -e 's/rfiod/dpm-rfiod/g' \
	    -e 's!/usr/local/bin!/usr/sbin!g' \
	    -i $(INSTALLDIR)$(_mandir)/man8/rfiod.8
	mv $(INSTALLDIR)$(_mandir)/man8/rfiod.8 \
	   $(INSTALLDIR)$(_mandir)/man8/dpm-rfiod.8

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

	# DPM with postgres backend

	cd dpm-postgres && \
	$(MAKE) SOAPFLG="$(shell pkg-config --cflags gsoap)" \
		DEB_HOST_MULTIARCH=$(DEB_HOST_MULTIARCH) \
		SYSCONFDIR=$(INSTALLDIR)$(_sysconfdir) \
		LDFLAGS="$(LDFLAGS) -Wl,-z,defs" \
		SHLIBLDFLAGS="-shared $(LDFLAGS) -Wl,-z,defs" \
		prefix=$(INSTALLDIR)$(_prefix) install install.man

	# dpm startup script
	sed -e 's/LD_LIBRARY_PATH=$$LD_LIBRARY_PATH //' \
	    -e '/LD_LIBRARY_PATH/d' \
	    -e 's!\$$PREFIX/etc!/etc!' \
	    -i $(INSTALLDIR)$(_sysconfdir)/dpm-postgres/dpm.init

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

	# dpm man page
	sed -e 's/dpm-shutdown(1)/dpm-shutdown(8)/g' \
	    -i $(INSTALLDIR)$(_libdir)/dpm-postgres/dpm.8
	gzip -9 $(INSTALLDIR)$(_libdir)/dpm-postgres/dpm.8

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

	# dpm-shutdown man page
	gzip -9 $(INSTALLDIR)$(_libdir)/dpm-postgres/dpm-shutdown.8

	# dpm-buildfsv man page
	gzip -9 $(INSTALLDIR)$(_libdir)/dpm-postgres/dpm-buildfsv.8

	# dpnsdaemon startup script
	sed -e 's/LD_LIBRARY_PATH=$$LD_LIBRARY_PATH //' \
	    -e '/LD_LIBRARY_PATH/d' \
	    -e 's!\$$PREFIX/etc!/etc!' \
	    -i $(INSTALLDIR)$(_sysconfdir)/dpm-postgres/dpnsdaemon.init

	# dpnsdaemon configuration file
	sed -e 's/\(^RUN_DPNSDAEMON=\).*/\1"no"/' \
	    -e 's!/opt/lcg!!' -e 's!export SEMSGCONFIGFILE!#&!' \
	    -i $(INSTALLDIR)$(_sysconfdir)/dpm-postgres/dpnsdaemon.conf

	# dpnsdaemon man page
	sed -e 's!/opt/lcg!!g' \
	    -e 's!/etc/NSCONFIG!/etc/DPNSCONFIG!g' \
	    -e 's/dpns-shutdown(1)/dpns-shutdown(8)/g' \
	    -i $(INSTALLDIR)$(_libdir)/dpm-postgres/dpnsdaemon.8
	gzip -9 $(INSTALLDIR)$(_libdir)/dpm-postgres/dpnsdaemon.8

	# dpns-shutdown man page
	gzip -9 $(INSTALLDIR)$(_libdir)/dpm-postgres/dpns-shutdown.8

	# dpmcopyd startup script
	sed -e 's/LD_LIBRARY_PATH=$$LD_LIBRARY_PATH //' \
	    -e '/LD_LIBRARY_PATH/d' \
	    -e 's!\$$PREFIX/etc!/etc!' \
	    -i $(INSTALLDIR)$(_sysconfdir)/dpm-postgres/dpmcopyd.init

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

	# dpmcopyd man page
	gzip -9 $(INSTALLDIR)$(_libdir)/dpm-postgres/dpmcopyd.8

	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/etc!/etc!' \
		-e "s/$${svc}/dpm-$${svc}/g" \
		-i $(INSTALLDIR)$(_sysconfdir)/dpm-postgres/$${svc}.init ; \
	    mv $(INSTALLDIR)$(_sysconfdir)/dpm-postgres/$${svc}.init \
	       $(INSTALLDIR)$(_sysconfdir)/dpm-postgres/dpm-$${svc}.init ; \
	    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"/' \
		-i $(INSTALLDIR)$(_sysconfdir)/dpm-postgres/$${svc}.conf ; \
	    mv $(INSTALLDIR)$(_sysconfdir)/dpm-postgres/$${svc}.conf \
	       $(INSTALLDIR)$(_sysconfdir)/dpm-postgres/dpm-$${svc}.conf ; \
	    rmdir $(INSTALLDIR)$(_localstatedir)/log/$${svc} ; \
	    mkdir -p $(INSTALLDIR)$(_localstatedir)/log/dpm-$${svc} ; \
	    sed -e "s/$${svc}/dpm-$${svc}/g" \
		-i $(INSTALLDIR)$(_sysconfdir)/dpm-postgres/$${svc}.logrotate ; \
	    mv $(INSTALLDIR)$(_sysconfdir)/dpm-postgres/$${svc}.logrotate \
	       $(INSTALLDIR)$(_sysconfdir)/dpm-postgres/dpm-$${svc}.logrotate ; \
	    mv $(INSTALLDIR)$(_libdir)/dpm-postgres/$${svc} \
	       $(INSTALLDIR)$(_libdir)/dpm-postgres/dpm-$${svc} ; \
	    sed -e "s/$${svc}/dpm-$${svc}/g" \
		-e 's/dpm(1)/dpm(8)/g' \
		-i $(INSTALLDIR)$(_libdir)/dpm-postgres/$${svc}.8 ; \
	    mv $(INSTALLDIR)$(_libdir)/dpm-postgres/$${svc}.8 \
	       $(INSTALLDIR)$(_libdir)/dpm-postgres/dpm-$${svc}.8 ; \
	    gzip -9 $(INSTALLDIR)$(_libdir)/dpm-postgres/dpm-$${svc}.8 ; \
	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)/lcgdm/create_dpm_tables_postgres.sql \
	    $(INSTALLDIR)$(_datadir)/lcgdm/create_dpns_tables_postgres.sql

	# Dynamic paths in perl packages
	sed -e 's,@ARCHLIB@,$(_archlib),g' debian/libdpm-perl.install.in > debian/libdpm-perl.install
	sed -e 's,@ARCHLIB@,$(_archlib),g' debian/liblfc-perl.install.in > debian/liblfc-perl.install

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=lcgdm-dbg
	dh_compress
	dh_fixperms
	dh_perl
	dh_python2
	dh_makeshlibs
	dh_installdeb
	dh_shlibdeps -l debian/libdpm1/usr/lib:debian/liblfc1/usr/lib:debian/liblcgdm1/usr/lib
	dh_gencontrol
	dh_md5sums
	dh_builddeb

binary: binary-indep binary-arch
	:

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

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