#!/usr/bin/make -f

# debian/rules file for the ACE Debian GNU/Linux package
# 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-2008 Thomas Girard <thomas.g.girard@free.fr>

# This is the debhelper compatibility version to use.
export DH_COMPAT=4

INSTALL := install -o root -g root
INSTALL_755 := $(INSTALL) -m 755

export DEB_HOST_GNU_TYPE ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
export DEB_BUILD_GNU_TYPE ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)
export DEB_HOST_ARCH ?= $(shell dpkg-architecture -qDEB_HOST_ARCH)

ifeq ($(DEB_BUILD_GNU_TYPE),$(DEB_HOST_GNU_TYPE))
confflags += --build $(DEB_HOST_GNU_TYPE)
else
confflags += --build $(DEB_BUILD_GNU_TYPE) \
             --host $(DEB_HOST_GNU_TYPE)
endif

TCL_HOME := /usr/lib
TK_HOME  := /usr/lib

ACE_SUBDIR := ACE_wrappers
ACE_ARCHIVE := $(shell ls -1 ACE+TAO+CIAO-src-*.tar.bz2 2>/dev/null || echo ace-archive-missing)
MPC_SUBDIR := MPC
MPC_ARCHIVE := $(shell ls -1 MPC*.tar.gz 2>/dev/null || echo mpc-archive-missing)

MWCFLAGS := -type automake -features "ssl=1,zlib=1,zzip=1,xt=1,tk=1,fl=1,qt=1,fox=1" -noreldefs

MAN1 := debian/Basic_Logging_Service.1 \
	debian/Event_Logging_Service.1 \
	debian/Notify_Logging_Service.1 \
	debian/RTEvent_Logging_Service.1 \
	debian/TAO_ORB_Options.1 \
	debian/mpc-ace.1
MAN5 :=

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

ifneq (mpc-archive-missing,$(MPC_ARCHIVE))
export MPC_ROOT := $(shell pwd)/$(MPC_SUBDIR)
endif
export ACE_ROOT := $(shell pwd)/$(ACE_SUBDIR)
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 dpatch is only a place holder. The `prepatch' targets stops
# here by default.
PREPATCH_MARK := 50-prepatch-dummy-mark.dpatch
UNTIL := $(PREPATCH_MARK)

# Where we install every file before calling dh_install
DT := debian/tmp

CONFIG := objdir/ace/config.h

extract-stamp: $(ACE_ARCHIVE)
	tar -xjf $<
	cp debian/platform_macros.GNU.in $(ACE_ROOT)/include/makeinclude
	touch $@

.PHONY: extract
extract: extract-stamp

$(MPC_ROOT): $(MPC_ARCHIVE)
	tar -xzf $<
	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/%.5: debian/%.sgml
	docbook-to-man $< > $@

# Prepare a ACE_wrappers directory where most of the patches are applied.
ifneq (mpc-archive-missing,$(MPC_ARCHIVE))
prepatch-stamp: extract-stamp $(MPC_ROOT)
else
prepatch-stamp: extract-stamp
endif
	dpatch apply-until $(UNTIL)
	cd $(ACE_ROOT) &&    bin/mwc.pl -noreldefs $(MWCFLAGS) ACE.mwc
	cd $(TAO_ROOT) && ../bin/mwc.pl -noreldefs $(MWCFLAGS) TAO.mwc
	patch -p0 < debian/patches/09-autoconf-fix-no-base-include-headers.diff
	patch -p0 < debian/patches/21-generate-platform_macros.GNU.diff
	touch $@

.PHONY: prepatch
prepatch: prepatch-stamp

pl-stamp: extract-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 $@

patch-stamp: pl-stamp autotools-stamp
	dpatch apply-all
	chmod +x $(ACE_ROOT)/configure $(ACE_ROOT)/TAO/configure
	touch $@

.PHONY: patch
patch: patch-stamp

unpatch: patch-stamp
	dpatch unpatch
	rm -f $<

ifeq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
      CXXFLAGS+=-O2
endif

autotools-stamp: prepatch-stamp
	cd $(ACE_ROOT) && bin/bootstrap
	touch $@

.PHONY: autotools
autotools: autotools-stamp

build: build-stamp manpages-stamp

manpages-stamp: $(MAN1) $(MAN5)
	touch $@

doxygen-stamp: extract-stamp
	cd $(ACE_ROOT) && bin/generate_doxygen.pl -is_release
	find $(ACE_ROOT)/html/ace $(DOXYGEN_FILES_TO_REMOVE) | xargs -r rm -f
	find $(ACE_ROOT)/html/tao $(DOXYGEN_FILES_TO_REMOVE) | xargs -r rm -f
	touch $@

.PHONY: doxygen
doxygen: doxygen-stamp

build-stamp: configure-stamp
	dh_testdir
	mkdir -p objdir/ace/QtReactor
	$(MAKE) -C objdir
	$(MAKE) -C objdir/TAO

	touch $@

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

install: build
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs

	$(MAKE) -C objdir install DESTDIR=$(CURDIR)/$(DT)
	$(MAKE) -C objdir/TAO install DESTDIR=$(CURDIR)/$(DT)

#	Rename the catior utility, it already exists in another package
	mv $(DT)/usr/bin/catior $(DT)/usr/bin/tao-catior

#       Multiple README
	cp $(TAO_ROOT)/utils/catior/README $(TAO_ROOT)/utils/catior/README.catior
	cp $(TAO_ROOT)/utils/nslist/README $(TAO_ROOT)/utils/nslist/README.nslist

#	We provide scripts replacement for tao_idl and tao_ifr ; they
#	automatically define ACE_ROOT and TAO_ROOT
	mv $(DT)/usr/bin/tao_idl $(DT)/usr/bin/tao_idl.real
	$(INSTALL_755) -D debian/tao_idl $(DT)/usr/bin/tao_idl
	mv  $(DT)/usr/bin/tao_ifr  $(DT)/usr/bin/tao_ifr.real
	$(INSTALL_755) -D debian/tao_ifr $(DT)/usr/bin/tao_ifr

#       Copy MPC files.
	$(INSTALL_755) -D $(ACE_ROOT)/bin/mpc.pl $(DT)/usr/bin/mpc-ace
	$(INSTALL_755) -D $(ACE_ROOT)/bin/mwc.pl $(DT)/usr/bin/mwc-ace

#	Be lintian clean, fix permissions on examples
	-chmod -x `find $(TAO_ROOT)/examples/ -perm 755 ! -type d -a ! \( -name "*.pl" -o -name "*.sh" \)`

#       We don't install .la files
	find $(DT) -name '*.la' -print0 | xargs -0 rm
	rm -f $(DT)/usr/bin/TAO_Service
	dh_install --sourcedir=$(DT) --fail-missing

binary-indep: build install doxygen-stamp
	dh_testdir -i
	dh_testroot -i
	dh_installdocs -i
	dh_installexamples -i
	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 -pgperf-ace $(ACE_ROOT)/apps/gperf/ace_gperf.info
	dh_installchangelogs -a $(ACE_ROOT)/ChangeLog
	dh_installchangelogs -pgperf-ace $(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

configure-stamp: patch-stamp
	mkdir -p objdir
	cd objdir && ../ACE_wrappers/configure $(confflags) --prefix=/usr \
           --disable-tao-examples --disable-tao-tests \
           --disable-ace-examples --disable-ace-tests \
           --enable-fl-reactor --enable-qt-reactor --enable-xt-reactor \
           --enable-tk-reactor \
           --with-fox-config=/usr/bin --enable-fox-reactor \
           --with-tclconfig=$(TCL_HOME) --with-tkconfig=$(TK_HOME) \
           --with-zlib --with-zzip \
           --enable-ipv6 --enable-ipv4-ipv6 \
           --enable-inline \
           --enable-pthreads \
           --enable-symbol-visibility \
           --enable-ace-reactor-notification-queue
	echo '#define TAO_IDL_INCLUDE_DIR "/usr/include/tao"' >> $(CONFIG)

	# bz#3138
	echo "#define ACE_HAS_RECURSIVE_MUTEXES" >> $(CONFIG)

	echo "#define ACE_HAS_SCANDIR" >> $(CONFIG)
	echo "#define ACE_SCANDIR_CMP_USES_CONST_VOIDPTR" >> $(CONFIG)

	# bz#3144
	echo "#define ACE_HAS_SENDFILE" >> $(CONFIG)

	echo "#define ACE_LACKS_WCSICMP" >> $(CONFIG)

	echo "#define ACE_POSIX_SIG_PROACTOR" >> $(CONFIG)

	# TODO: ACE_GETNAME_RETURNS_RANDOM_SIN_ZERO?

	# Remove PACKAGE_* from the generated config.h
	sed -i -e '/package/I D' objdir/ace/config.h
	touch $@

.PHONY: configure
configure: configure-stamp

.PHONY: build clean binary-indep binary-arch binary install unpatch source diff
