#!/usr/bin/make -f

#                         2 .   6    .  0    ~20090423-1
regexp := ^Version: ([^.]+)\.([^.]+)\.([^~]+)~([^-]*)-(.*)
major  := $(shell dpkg-parsechangelog | grep "^Version: " | sed -r 's/$(regexp)/\1/')
minor  := $(shell dpkg-parsechangelog | grep "^Version: " | sed -r 's/$(regexp)/\2/')
micro  := $(shell dpkg-parsechangelog | grep "^Version: " | sed -r 's/$(regexp)/\3/')
date   := $(shell dpkg-parsechangelog | grep "^Version: " | sed -r 's/$(regexp)/\4/')
upload := $(shell dpkg-parsechangelog | grep "^Version: " | sed -r 's/$(regexp)/\5/')

debian_libdir = debian/tmp/usr/lib

revision = t:polyorb-$(major).$(minor).$(micro)~$(date)-1
orig_dir = polyorb-$(major).$(minor).$(micro)~$(date).orig
orig_tgz = polyorb_$(major).$(minor).$(micro)~$(date).orig.tar.gz
branch   = com.adacore.polyorb.debian
mtndb    = polyorb.mtn

# number of parallel build processes to use (default: #cpus),
# override by specifying DEB_BUILD_OPTIONS="parallel=n".
cpus := $(shell getconf _NPROCESSORS_ONLN)
ifneq (,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
	cpus = $(patsubst parallel=%,%,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
endif

gen-orig-source:
	if [ ! -e ../$(mtndb) ]; then \
		mtn -d../$(mtndb) db init; \
	fi; \
	mtn -d../$(mtndb) pull www.ada-france.org '$(branch)'; \
	cd .. && \
	mtn -d$(mtndb) co -r$(revision) -b$(branch) $(orig_dir) && \
	rm -r $(orig_dir)/_MTN && \
	tar czf $(orig_tgz) $(orig_dir) && \
	rm -r $(orig_dir)

configure-stamp: configure
	dh_testdir

	ADA=gcc-4.4 ./configure --prefix=`pwd`/debian/tmp \
		--enable-debug \
		--with-appli-perso="corba moma dsa" \
		--with-corba-services="event ir naming notification time"

	touch $@

configure: configure.ac
	support/reconfig

dirs:
	mkdir -p STATIC/src
	mkdir -p DYNAMIC/src
	touch $@

build-arch: build-stamp
build-stamp: configure-stamp dirs
	dh_testdir

	gcc -c -o STATIC/src/csupport.o -Isrc -g -O2 src/csupport.c
	gcc -c -o DYNAMIC/src/csupport.o -Isrc -g -O2 -fPIC src/csupport.c
	$(MAKE) \
		ADAFLAGS="-XLibversion=$(major) -XLibtype=STATIC" \
		PARALLEL_GNATMAKE_FLAGS="-j$(cpus)"
	$(MAKE) \
		ADAFLAGS="-XLibversion=$(major) -XLibtype=DYNAMIC" \
		PARALLEL_GNATMAKE_FLAGS="-j$(cpus)"

	$(MAKE) examples
	$(MAKE) testsuite
	$(MAKE) run_tests
	touch $@

build-doc: build-doc-stamp
build-doc-stamp: configure-stamp dirs
	dh_testdir
	$(MAKE) -C docs
	$(MAKE) -C docs prefix=$(CURDIR)/debian/tmp/usr install-data-local
	touch $@

clean:
	dh_testdir
	dh_testroot
	dh_clean build-stamp
	dh_clean configure-stamp config.log

	[ ! -f docs/Makefile ] || $(MAKE) -C docs clean
	[ ! -f Makefile ] || $(MAKE) distclean

	rm -rf STATIC DYNAMIC dirs
	rm -f `find . -name mkdir.flag`
	rm -f `find . -name "b~*.ad[sb]"`
	rm -f compilers/iac/mknodes
	rm -f compilers/idlac/idl_fe-tree.ad*
	rm -f support/native-linker
	rm -f support/native-adacompiler
	rm -f support/texinfo.tex
	rm -f support/ltmain.sh
	rm -f support/config.guess
	rm -f support/config.sub
	rm -f support/libtool.m4
	rm -f docs/Makefile.in
	rm -f docs/MANIFEST
	rm -f src/config.h*
	rm -f src/stamp-h1
	rm -f aclocal.m4
	rm -f configure
	rm -f libtool
	for i in `cat debian/files_to_clean`; do rm -f $$i; done

	dh_clean

install: build-arch
	dh_testdir
	dh_testroot
	dh_prep
	dh_installdirs

	$(MAKE) prefix=$(CURDIR)/debian/tmp/usr install

	cp debian/addon/polyorb.gpr debian/tmp/usr/share/ada/adainclude

	for lib in `ls $(debian_libdir)/lib*.so.$(major)`; do \
		ln -sf `basename $$lib` \
			$(debian_libdir)/`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]

binary-indep: build-doc
	dh_testdir -i
	dh_testroot -i
	dh_installchangelogs -i
	dh_installdocs -i
	dh_installexamples -i
	dh_installman -i
	dh_install -i
	dh_lintian -i
	dh_compress -i
	dh_fixperms -i
	dh_makeshlibs -i
	dh_installdeb -i
	dh_shlibdeps -i
	dh_gencontrol -i
	dh_md5sums -i
	dh_builddeb -i

binary-arch: build install
	dh_testdir -a
	dh_testroot -a
	dh_installchangelogs -a
	dh_installdocs -a
	dh_installexamples -a
	dh_installman -a
	dh_install -a
	dh_lintian -a
	dh_strip --dbg-package=libpolyorb-dbg -a
	dh_compress -X.ads -X.adb -XMakefile -a
	dh_fixperms -a
	dh_makeshlibs -a
	dh_installdeb -a
	dh_shlibdeps -a
	dh_gencontrol -a
	dh_md5sums -a
	dh_builddeb -a

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