#!/usr/bin/make -f

$(foreach line,$(shell sed -n '\
  s/^ gnat, gnat-\([0-9.]\+\),$$/gnat_version:=\1/p;\
  s/^Package: libpolyorb\([0-9.]\+\)$$/soversion:=\1/p;\
  s/^Package: \(libpolyorb[0-9.]\+-dev\)$$/dev_pkg:=\1/p;\
  ' debian/control),$(eval $(line)))
lib_pkg := libpolyorb$(soversion)

DEB_BUILD_MAINT_OPTIONS := hardening=+all
include /usr/share/dpkg/default.mk
include /usr/share/ada/debian_packaging-$(gnat_version).mk

# Disable style checks, do not treat warnings as errors.
# There is no point in fixing new GCC warnings in old polyorb sources.
ADAFLAGS += -gnatyN -gnatwn

# Ignore timestamps, only consider checksums.
BUILDER_OPTIONS += -m

# A patch allows to select the library version.
BUILDER_OPTIONS += -XLibversion=$(soversion)

revision = a33ec96a70d7827d73ad160eceee81781cd529cd
orig_dir = polyorb.orig
orig_tgz = polyorb.orig.tar.gz
orig_pkg_dir = polyorb-$(DEB_VERSION_UPSTREAM).orig
orig_pkg_tgz = polyorb_$(DEB_VERSION_UPSTREAM).orig.tar.gz
branch   = com.adacore.polyorb.debian
mtndb    = polyorb.mtn
mtn_uri :=  mtn://www.ada-france.org?com.adacore.polyorb.debian

%:
	dh $@ --with autotools-dev

# Ignore upstream tests, if any.
override_dh_auto_test:

.PHONY: get-orig-source
get-orig-source:
	if [ ! -e ../$(mtndb) ]; then \
		mtn -d../$(mtndb) db init; \
	fi; \
	TMPDIR=$$(mktemp --tmpdir --directory polyorb-get-orig-source-XXXXXXXXXX) && \
	mtn clone -d ../$(mtndb) $(mtn_uri) -k "" $$TMPDIR/$(orig_dir) && \
	chmod uog-x $$TMPDIR/$(orig_dir)/examples/corba/send/print.idl && \
	(cd $$TMPDIR && tar --create --gzip --exclude="_MTN" $(orig_dir)) > $(orig_tgz) ; \
	rm -rf $$TMPDIR
	@echo "The file $(orig_tgz) contains the latest Debian snapshot of upstream sources."

.PHONY: get-pkg-source
get-pkg-source:
	TMPDIR=$$(mktemp --tmpdir --directory polyorb-get-orig-source-XXXXXXXXXX) && \
	if [ ! -e ../$(mtndb) ]; then \
		mtn -d../$(mtndb) db init; \
	fi; \
	mtn -d../$(mtndb) -k "" pull $(mtn_uri); \
	mtn -d../$(mtndb) co -r$(revision) -b$(branch) $$TMPDIR/$(orig_pkg_dir) && \
	chmod uog-x $$TMPDIR/$(orig_pkg_dir)/examples/corba/send/print.idl && \
	(cd $$TMPDIR && tar --create --gzip --exclude="_MTN" $(orig_pkg_dir)) > $(orig_pkg_tgz)
	rm -rf $$TMPDIR
	@echo "The file $(orig_pkg_tgz) contains the pkg Debian of upstream sources."

.PHONY: override_dh_autoreconf
override_dh_autoreconf:
	support/reconfig

.PHONY: override_dh_auto_configure
override_dh_auto_configure:
 # Using full DEB_VERSION would change ALI checksums on every upload.
	dh_auto_configure -- \
		PACKAGE_VERSION="Debian $(DEB_VERSION_UPSTREAM)" \
		ADA=gcc-$(gnat_version) \
		$(foreach v,ADAFLAGS CFLAGS CPPFLAGS LDFLAGS,'$(v)=$($(v))') \
		--enable-debug \
		--with-appli-perso="corba moma dsa" \
		--with-proto-perso="giop" \
		--with-corba-services="event ir naming notification time"

DIRS := static/src relocatable/src
$(DIRS):
	mkdir -p $@

# TODO: the log mentions config.adc, is it seen in src/ with the ad[bs] ??
# Symlinks create less timestamps problems than copies.
symlink_old := config.adc src.exclude
symlink_new := $(addprefix relocatable/src/,$(symlink_old)) \
               $(addprefix      static/src/,$(symlink_old))
$(symlink_new): | $(DIRS)
	ln -s ../../src/$(notdir $@) $@
# Copy part of Makefile.in buliding C sources, but compile twice.
relocatable/src/csupport.o : src/csupport.c  | relocatable/src
	gcc-$(gnat_version) -c -o $@ -Isrc $(CFLAGS) $(CPPFLAGS) -fPIC $<
static/src/csupport.o      : src/csupport.c  |      static/src
	gcc-$(gnat_version) -c -o $@ -Isrc $(CFLAGS) $(CPPFLAGS)       $<

.PHONY: override_dh_auto_build-arch
override_dh_auto_build-arch: $(symlink_new) \
                             relocatable/src/csupport.o static/src/csupport.o
	$(MAKE) \
	  LIBRARY_TYPE_OPTIONS=-XLIBRARY_TYPE=static \
	  EXTRA_GNATMAKE_FLAGS='$(BUILDER_OPTIONS)'
  # Pass LDFLAGS here because ./configure and Makefile.in ignore them.
  # They should only be useful during relocatable builds.
  # -largs acts when building executables.
  # LDFLAGS acts on projects thanks to add-version-to-libraries.patch.
  # -lpthread is only useful on some architecturse, but does not hurt
  # -with --as-needed.
	$(MAKE) \
	  LIBRARY_TYPE_OPTIONS=-XLIBRARY_TYPE=relocatable \
	  EXTRA_GNATMAKE_FLAGS='$(BUILDER_OPTIONS) "-XLDFLAGS=$(LDFLAGS) -lpthread" -largs $(LDFLAGS) -lpthread'

# if you really want to run the test suite
.PHONY: run_testsuite
run_testsuite:
	$(MAKE) examples

	rm -f testsuite/test_failed
	$(MAKE) testsuite

	cp -f src/moma/destinations.conf testsuite
	$(MAKE) run_tests

# remove .o .ali and exe in examples dir before doc inclusion
examples_to_delete := \
  polyorb/polyorb-test-thread_pool_poa \
  polyorb/polyorb-test-no_tasking_poa \
  polyorb/polyorb-test-thread_pool \
  polyorb/polyorb-test-no_tasking \
  corba/all_types/ir_server \
  corba/all_types/client_moma corba/all_types/dynclient \
  corba/all_types/server \
  corba/all_types/client \
  corba/echo/dynserver \
  corba/echo/dynclient \
  corba/echo/server corba/echo/client \
  corba/all_functions/dynclient \
  corba/all_functions/server \
  corba/all_functions/client \
  corba/thread_pool_tester/server corba/thread_pool_tester/client \
  corba/random/server \
  corba/random/client \
  corba/send/listener \
  corba/send/send
.PHONY: clean_examples_dir
clean_examples_dir:
	find examples -name "*.o" \
	           -o -name "*.ali" \
	           -o -name "b~*.ad[sb]" -delete
	rm -f $(addprefix examples/,$(examples_to_delete))

ifeq (,$(filter nodoc,$(DEB_BUILD_OPTIONS)))

.PHONY: override_dh_auto_build-indep
override_dh_auto_build-indep: | $(DIRS)
	$(MAKE) -C docs

.PHONY: override_dh_auto_install-indep
override_dh_auto_install-indep:clean_examples_dir
	$(MAKE) -C docs DESTDIR=$(CURDIR)/debian/tmp install

endif

.PHONY: override_dh_auto_clean
override_dh_auto_clean:clean_examples_dir
	[ ! -f docs/Makefile ] || $(MAKE) -C docs clean
	[ ! -f Makefile ] || $(MAKE) distclean
	rm -rf static relocatable ali
	rm -f $(wildcard mkdir.flag b~*.ad[sb])

.PHONY: override_dh_auto_install-arch
override_dh_auto_install-arch:
	dh_auto_install

# These 2 lists will only be correct when dh calls the
# override_dh_install_target, but this is sufficient.
sublibs := $(patsubst debian/tmp/usr/lib/libpolyorb-%.so.$(soversion),%,\
           $(wildcard debian/tmp/usr/lib/libpolyorb-*.so.$(soversion)))
gpr_dir := debian/$(dev_pkg)/usr/share/ada/adainclude
gpr_template := debian/project_template
.PHONY: override_dh_install
override_dh_install:
	dh_install -p$(dev_pkg) \
	  usr/lib/polyorb/static/libpolyorb*.a \
	  usr/lib/$(DEB_HOST_MULTIARCH)
	dh_link -p$(dev_pkg) \
	  $(foreach l,libpolyorb $(addprefix libpolyorb-,$(sublibs)), \
	    usr/lib/$(DEB_HOST_MULTIARCH)/$(l).so.$(soversion) \
	    usr/lib/$(DEB_HOST_MULTIARCH)/$(l).so)
	mkdir -p $(gpr_dir)/polyorb
	cp debian/addon/polyorb_start.gpr $(gpr_dir)/polyorb.gpr
  # The list of libraries will be displayed here in the log.
	for sublib in $(sublibs); do \
	  nodash=`echo $$sublib | sed s/-/_/g` && \
	  sed $(gpr_template) \
	    -e s/PROJECT/polyorb_src_$$nodash/ \
	    -e s/VALUE_NAME/polyorb-$$sublib/ \
	    -e s/DEB_HOST_MULTIARCH/$(DEB_HOST_MULTIARCH)/ \
	    > $(gpr_dir)/polyorb/polyorb_src_$$nodash.gpr && \
	  echo "with \"polyorb/polyorb_src_$$nodash\";" >> $(gpr_dir)/polyorb.gpr ; \
	done
	cat debian/addon/polyorb.gpr >> $(gpr_dir)/polyorb.gpr
	sed $(gpr_template) \
	  -e s/PROJECT/polyorb_src/ \
	  -e s/VALUE_NAME/polyorb/ \
	  -e s/DEB_HOST_MULTIARCH/$(DEB_HOST_MULTIARCH)/ \
	  > $(gpr_dir)/polyorb/polyorb_src.gpr
	dh_install -p$(dev_pkg) -Xsoap -Xaws \
	  usr/include/polyorb/* \
	  usr/share/ada/adainclude/polyorb
	dh_install -p$(dev_pkg) \
	  usr/lib/polyorb/*.ali \
	  usr/lib/ada/adalib/polyorb
	dh_install -p$(dev_pkg) usr/bin/polyorb-config
	dh_install -p$(dev_pkg) usr/bin/iac
	dh_install -p$(dev_pkg) usr/bin/idlac
	dh_install -p$(dev_pkg) usr/bin/polyorb-config
	dh_install -p$(dev_pkg) usr/bin/po_gnatdist
	dh_install -p$(lib_pkg) \
	  usr/lib/libpolyorb*.so.$(soversion) \
	  usr/lib/$(DEB_HOST_MULTIARCH)
	dh_install --remaining-packages

.PHONY: override_dh_installman
override_dh_installman:
	dh_installman -p$(dev_pkg) debian/manpages/po_gnatdist.1 \
	  docs/iac.1 docs/idlac.1 docs/polyorb-config.1
	dh_installman --remaining-packages

.PHONY: override_dh_compress
override_dh_compress:
	dh_compress -p$(dev_pkg) -X.ads -X.adb -XMakefile
	dh_compress --remaining-packages
