#!/usr/bin/make -f

# debian/rules file for ACE+TAO Debian GNU/Linux packages
# written February 2002 by Ossama Othman <ossama@debian.org>
# Modified August 2003 by Brian Nelson <pyro@debian.org>
# Copyright (C) 2004  Raphael Bossek <bossekr@debian.org>
# Copyright © 2005-2010 Thomas Girard <thomas.g.girard@free.fr>

INSTALL_755 := install -o root -g root -m 755

ACE_SUBDIR := ACE_wrappers
ACE_ARCHIVE := $(shell ls -1 ACE+TAO+CIAO-src-*.tar.bz2 2>/dev/null || echo ace-archive-missing)
MWCFLAGS := -type gnuace -noreldefs
ACE_MAKE_FLAGS := versioned_so=2

MAN1 := debian/ace_netsvcs.1 \
	debian/tao_catior.1 \
	debian/tao_nslist.1 \
	debian/tao_nsadd.1 \
	debian/tao_nsdel.1 \
	debian/tao_tls_basic.1 \
	debian/tao_tls_event.1 \
	debian/tao_tls_notify.1 \
	debian/tao_tls_rtevent.1 \
	debian/TAO_ORB_Options.1 \
	debian/mpc-ace.1
# Where we install every file before calling dh_install
DT := debian/tmp

DOXYGEN_FILES_TO_REMOVE := -name '*.md5' -o \
                           -name '*.dot' -o \
                           -name '*.idx' -o \
                           -name '*.map' -o \
                           -name '*.tag' -o \
                           -name 'index.hh?'

export ACE_ROOT := $(shell pwd)/$(ACE_SUBDIR)
export MPC_ROOT := $(ACE_ROOT)/MPC
export TAO_ROOT := $(ACE_ROOT)/TAO
export CIAO_ROOT := $(TAO_ROOT)/CIAO
export LD_LIBRARY_PATH := $(LD_LIBRARY_PATH):$(ACE_ROOT)/lib
export QTDIR := /usr/share/qt4

# This dpatches are only a place holders. The `prepatch' and `postbuild' targets stops
# here by default.
PREPATCH_MARK := 50-prepatch-dummy-mark.dpatch
POST_BUILD_MARK := 75-post-build-dummy-mark.dpatch
UNTIL_BUILD := $(PREPATCH_MARK)
UNTIL_POST_BUILD := $(POST_BUILD_MARK)

.NOTPARALLEL: debian/mpc-ace.sgml debian/%.1 prepatch extract doxygen clean install build binary-indep binary-arch binary
.PHONY: doxygen patch unpatch prepatch extract build clean binary-indep binary-arch binary install source diff configure

extract: extract-stamp
extract-stamp: $(ACE_ARCHIVE)
	tar -xjf $<
	cp debian/platform_macros.GNU $(ACE_ROOT)/include/makeinclude
	cp debian/config.h $(ACE_ROOT)/ace/config.h
	cp debian/default.features $(ACE_ROOT)/bin/MakeProjectCreator/config
	# We need to delete this one to prevent an empty library generation
	rm -f $(ACE_ROOT)/ace/ace_qt3reactor.mpc
	touch $@

debian/mpc-ace.sgml: ACE_wrappers/MPC/docs/MPC.sgml
	sed -e 's/mpc\.pl/mpc-ace/g' -e 's/mwc\.pl/mwc-ace/g' $< > $@

debian/%.1: debian/%.sgml
	docbook-to-man $< > $@

# Prepare a ACE_wrappers directory where most of the patches are applied.
prepatch: prepatch-stamp
prepatch-stamp: extract-stamp
	dpatch apply-until $(UNTIL_BUILD)
	touch $@

pl-stamp: prepatch-stamp
	for fn in `find "$(ACE_ROOT)" -name "*.pl"`; do (echo '#!/usr/bin/perl'; cat "$$fn") > "$${fn}T"; mv "$${fn}T" "$$fn"; chmod a+x "$$fn"; done
	touch $@

configure: configure-stamp
configure-stamp: prepatch-stamp pl-stamp
	cp debian/ACE-DPKG.mwc $(ACE_ROOT)
	cd $(ACE_ROOT) &&  ./bin/mwc.pl $(MWCFLAGS) ACE-DPKG.mwc
	cp debian/TAO-DPKG.mwc $(TAO_ROOT)
	cd $(TAO_ROOT) &&  ./../bin/mwc.pl $(MWCFLAGS) TAO-DPKG.mwc
	touch $@

patch: patch-stamp
patch-stamp: configure-stamp
	dpatch apply-until $(UNTIL_POST_BUILD)
	touch $@

unpatch:
	dpatch deapply-all
	rm -rf patch-stamp debian/patched

doxygen: doxygen-stamp
doxygen-stamp: build-stamp
	cd $(ACE_ROOT) && bin/generate_doxygen.pl -is_release -exclude_ciao
	find $(ACE_ROOT)/html/libace-doc $(DOXYGEN_FILES_TO_REMOVE) | xargs -r rm -f
	find $(ACE_ROOT)/html/libtao-doc $(DOXYGEN_FILES_TO_REMOVE) | xargs -r rm -f
#	Somehow search/ is still generated, even if we don't want it.
	find $(ACE_ROOT)/html -type d -name search | xargs -r rm -Rf
	touch $@

build: build-stamp manpages-stamp
build-stamp: patch-stamp
	dh_testdir
	cd $(ACE_ROOT) && $(MAKE) $(ACE_MAKE_FLAGS)
	cd $(TAO_ROOT) && $(MAKE) $(ACE_MAKE_FLAGS)
	touch $@

manpages-stamp: $(MAN1)
	touch $@

clean:
	dh_testdir
	rm -rf $(ACE_SUBDIR) debian/patched $(MAN1) *-stamp debian/mpc-ace.sgml
	dh_clean

post-build-patch: post-build-patch-stamp
post-build-patch-stamp: build-stamp
	dpatch apply-all
	touch $@


install: post-build-patch
	dh_testdir
	dh_testroot
	dh_prep
	dh_installdirs
#	Install all files in debian/tmp
	cd $(ACE_ROOT) && $(MAKE) install DESTDIR=$(CURDIR)/$(DT)/ INSTALL_PREFIX=/usr install_rpath=0 $(ACE_MAKE_FLAGS)
	cd $(TAO_ROOT) && $(MAKE) install DESTDIR=$(CURDIR)/$(DT)/ INSTALL_PREFIX=/usr install_rpath=0 $(ACE_MAKE_FLAGS)

	mkdir -p $(DT)/usr/share/man/man1
	cp $(ACE_ROOT)/apps/gperf/ace_gperf.1 $(DT)/usr/share/man/man1

#	Relocate .idl and .pidl files
	cd $(DT) && for f in $$(find usr/include -name '*.idl' -o -name '*.pidl') ; do \
	   g=$$(echo $${f%%include*}share/idl/$${f##*usr/include/}) ; \
	   mkdir -p $$(dirname $$g) ; \
	   mv $$f $$g ; \
	   done

#	We provide scripts replacement for tao_idl and tao_ifr; they
#	automatically define ACE_ROOT and TAO_ROOT
	mkdir -p $(DT)/usr/lib/ace/TAO
	mv $(DT)/usr/bin/tao_idl $(DT)/usr/bin/tao_ifr $(DT)/usr/lib/ace/TAO

	$(INSTALL_755) -D debian/tao_idl $(DT)/usr/bin/tao_idl
	$(INSTALL_755) -D debian/tao_ifr $(DT)/usr/bin/tao_ifr

	dh_install --sourcedir=$(DT) --list-missing

binary-indep: build install doxygen-stamp
	dh_testdir -i
	dh_testroot -i
	dh_installdocs -i

	chmod -x debian/libtao-doc/usr/share/doc/libtao-doc/examples/RTCORBA/Activity/svc.conf.server
	chmod -x debian/libtao-doc/usr/share/doc/libtao-doc/examples/Simulator/DOVEBrowser/make.bat
	chmod -x debian/libtao-doc/usr/share/doc/libtao-doc/examples/RTCORBA/Activity/svc.conf.client

	dh_installman -i
	dh_installchangelogs -i $(ACE_ROOT)/ChangeLog
	dh_perl -i
	dh_link -i
	dh_compress -i -Xexamples -Xtutorials
	dh_fixperms -i
	dh_installdeb -i
	dh_gencontrol -i
	dh_md5sums -i
	dh_builddeb -i

binary-arch: build install
	dh_testdir -a
	dh_testroot -a
	dh_installdocs -a -A
	dh_installman -a
	dh_installinfo -pace-gperf $(ACE_ROOT)/apps/gperf/ace_gperf.info
	dh_installchangelogs -a $(ACE_ROOT)/ChangeLog
	dh_installchangelogs -pace-gperf $(ACE_ROOT)/apps/gperf/ChangeLog
	dh_strip -a
	dh_link -a
	dh_compress -a
	dh_fixperms -a
	dh_makeshlibs -a -V
	dh_installdeb -a
	dh_shlibdeps -a -l `ls -1 debian/lib*.install | sed -e 's#\(.*\)\.install#\1/usr/lib#' | tr '\n' ':' | sed -e 's#:$$##'`
	dh_gencontrol -a
	dh_md5sums -a
	dh_builddeb -a

source diff:
	@echo >&2 'source and diff are obsolete - use dpkg-source -b'; false

binary: binary-indep binary-arch
