#!/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
_libdir = $(_prefix)/lib
_datadir = $(_prefix)/share

configure: configure-stamp
	:

configure-stamp:
	dh_testdir

	for f in config.sub config.guess ; do \
	  if [ -e /usr/share/misc/$$f -a ! -e project/$$f.dist ] ; then \
	    mv project/$$f project/$$f.dist ; \
	    cp -f /usr/share/misc/$$f project/$$f ; \
	  fi ; \
	done

	aclocal -I project
	libtoolize --force
	autoheader
	automake --foreign --copy --add-missing
	autoconf

	CFLAGS="$(CFLAGS)" CXXFLAGS="$(CFLAGS)" FFLAGS="$(CFLAGS)" \
	LDFLAGS="-Wl,-z,defs" ./configure \
	   --host=$(DEB_HOST_GNU_TYPE) \
	   --build=$(DEB_BUILD_GNU_TYPE) \
	   --prefix=$(_prefix) \
	   --libdir=$(_libdir)/voms

	touch $@

build: build-indep build-arch
	:

build-indep: build-stamp
	:

build-arch: build-stamp
	:

build-stamp: configure-stamp
	dh_testdir

	$(MAKE)

	touch $@

clean:
	dh_testdir
	dh_testroot

	if [ -r Makefile ] ; then $(MAKE) clean && $(MAKE) distclean ; fi

	find . -name Makefile.in -exec rm {} ';'
	rm -f aclocal.m4
	rm -f configure
	rm -f project/ltmain.sh
	rm -f project/libtool.m4
	rm -f project/lt*.m4
	rm -f src/autogen/config.h.in

	for f in `find . -name Makefile.am`; do \
	  sed -e 's!\(^sysconfdir *= *\)@sysconfdir@!\1\$$(prefix)/etc!' \
	      -i $$f ; \
	done

	for f in config.sub config.guess ; do \
	  if [ -e project/$$f.dist ] ; then \
	    mv -f project/$$f.dist project/$$f ; \
	  fi ; \
	done

	rm -f build-stamp configure-stamp

	dh_clean

install: build-stamp
	dh_testdir
	dh_testroot
	dh_clean -k 

	$(MAKE) install DESTDIR=$(INSTALLDIR)

	rm $(INSTALLDIR)$(_libdir)/voms/libvomsmysql.a
	rm $(INSTALLDIR)$(_libdir)/voms/libvomsmysql.la

binary-indep:
	:

binary-arch: install
	dh_testdir
	dh_testroot
	dh_installchangelogs
	dh_installdocs
	dh_installexamples
	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
	:

name=voms-mysql-plugin
version=3.1.5.1
get-orig-source:
	if [ -r ../$(name)_$(version).orig.tar.gz ] ; then \
	  echo "$(name)_$(version).orig.tar.gz already exists." ; \
	else \
	  echo "Fetching upstream CVS version" ; \
	  cvs -d:pserver:anonymous:@glite.cvs.cern.ch:/cvs/glite co \
	    -r glite-security-voms-mysql_R_`echo $(version)|tr "." "_"` \
	    -d $(name)-$(version) org.glite.security.voms-mysql ; \
	  echo "Packing it up." ; \
	  GZIP=-9 tar --exclude=CVS -z -c -f ../$(name)_$(version).orig.tar.gz $(name)-$(version) ; \
	  echo "Cleaning up." ; \
	  rm -rf $(name)-$(version) ; \
	fi

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