#!/usr/bin/make -f

include /usr/share/dpkg/default.mk
include /usr/share/ada/debian_packaging.mk

#                         2 .   8   ~20110207 -1
regexp := ^Version: ([^.]+)\.([^.]+)~([^-]*)-(.*)
major  := $(shell echo $(DEB_VERSION) | sed -r 's/$(regexp)/\1/')
minor  := $(shell echo $(DEB_VERSION) | sed -r 's/$(regexp)/\2/')
date   := $(shell echo $(DEB_VERSION) | sed -r 's/$(regexp)/\3/')
upload := $(shell echo $(DEB_VERSION) | sed -r 's/$(regexp)/\4/')
aliversion := $(shell sed -n -r 's/^Package: libpolyorb(.*)-dev$$/\1/p' debian/control)
soversion := $(shell sed -n -r 's/^Package: libpolyorb([^-]+)$$/\1/p' debian/control)

debian_libdir = debian/tmp/usr/lib

revision = 0f4393e4f6d42f8d093a29b8fa35aebf4eb4a5f4
orig_dir = polyorb.orig
orig_tgz = polyorb.orig.tar.gz
orig_pkg_dir = polyorb-$(major).$(minor)~$(date).orig
orig_pkg_tgz = polyorb_$(major).$(minor)~$(date).orig.tar.gz
branch   = com.adacore.polyorb.debian
mtndb    = polyorb.mtn
mtn_uri :=  mtn://www.ada-france.org?com.adacore.polyorb.debian

binary binary-arch binary-indep build build-arch build-indep install clean:
	dh $@

# Ignore upstream tests, if any.
override_dh_auto_test:

test_env:
	@echo "major=" $(major)
	@echo "minor=" $(minor)
	@echo "date=" $(date)
	@echo "aliversion=" $(aliversion)
	@echo "soversion=" $(soversion)
	@echo "upload=" $(upload)

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."

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."

override_dh_auto_configure: configure
	ADA=gnatgcc ./configure --prefix=`pwd`/debian/tmp \
		--enable-debug \
		--with-appli-perso="corba moma dsa" \
		--with-proto-perso="giop" \
		--with-corba-services="event ir naming notification time"

configure: configure.ac
	support/reconfig

DIRS := STATIC/src DYNAMIC/src
$(DIRS):
	mkdir -p $@

EXAMPLES_EXE = polyorb/polyorb-test-thread_pool_poa
EXAMPLES_EXE += polyorb/polyorb-test-no_tasking_poa
EXAMPLES_EXE += polyorb/polyorb-test-thread_pool
EXAMPLES_EXE += polyorb/polyorb-test-no_tasking
EXAMPLES_EXE += corba/all_types/ir_server
EXAMPLES_EXE += corba/all_types/client_moma corba/all_types/dynclient
EXAMPLES_EXE += corba/all_types/server corba/all_types/client
EXAMPLES_EXE += corba/echo/dynserver corba/echo/dynclient
EXAMPLES_EXE += corba/echo/server corba/echo/client
EXAMPLES_EXE += corba/all_functions/dynclient
EXAMPLES_EXE += corba/all_functions/server corba/all_functions/client
EXAMPLES_EXE += corba/thread_pool_tester/server corba/thread_pool_tester/client
EXAMPLES_EXE += corba/random/server corba/random/client
EXAMPLES_EXE += corba/send/listener corba/send/send

override_dh_auto_build-arch: | $(DIRS)
	$(CC) -c -o STATIC/src/csupport.o -Isrc $(CFLAGS) src/csupport.c
	$(CC) -c -o DYNAMIC/src/csupport.o -Isrc $(CFLAGS) -fPIC src/csupport.c
	cp src/src.exclude STATIC/src
	cp src/src.exclude DYNAMIC/src

	$(MAKE) \
		ADAFLAGS="-XLibversion=$(soversion) -XLibtype=STATIC $(ADAFLAGS)" \
		PARALLEL_GNATMAKE_FLAGS="$(BUILDER_OPTIONS)"
	$(MAKE) \
		ADAFLAGS="-XLibversion=$(soversion) -XLibtype=DYNAMIC $(ADAFLAGS)" \
		PARALLEL_GNATMAKE_FLAGS="$(BUILDER_OPTIONS)"

# if you really want to run the test suite
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
clean_examples_dir:
	find examples -name "*.o" -delete
	find examples -name "*.ali" -delete
	find examples -name "b~*.ad[sb]" -delete
	for i in $(EXAMPLES_EXE); do \
	   rm -f examples/$$i; \
	done

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

override_dh_auto_install-indep:clean_examples_dir
	$(MAKE) -C docs prefix=$(CURDIR)/debian/tmp/usr install-data-local

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

override_dh_auto_install-arch:
	$(MAKE) prefix=$(CURDIR)/debian/tmp/usr install

	cp debian/addon/polyorb.gpr debian/tmp/usr/share/ada/adainclude
	mkdir -p debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)
	cp $(debian_libdir)/*.a debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)

	for lib in `ls $(debian_libdir)/lib*.so.$(soversion)`; do \
		cp $$lib debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH); \
		ln -sf `basename $$lib` \
			$(debian_libdir)/$(DEB_HOST_MULTIARCH)/`basename $$lib | cut -d. -f1`.so; \
	done;

	rm debian/tmp/usr/share/ada/adainclude/polyorb/*soap*.ad[sb]
	rm debian/tmp/usr/share/ada/adainclude/polyorb/*aws*.ad[sb]

override_dh_compress-arch:
	dh_compress -X.ads -X.adb -XMakefile

override_dh_strip-arch:
	dh_strip --dbg-package=libpolyorb-dbg

.PHONY: build clean binary-indep binary-arch binary install
