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

export DEB_BUILD_MAINT_OPTIONS = hardening=+bindnow

# Add -D_GNU_SOURCE to CPPFLAGS to make strtod_l available on hurd-any
export DEB_CPPFLAGS_MAINT_APPEND = -D_GNU_SOURCE

version = 2.8.142

%:
	dh $@

override_dh_auto_configure-arch:
	find gsoap/samples/autotest/databinding/examples -name '*.xml' \
		-exec chmod a-x {} ';'

ifneq ($(DEB_BUILD_ARCH),$(DEB_HOST_ARCH))
	mkdir build-native
	cd build-native && ../configure
	$(MAKE) -C build-native

	dh_auto_configure -- \
		--with-soapcpp2=$(CURDIR)/build-native/gsoap/src/soapcpp2 \
		--with-wsdl2h=$(CURDIR)/build-native/gsoap/wsdl/wsdl2h \
		--enable-ipv6 \
		--enable-samples
else
	dh_auto_configure -- \
		--enable-ipv6 \
		--enable-samples
endif

override_dh_auto_build-arch:
	# Add /usr/share/gsoap to soapcpp2's default import path
	dh_auto_build -- SOAPCPP2_IMPORTPATH='-DSOAPCPP2_IMPORT_PATH="\"/usr/share/gsoap/import:/usr/share/gsoap\""'

override_dh_auto_clean-arch:
	dh_auto_clean
	rm -rf build-native

override_dh_auto_install-arch:
	dh_auto_install
	rm debian/tmp/usr/lib/*/*.la
	rm debian/tmp/usr/share/gsoap/custom/*.o
	rm debian/tmp/usr/share/gsoap/plugin/*.o
	install -p -m 644 debian/WS-README debian/tmp/usr/share/gsoap/WS

override_dh_autoreconf-indep:

override_dh_auto_configure-indep:
	chmod a-x gsoap/doc/fonts/*

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

override_dh_auto_clean-indep:
	rm -rf gsoap/doc/*/html
	rm -f gsoap/doc/*/doxygen_sqlite3.db

override_dh_auto_install-indep:

override_dh_installdocs-indep:
	dh_installdocs -X Doxyfile -X README.txt \
		-X doxygen_footer.html -X doxygen_header.html \
		-X guide/index.md -X guide/stdsoap2.h -X soapdoc2.html \
		-X gsoap/doc/GeniviaLogo2_trans_noslogan.png \
		-X gsoap/doc/genivia_content.css \
		-X genivia_tabs.css \
		-X gsoap/doc/index.html \
		-X doxygen_sqlite3.db

override_dh_compress-indep:
	dh_compress -X fonts

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 https://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 ; \
	  rm gsoap-$(version)/gsoap/samples/rest/person \
	     gsoap-$(version)/gsoap/samples/wcf/Basic/TransportSecurity/calculator \
	     gsoap-$(version)/gsoap/VisualStudio2005/wsdl2h/wsdl2h/soapcpp2.exe ; \
	  echo "Deleting pre-generated doxygen documentaion" ; \
	  rm -rf gsoap-$(version)/gsoap/doc/*/html ; \
	  echo "Deleting code with only non-free licenses" ; \
	  rm -rf gsoap-$(version)/gsoap/extras \
		 gsoap-$(version)/gsoap/mod_gsoap \
		 gsoap-$(version)/gsoap/Symbian \
		 gsoap-$(version)/gsoap/doc/apache \
		 gsoap-$(version)/gsoap/doc/wininet \
		 gsoap-$(version)/gsoap/doc/isapi ; \
	  echo "Deleting xsd and wsdl files" ; \
	  rm 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 pre-generated files" ; \
	  rm gsoap-$(version)/gsoap/samples/webserver/optC.c \
	     gsoap-$(version)/gsoap/samples/webserver/optH.h \
	     gsoap-$(version)/gsoap/samples/webserver/optStub.h ; \
	  rm gsoap-$(version)/gsoap/VisualStudio2005/wsdl2h/wsdl2h/wsdlC.cpp \
	     gsoap-$(version)/gsoap/VisualStudio2005/wsdl2h/wsdl2h/wsdlH.h \
	     gsoap-$(version)/gsoap/VisualStudio2005/wsdl2h/wsdl2h/wsdlStub.h ; \
	  echo "Deleting .DS_Store files" ; \
	  find gsoap-$(version) -name .DS_Store -exec rm {} ';' ; \
	  echo "Repacking" ; \
	  tar -z -c -f gsoap_$(version).orig.tar.gz gsoap-$(version) ; \
	  echo "Cleaning up" ; \
	  rm -rf gsoap-$(version) ; \
	  echo "Done" ; \
	fi
