#!/usr/bin/make -f

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

#                         2 .   8   ~20110207 -1
#regexp := ^Version: ([^.]+)\.([^.]+)~([^-]*)-(.*)
regexp := ([^.]+)\.([^.]+)~([^-]*)-(.*)
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 = a33ec96a70d7827d73ad160eceee81781cd529cd
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 $@ --with autotools-dev

# 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:
	support/reconfig
	ADA=gcc-4.9 ./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"

DIRS := static/src relocatable/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 relocatable/src/csupport.o -Isrc $(CFLAGS) -fPIC src/csupport.c
	gcc-4.9 -c -o static/src/csupport.o -Isrc $(CFLAGS) src/csupport.c
	gcc-4.9 -c -o relocatable/src/csupport.o -Isrc $(CFLAGS) -fPIC src/csupport.c
	cp src/config.adc static/src
	cp src/config.adc relocatable/src
	cp src/src.exclude static/src
	cp src/src.exclude relocatable/src

	$(MAKE) \
		LIBRARY_TYPE_OPTION="-XLIBRARY_TYPE=static" ADAFLAGS="$(ADAFLAGS) -v -margs -v -XLibversion=$(soversion) -XLIBRARY_TYPE=static -XLDFLAGS=\"$(LDFLAGS) -lpthread\"" \
		PARALLEL_GNATMAKE_FLAGS="$(BUILDER_OPTIONS)"
	$(MAKE) \
		LIBRARY_TYPE_OPTION="-XLIBRARY_TYPE=relocatable" ADAFLAGS="$(ADAFLAGS) -v -margs -v -XLibversion=$(soversion) -XLIBRARY_TYPE=relocatable -XLDFLAGS=\"$(LDFLAGS) -lpthread\"" \
		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

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])

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

	cp debian/addon/polyorb_start.gpr debian/tmp/usr/share/ada/adainclude/polyorb.gpr
	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 \
		echo $$lib; \
		cp $$lib debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH); \
		ln -sf `basename $$lib` \
			$(debian_libdir)/$(DEB_HOST_MULTIARCH)/`basename $$lib | cut -d. -f1`.so; \
		export libname=`basename $$lib | sed -e 's#libpolyorb\-##' | sed -e 's#\.so\.$(soversion)##'`; \
		echo $$libname; \
		if [ $$libname != libpolyorb ]; then \
			cat debian/project_template | sed -e s/VALUE_NAME/polyorb\-$$libname/ | sed -e s/VALUE_PRJ/`echo $$libname|sed -e s#\-#_#g`/ > debian/tmp/usr/share/ada/adainclude/polyorb/polyorb_src_`echo $$libname|sed -e s#\-#_#g`.gpr; \
			echo "with \"polyorb/polyorb_src_`echo $$libname|sed -e s#\-#_#g`\";" >> debian/tmp/usr/share/ada/adainclude/polyorb.gpr; \
		fi; \
	done;
	echo "with \"polyorb/polyorb_src\";" >> debian/tmp/usr/share/ada/adainclude/polyorb.gpr
	cat debian/addon/polyorb.gpr >> debian/tmp/usr/share/ada/adainclude/polyorb.gpr
	cat debian/project_template | sed -e s/VALUE_NAME/polyorb/ | sed -e s/_VALUE_PRJ// > debian/tmp/usr/share/ada/adainclude/polyorb/polyorb_src.gpr
	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
