#!/usr/bin/make -f
# -*- makefile -*-

DEB_HOST_MULTIARCH ?= $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)

-include /usr/share/dpkg/buildflags.mk

version = 2.8.17
# Bump the soversion whenever the new version is not backwards compatible
soversion = 5

configure: configure-stamp

configure-stamp:
	dh_testdir

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

	autoreconf --install --force

	CFLAGS='$(CFLAGS)' CXXFLAGS='$(CXXFLAGS)' \
	CPPFLAGS='$(CPPFLAGS)' LDFLAGS='$(LDFLAGS)' ./configure \
		--prefix=/usr \
		--libdir='$${prefix}/lib/$(DEB_HOST_MULTIARCH)' \
		--enable-ipv6 \
		--enable-samples

	touch $@

build: build-arch build-indep

build-arch: build-stamp

build-indep: build-stamp

build-stamp: configure-stamp
	dh_testdir

	$(MAKE) SOVERSION=$(soversion)

	for f in gsoap/doc/*/Doxyfile ; do \
	  ( cd `dirname $$f` ; doxygen Doxyfile ) ; \
	done

	touch $@

clean:
	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 -rf gsoap/doc/*/html

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

	rm -f build-stamp configure-stamp

	dh_clean

install: build-stamp
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs

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

	install -p -m 644 debian/WS-README \
	  $(CURDIR)/debian/tmp/usr/share/gsoap/WS

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

binary: binary-arch binary-indep

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

binary-indep: install
	dh_testdir
	dh_testroot
	dh_installdocs -i
	find $(CURDIR)/debian/gsoap-doc -name Doxyfile -exec rm {} ';'
	dh_installchangelogs -i
	dh_install -i --fail-missing
	dh_installman -i
	dh_link -i
	dh_compress -i
	dh_fixperms -i
	dh_installdeb -i
	dh_gencontrol -i
	dh_md5sums -i
	dh_builddeb -i

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 precompiled binaries" ; \
	  rm -rf gsoap-$(version)/gsoap/bin \
		 gsoap-$(version)/gsoap/ios_plugin/examples/*/build \
		 gsoap-$(version)/gsoap/samples/calc_vs2005/calc_vs2005/soapcpp2.exe \
		 gsoap-$(version)/gsoap/VisualStudio2005/wsdl2h/wsdl2h/soapcpp2.exe ; \
	  ( cd gsoap-$(version)/gsoap/samples/link++ ; make distclean ) ; \
	  ( cd gsoap-$(version)/gsoap/samples/wcf/WS/DualHttp ; make distclean ) ; \
	  echo "Deleting pre-generated doxygen documentaion" ; \
	  rm -rf gsoap-$(version)/gsoap/doc/*/html \
		 gsoap-$(version)/gsoap/ios_plugin/ios_plugin/docs/html \
		 gsoap-$(version)/gsoap/samples/databinding/html ; \
	  echo "Deleting code with only non-free licenses" ; \
	  rm -rf gsoap-$(version)/gsoap/extras \
		 gsoap-$(version)/gsoap/mod_gsoap \
		 gsoap-$(version)/gsoap/Symbian ; \
	  echo "Deleting xsd and wsdl files" ; \
	  rm -rf gsoap-$(version)/gsoap/WS/*.xsd \
		 gsoap-$(version)/gsoap/WS/*.wsdl \
	         gsoap-$(version)/gsoap/samples/autotest/*.xsd \
		 gsoap-$(version)/gsoap/samples/autotest/*.wsdl \
	         gsoap-$(version)/gsoap/uddi2/*.xsd \
		 gsoap-$(version)/gsoap/uddi2/*.wsdl ; \
	  echo "Deleting .dotfiles" ; \
	  find gsoap-$(version) -name .DS_Store -exec rm {} ';' ; \
	  rm gsoap-$(version)/gsoap/samples/wcf/WS/DualHttp/.calculator.cpp.swp ; \
	  echo "Repacking" ; \
	  tar -z -c -f gsoap_$(version).orig.tar.gz gsoap-$(version) ; \
	  echo "Cleaning up" ; \
	  rm -rf gsoap-$(version) ; \
	  echo "Done" ; \
	fi

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