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

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/Project.tmpl \
		 lib/Imakefile shlib/Imakefile ; do \
	    [ ! -e $$f.save ] && cp -p $$f $$f.save || : ; \
	done

	sed 's!@@LIBDIR@@!$(_libdir)!' -i security/Csec_api_loader.c

	# 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-mysql \
		--libdir=lib \
		--with-gsoap-location=$(_prefix) \
		--with-gsoap-version=$$gsoapversion \
		--with-voms-location=$(_prefix) \
		--with-dpm-config-file=$(_sysconfdir)/DPMCONFIG \
		--with-id-map-file=$(_sysconfdir)/lcgdm-mapfile \
		--with-ns-config-file=$(_sysconfdir)/DPNSCONFIG \
		--with-sysconf-dir='$$(prefix)/../etc'

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

	make -f Makefile.ini Makefiles

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

	touch $@

build: build-stamp
	:

build-stamp: configure-stamp
	dh_testdir

	$(MAKE) prefix=$(_prefix)

	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/Project.tmpl \
		 lib/Imakefile shlib/Imakefile ; do \
	    [ -e $$f.save ] && mv $$f.save $$f || : ; \
	done

	sed 's!$(_libdir)!@@LIBDIR@@!' -i security/Csec_api_loader.c

	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_mysql_ifce.c
	rm -f dpmcopy/dpm_copyfile.c

	rm -f h/patchlevel.h
	rm -f dpm/dpm.pm dpm/dpm_perlwrap.c
	rm -f dpm/dpm.py dpm/dpm_wrap.c
	rm -f dpm/dpm2.py dpm/dpm2_wrap.c
	rm -f NSCONFIG

	rm -f shlib/liblcgdm.so*

	rm -f build-stamp configure-stamp

	dh_clean

install: build-stamp
	dh_testdir
	dh_testroot
	mkdir -p debian/libdpm-dev$(_docdir)
	ln -s libdpm1 debian/libdpm-dev$(_docdir)/libdpm-dev
	dh_clean -k

	$(MAKE) prefix=$(INSTALLDIR)$(_prefix) install
	$(MAKE) prefix=$(INSTALLDIR)$(_prefix) install.man

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

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

	mkdir -p $(INSTALLDIR)$(_sysconfdir)/init.d
	mkdir -p $(INSTALLDIR)$(_sysconfdir)/default
	mkdir -p $(INSTALLDIR)$(_sysconfdir)/logrotate.d
	mkdir -p $(INSTALLDIR)$(_sbindir)
	mkdir -p $(INSTALLDIR)$(_mandir)/man8

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

	# dpm startup script
	sed -e 's/LD_LIBRARY_PATH=$$LD_LIBRARY_PATH //' \
	    -e '/LD_LIBRARY_PATH/d' \
	    -e 's!/opt/lcg/bin!/usr/sbin!g' -e 's!/opt/lcg!!g' \
	    $(INSTALLDIR)$(_datadir)/dpm/rc.dpm > \
	    $(INSTALLDIR)$(_sysconfdir)/dpm-mysql/dpm.init
	chmod 755 $(INSTALLDIR)$(_sysconfdir)/dpm-mysql/dpm.init
	rm $(INSTALLDIR)$(_datadir)/dpm/rc.dpm

	# dpm configuration file
	sed -e 's!/opt/lcg!!g' \
	    -e 's/\(^DPNS_HOST=\).*/\1`hostname -f`/' \
	    -e 's/\(^RUN_DPMDAEMON=\).*/\1"no"/' \
	    $(INSTALLDIR)$(_sysconfdir)/dpm.conf.templ > \
	    $(INSTALLDIR)$(_sysconfdir)/dpm-mysql/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-mysql/dpm.logrotate

	# dpm binary and man page
	mv $(INSTALLDIR)$(_bindir)/dpm \
	   $(INSTALLDIR)$(_libdir)/dpm-mysql/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-mysql/dpm.8.gz
	rm $(INSTALLDIR)$(_mandir)/man1/dpm.1

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

	# dpm-shutdown binary and man page
	mv $(INSTALLDIR)$(_bindir)/dpm-shutdown \
	   $(INSTALLDIR)$(_libdir)/dpm-mysql/dpm-shutdown
	sed -e 's/\(\.TH [^ ]* \)1/\18/' \
	    $(INSTALLDIR)$(_mandir)/man1/dpm-shutdown.1 | gzip -9 -n -c > \
	    $(INSTALLDIR)$(_libdir)/dpm-mysql/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!/opt/lcg/bin!/usr/sbin!g' -e 's!/opt/lcg!!g' \
	    -e 's!/etc/NSCONFIG!/etc/DPNSCONFIG!g' \
	    $(INSTALLDIR)$(_datadir)/dpm/rc.dpnsdaemon > \
	    $(INSTALLDIR)$(_sysconfdir)/dpm-mysql/dpnsdaemon.init
	chmod 755 $(INSTALLDIR)$(_sysconfdir)/dpm-mysql/dpnsdaemon.init
	rm $(INSTALLDIR)$(_datadir)/dpm/rc.dpnsdaemon

	# dpnsdaemon configuration file
	sed -e 's!/opt/lcg!!g' \
	    -e 's!/etc/NSCONFIG!/etc/DPNSCONFIG!g' \
	    -e 's/\(^RUN_DPNSDAEMON=\).*/\1"no"/' \
	    $(INSTALLDIR)$(_sysconfdir)/dpnsdaemon.conf.templ > \
	    $(INSTALLDIR)$(_sysconfdir)/dpm-mysql/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-mysql/dpnsdaemon.logrotate

	# dpnsdaemon binary and man page
	mv $(INSTALLDIR)$(_bindir)/dpnsdaemon \
	   $(INSTALLDIR)$(_libdir)/dpm-mysql/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-mysql/dpnsdaemon.8.gz
	rm $(INSTALLDIR)$(_mandir)/man1/dpnsdaemon.1

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

	# dpns-shutdown binary and man page
	mv $(INSTALLDIR)$(_bindir)/dpns-shutdown \
	   $(INSTALLDIR)$(_libdir)/dpm-mysql/dpns-shutdown
	sed -e 's/\(\.TH [^ ]* \)1/\18/' \
	    $(INSTALLDIR)$(_mandir)/man1/dpns-shutdown.1 | gzip -9 -n -c > \
	    $(INSTALLDIR)$(_libdir)/dpm-mysql/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!/opt/lcg/bin!/usr/sbin!g' -e 's!/opt/lcg!!g' \
	    $(INSTALLDIR)$(_datadir)/dpm/rc.dpmcopyd > \
	    $(INSTALLDIR)$(_sysconfdir)/dpm-mysql/dpmcopyd.init
	chmod 755 $(INSTALLDIR)$(_sysconfdir)/dpm-mysql/dpmcopyd.init
	rm $(INSTALLDIR)$(_datadir)/dpm/rc.dpmcopyd

	# dpmcopyd configuration file
	sed -e 's!/opt/lcg!!g' \
	    -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-mysql/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-mysql/dpmcopyd.logrotate

	# dpmcopyd binary and man page
	mv $(INSTALLDIR)$(_bindir)/dpmcopyd \
	   $(INSTALLDIR)$(_libdir)/dpm-mysql/dpmcopyd
	sed -e 's/\(\.TH [^ ]* \)1/\18/' \
	    -e 's!/opt/lcg/lib/!!g' \
	    $(INSTALLDIR)$(_mandir)/man1/dpmcopyd.1 | gzip -9 -n -c > \
	    $(INSTALLDIR)$(_libdir)/dpm-mysql/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!/opt/lcg/bin!/usr/sbin!g' -e 's!/opt/lcg!!g' \
		-e "s/$${svc}/dpm-$${svc}/g" \
		$(INSTALLDIR)$(_datadir)/dpm/rc.$${svc} > \
		$(INSTALLDIR)$(_sysconfdir)/dpm-mysql/dpm-$${svc}.init ; \
	    chmod 755 $(INSTALLDIR)$(_sysconfdir)/dpm-mysql/dpm-$${svc}.init ; \
	    rm $(INSTALLDIR)$(_datadir)/dpm/rc.$${svc} ; \
	    sed -e "s/$${svc}/dpm-$${svc}/g" -e 's!/opt/lcg!!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-mysql/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-mysql/dpm-$${svc}.logrotate ; \
	    mv $(INSTALLDIR)$(_bindir)/$${svc} \
	       $(INSTALLDIR)$(_libdir)/dpm-mysql/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-mysql/dpm-$${svc}.8.gz ; \
	    rm $(INSTALLDIR)$(_mandir)/man1/$${svc}.1 ; \
	done

	# rfiod startup script
	sed -e 's/LD_LIBRARY_PATH=$$LD_LIBRARY_PATH //' \
	    -e '/LD_LIBRARY_PATH/d' \
	    -e 's!/opt/lcg/bin!/usr/sbin!g' -e 's!/opt/lcg!!g' \
	    -e 's/rfiod/dpm-rfiod/g' \
	    -e 's!/var/log/rfio!/var/log/dpm-rfiod!g' \
	    $(INSTALLDIR)$(_datadir)/dpm/rc.rfiod > \
	    $(INSTALLDIR)$(_sysconfdir)/init.d/dpm-rfiod
	chmod 755 $(INSTALLDIR)$(_sysconfdir)/init.d/dpm-rfiod
	rm $(INSTALLDIR)$(_datadir)/dpm/rc.rfiod

	# rfiod configuration file
	sed -e 's/rfiod/dpm-rfiod/g' -e 's!/opt/lcg!!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"/' \
	    $(INSTALLDIR)$(_sysconfdir)/rfiod.conf.templ > \
	    $(INSTALLDIR)$(_sysconfdir)/default/dpm-rfiod
	rm $(INSTALLDIR)$(_sysconfdir)/rfiod.conf.templ

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

	# dpm-rfiod binary and man page
	mv $(INSTALLDIR)$(_bindir)/rfiod \
	   $(INSTALLDIR)$(_sbindir)/dpm-rfiod
	sed -e 's/\.TH \([^ ]* \)1/.TH DPM-\18/' \
	    -e 's/rfiod/dpm-rfiod/g' \
	    -e 's!/usr/local/bin!/usr/sbin!g' \
	    $(INSTALLDIR)$(_mandir)/man1/rfiod.1 > \
	    $(INSTALLDIR)$(_mandir)/man8/dpm-rfiod.8
	rm $(INSTALLDIR)$(_mandir)/man1/rfiod.1

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

	# Remove files already present in other packages
	rm $(INSTALLDIR)$(_mandir)/man3/serrno.3
	rm $(INSTALLDIR)$(_mandir)/man4/Castor_limits.4

	# Lintian overrides
	mkdir -p $(INSTALLDIR)/usr/share/lintian/overrides
	for pkg in dpm-mysql dpm-mysql-nameserver dpm-mysql-copyd \
	    dpm-mysql-srmv1 dpm-mysql-srmv2 dpm-mysql-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_perl
	dh_pysupport
	dh_makeshlibs
	dh_installdeb
	dh_shlibdeps -l debian/libdpm1/usr/lib
	dh_gencontrol
	dh_md5sums
	dh_builddeb

binary: binary-indep binary-arch
	:

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

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