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

include /usr/share/quilt/quilt.make

version = 2.8.3

configure: configure-stamp
	:

configure-stamp: $(QUILT_STAMPFN)
	dh_testdir

	find . -name '*.xml' -exec chmod a-x {} \;

	rm -f gsoap/doc/wsse/html/.DS_Store
	rm -f gsoap/doc/.DS_Store

	sed 's/Cflags:/& @SOAPCPP2_IPV6@/' -i gsoap*.pc.in

	autoreconf --install --force

	CFLAGS='$(CFLAGS)' CXXFLAGS='$(CFLAGS)' ./configure \
		--host=$(DEB_HOST_GNU_TYPE) \
		--build=$(DEB_BUILD_GNU_TYPE) \
		--prefix=/usr \
		--exec-prefix='$${prefix}' \
		--mandir='$${datadir}/man' \
		--infodir='$${datadir}/info' \
		--enable-ipv6 \
		--enable-samples

	touch $@

build: build-stamp
	:

build-stamp: configure-stamp
	dh_testdir

	$(MAKE)

	touch $@

clean: unpatch
	:

unpatch: cleanup

cleanup:
	dh_testdir
	dh_testroot

	if [ -r Makefile ]; then $(MAKE) reallyveryveryclean; fi

	# Remove autogenerated files

	rm -f ltmain.sh
	rm -f libtool
	rm -f ylwrap

	rm -rf autom4te.cache

	rm -f gsoap/samples/databinding/address.h
	rm -f gsoap/samples/udp/udp.wsdl

	rm -f gsoap/samples/chaining++/QuoteC.cpp
	rm -f gsoap/samples/chaining++/QuoteH.h
	rm -f gsoap/samples/chaining++/QuoteStub.h
	rm -f gsoap/samples/chaining++/CalcC.cpp
	rm -f gsoap/samples/chaining++/CalcH.h
	rm -f gsoap/samples/chaining++/CalcStub.h

	find . -name Makefile.in -exec rm {} ';'

	rm -f build-stamp configure-stamp

	sed 's/ @SOAPCPP2_IPV6@//' -i gsoap*.pc.in

	dh_clean

install: build-stamp
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs

	$(MAKE) install DESTDIR=$(CURDIR)/debian/tmp

	rm $(CURDIR)/debian/tmp/usr/lib/*.la

binary-indep:
	:

binary-arch: install
	dh_testdir
	dh_testroot
	dh_installchangelogs 
	dh_installdocs
	dh_install --fail-missing
	dh_installman
	dh_link
	dh_strip --dbg-package=gsoap-dbg
	dh_compress
	dh_fixperms
	dh_makeshlibs
	dh_installdeb
	dh_shlibdeps -l debian/gsoap/usr/lib
	dh_gencontrol
	dh_md5sums
	dh_builddeb

binary: binary-indep binary-arch
	:

get-orig-source:
	if [ -r gsoap_$(version).orig.tar.gz ] ; then \
	  echo "gsoap_$(version).orig.tar.gz already exists." ; \
	else \
	  if [ -d "gsoap-2.8" ]; then \
	    echo "Please remove existing directory 'gsoap-2.8'" ; \
	    exit 1 ; \
	  fi ; \
	  if [ -d "gsoap-$(version)" ]; then \
	    echo "Please remove existing directory 'gsoap-$(version)'" ; \
	    exit 1 ; \
	  fi ; \
	  if [ -r gsoap_$(version).zip ]; then \
	    echo "Using existing ZIP archive gsoap_$(version).zip" ; \
	    echo "Delete the file and rerun if you want to redownload" ; \
	  else \
	    echo "Fetching upstream ZIP archive" ; \
	    wget http://downloads.sourceforge.net/gsoap2/gsoap_$(version).zip ; \
	  fi ; \
	  echo "Unpacking and renaming folder" ; \
	  unzip gsoap_$(version).zip ; \
	  mv gsoap-2.8 gsoap-$(version) ; \
	  echo "Deleting code with only non-free licenses" ; \
	  rm -rf gsoap-$(version)/gsoap/extras \
		 gsoap-$(version)/gsoap/mod_gsoap \
		 gsoap-$(version)/gsoap/Symbian ; \
	  echo "Repacking" ; \
	  tar -z -c -f gsoap_$(version).orig.tar.gz gsoap-$(version) ; \
	  echo "Cleaning up" ; \
	  rm -rf gsoap-$(version) ; \
	  echo "Done" ; \
	fi

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