#!/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
_datadir = $(_prefix)/share
_mandir = $(_datadir)/man
_docdir = $(_datadir)/doc

version=2.47.2
tagvers=$(version)-0

configure: configure-stamp
	:

configure-stamp: $(QUILT_STAMPFN)
	dh_testdir

	for f in Copyright base64.c base64.h util.c ; do \
	  if (file plugins/gssapi/$$f | grep -q 8859) ; then \
	    iconv -f iso-8859-1 -t utf-8 plugins/gssapi/$$f \
				      -o plugins/gssapi/$$f.new ; \
	    mv plugins/gssapi/$$f.new plugins/gssapi/$$f ; \
	  fi ; \
	done

	sed 's%@@LIBDIR@@%/usr/lib%' -i src/tunnelManager.c

	chmod +x bootstrap.sh
	./bootstrap.sh

	CFLAGS="$(CFLAGS) -fno-strict-aliasing" LDFLAGS="-Wl,-z,defs" \
	./configure --prefix=/usr --with-globus-include="$(_includedir)/globus -I$(_libdir)/globus/include" --with-globus-lib=/dummy

	touch $@

build: build-stamp
	:

build-stamp: configure-stamp
	dh_testdir

	$(MAKE)

	touch $@

unpatch: cleanup

clean: unpatch
	:

cleanup:
	dh_testdir
	dh_testroot

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

	rm -rf config
	rm -f aclocal.m4
	rm -f configure
	rm -f Makefile.in
	rm -f src/debug_level.h.in
	rm -f src/Makefile.in
	rm -f plugins/Makefile.in
	rm -f plugins/*/Makefile.in

	rm -f build-stamp configure-stamp

	for f in Copyright base64.c base64.h util.c ; do \
	  if (file plugins/gssapi/$$f | grep -qi UTF) ; then \
	    iconv -f utf-8 -t iso-8859-1 plugins/gssapi/$$f \
				      -o plugins/gssapi/$$f.new ; \
	    mv plugins/gssapi/$$f.new plugins/gssapi/$$f ; \
	  fi ; \
	done

	sed 's%/usr/lib%@@LIBDIR@@%' -i src/tunnelManager.c

	dh_clean

install: build-stamp
	dh_testdir
	dh_testroot
	dh_clean -k 

	$(MAKE) install DESTDIR=$(INSTALLDIR)

	# Move plugins out of the default library path
	mkdir $(INSTALLDIR)/$(_libdir)/dcap
	mv $(INSTALLDIR)/$(_libdir)/lib*Tunnel* $(INSTALLDIR)/$(_libdir)/dcap

	rm $(INSTALLDIR)/$(_libdir)/dcap/*.a
	rm $(INSTALLDIR)/$(_libdir)/dcap/*.la

	rm -rf $(INSTALLDIR)/$(_datadir)/doc

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 -l debian/libdcap1/usr/lib
	dh_gencontrol
	dh_md5sums
	dh_builddeb

binary: binary-indep binary-arch
	:

get-orig-source:
	if [ -r ../dcap_$(version).orig.tar.gz ] ; then \
	  echo "dcap_$(version).orig.tar.gz already exists." ; \
	else \
	  if [ -d "dcap-$(version)" ]; then \
	    echo "Please remove existing directory 'dcap-$(version)'" ; \
	    exit -1 ; \
	  fi ; \
	  echo "Fetching upstream svn version" ; \
	  svn co http://svn.dcache.org/dCache/tags/dcap-$(tagvers) \
	    dcap-$(version) ; \
	  echo "Removing some windows sources due to different licensing" ; \
	  rm dcap-$(version)/src/dcap_unix2win.c ; \
	  echo "Removing java base 64 sources due to different licensing" ; \
	  rm dcap-$(version)/plugins/javatunnel/Base64.java ; \
	  echo "Packing it up." ; \
	  GZIP=-9 tar --exclude=.svn -z -c -f ../dcap_$(version).orig.tar.gz \
	    dcap-$(version) ; \
	  echo "Cleaning up." ; \
	  rm -rf dcap-$(version) ; \
	fi

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