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

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 config/Library.tmpl ; 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 lfc --with-postgres \
		--libdir=lib \
		--with-gsoap-location=$(_prefix) \
		--with-gsoap-version=$$gsoapversion \
		--with-voms-location=$(_prefix) \
		--with-id-map-file=$(_sysconfdir)/lcgdm-mapfile \
		--with-ns-config-file=$(_sysconfdir)/NSCONFIG \
		--with-sysconf-dir='$$(prefix)/../etc'

	sed -e 's/\(BuildDLI	*\)YES/\1NO/' \
	    -e 's/\(BuildInterfaces	*\)YES/\1NO/' \
	    -e 's/\(BuildNameServerClient	*\)YES/\1NO/' \
	    -e 's/\(BuildNameServerLibrary	*\)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/liblfc.so* .

	touch $@

build: build-stamp
	:

build-stamp: configure-stamp
	dh_testdir

	$(MAKE) prefix=$(_prefix) 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/Project.tmpl config/Library.tmpl ; 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 h/patchlevel.h
	rm -f NSCONFIG

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

	rm -f build-stamp configure-stamp

	dh_clean

install: build-stamp
	dh_testdir
	dh_testroot
	dh_clean -k

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

	mv $(INSTALLDIR)$(_datadir)/LFC $(INSTALLDIR)$(_datadir)/lfc

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

	# lfcdaemon 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)/lfc/rc.lfcdaemon > \
	    $(INSTALLDIR)$(_sysconfdir)/lfc-postgres/lfcdaemon.init
	chmod 755 $(INSTALLDIR)$(_sysconfdir)/lfc-postgres/lfcdaemon.init
	rm $(INSTALLDIR)$(_datadir)/lfc/rc.lfcdaemon

	# lfcdaemon configuration file
	sed -e 's!/opt/lcg!!g' \
	    -e 's/\(RUN_LFCDAEMON=\).*/\1"no"/' \
	    $(INSTALLDIR)$(_sysconfdir)/lfcdaemon.conf.templ > \
	    $(INSTALLDIR)$(_sysconfdir)/lfc-postgres/lfcdaemon.conf
	rm $(INSTALLDIR)$(_sysconfdir)/lfcdaemon.conf.templ

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

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

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

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

	# 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)/lfc/create_lfc_tables_postgres.sql

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

	# Lintian overrides
	mkdir -p $(INSTALLDIR)/usr/share/lintian/overrides
	install -m 644 debian/lfc-postgres.lintian-overrides \
		$(INSTALLDIR)/usr/share/lintian/overrides/lfc-postgres

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="../lfc-postgres_$(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 lfc-postgres-$(VERSION) ; \
	  LANG=C svn co http://svnweb.cern.ch/guest/lcgdm/lcg-dm/tags/$$uversion lfc-postgres-$(VERSION) ; \
	  echo "Packing it up." ; \
	  GZIP=-9 tar --exclude .svn --exclude debian -z -c -f "$$dest" lfc-postgres-$(VERSION) ; \
	  echo "Cleaning up." ; \
	  rm -rf lfc-postgres-$(VERSION) ; \
	fi

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