#
# Copyright (c) 2007 Aconex.  All Rights Reserved.
#
ifndef _BUILDRULES_INCLUDED_
_BUILDRULES_INCLUDED_ = 1

include $(TOPDIR)/src/include/builddefs

clean clobber:
	@rm -f $(DIRT)
	@rm -fr $(DIRDIRT)
	$(SUBDIRS_MAKERULE)

realclean distclean: clean
	rm -f $(TOPDIR)/src/include/builddefs \
	      $(TOPDIR)/src/include/version.h \
	      $(TOPDIR)/src/libqmc/libqmc.pro \
	      $(TOPDIR)/src/libqwt/libqwt.pro \
	      $(TOPDIR)/src/dumptext/pmdumptext.pro \
	      $(TOPDIR)/src/query/pmquery.pro \
	      $(TOPDIR)/src/chart/pmchart.pro \
	      $(TOPDIR)/src/time/pmtime.pro \
	rm -f $(TOPDIR)/pcp-gui.lsm $(TOPDIR)/configure


# Never blow away subdirs
ifdef SUBDIRS
.PRECIOUS: $(SUBDIRS)
endif

src-pcp-gui : $(SRCFILES) $(SUBDIRS)
	@test ! -z "$$SRC_ROOT" || ( echo '$$SRC_ROOT not set ... bozo!' ; echo "... generally unsafe to run make src-pcp-gui outside the Makepkgs script"; exit 1 )
	@test -z "$$DIR" && DIR="."; \
	for f in `echo $^`; do \
	    if test -d $$f ; then \
		$(MAKE) $(MAKEOPTS) -j 1 DIR=$$DIR/$$f -C $$f $@ || exit $$?; \
	    else \
		$(ECHO) $$DIR/$$f; \
	    fi; \
	done

src-link-pcp-gui : $(SRCFILES) $(CONFFILES) $(SUBDIRS)
	@test ! -z "$$SRCLINK_ROOT" || ( echo '$$SRCLINK_ROOT not set ... bozo!' ; echo "... generally unsafe to run make src-link-pcp-gui outside the Makepkgs script"; exit 1 )
	@test -z "$$DIR" && DIR="."; \
	for f in `echo $^`; do \
	    if test -d $$f ; then \
		mkdir $$SRCLINK_ROOT/$$DIR/$$f || exit $$?; \
		$(MAKE) $(MAKEOPTS) -j 1 DIR=$$DIR/$$f -C $$f $@ || exit $$?; \
	    else \
		ln $$f $$SRCLINK_ROOT/$$DIR/$$f || exit $$?; \
	    fi; \
	done

ifeq ($(PKG_PLATFORM),darwin)
QTMAKE = $(QMAKE) -spec macx-g++ && make -f Makefile
MACBUILD = $(COMMAND).app/Contents
BINARY = $(MACBUILD)/MacOS/$(COMMAND)
LNMAKE = test ! -f $(BINARY) -o -L $(COMMAND) || $(LN_S) $(BINARY) $(COMMAND)
WINDOW = mac
endif
ifeq ($(PKG_PLATFORM),mingw)
QTMAKE = $(QMAKE) && $(MAKE) -f Makefile
BINARY = $(QT_RELEASE)/$(COMMAND)
LNMAKE =
WINDOW = win
endif
ifeq "$(findstring $(PKG_PLATFORM),darwin mingw)" ""
QTMAKE = +$(QMAKE) && $(MAKE) $(MAKEOPTS) -f Makefile
BINARY = $(COMMAND)
LNMAKE =
WINDOW = x11
endif

ifeq ($(PKG_PLATFORM),darwin)
INSTALL_DIRECTORY_HIERARCHY=\
	d=$(1); while [ "$$d" != "$(2)" -a "$$d" != "/" -a "$$d" != "." ] ; do \
                echo $$d; d=`dirname $$d`; done | sort | while read id; do \
                $(INSTALL) -m 755 -d $$id || exit 1; done

INSTALL_QT_RESOURCES=\
	printf "[Paths]\nPlugins=/Library/PCP/Plugins\n\n" > qt.conf; \
	$(INSTALL) -m 644 qt.conf $(1)/qt.conf; \
	rm qt.conf; \
	find frameworks -type d -name qt_menu.nib | while read nib; do \
                $(INSTALL) -m 755 -d $(1)/qt_menu.nib || exit 1; \
                find $$nib -type f | while read nibs; do \
                        f=`basename $$nibs`; \
                        $(INSTALL) -m 644 $$nibs $(1)/qt_menu.nib/$$f || exit 1; \
                done; \
        done

MAC_APPSUPPORT_DIR=/Library/PCP
MAC_FRAMEWORKS_DIR=$(MAC_APPSUPPORT_DIR)/Frameworks

# WARNING!
# This rule modified the binary it was given, once modified the
# binary cannot be used with this rule again.
# If the binary is installed then it's important to call this
# rule before calling install rule for the binary.
INSTALL_QT_FRAMEWORKS=\
    otool -L $(1) | awk '{if (NR != 1) {print $$1}}' |\
    egrep 'Qt.*\.framework' | while read qt; do \
	tdir=`dirname $$qt`; \
	install_name_tool -change $$qt $(MAC_FRAMEWORKS_DIR)/$$qt $(1);\
	$(call INSTALL_DIRECTORY_HIERARCHY,$(MAC_FRAMEWORKS_DIR)/$$tdir,/Library/PCP); \
	mkdir -p frameworks/$$tdir || exit 1; \
	fwqt="frameworks/$$qt"; \
	cp /Library/Frameworks/$$qt frameworks/$$qt || exit 1; \
	otool -L $$fwqt | awk '{if (NR != 1) {print $$1}}' |\
	egrep 'Qt.*\.framework' | while read dep; do \
	    install_name_tool -change $$dep $(MAC_FRAMEWORKS_DIR)/$$dep $$fwqt;\
	done; \
	$(INSTALL) frameworks/$$qt $(MAC_FRAMEWORKS_DIR)/$$qt; \
	if [ -d /Library/Frameworks/$$tdir/Resources ] ; then \
	    $(INSTALL) -d $(MAC_FRAMEWORKS_DIR)/$$tdir/Resources; \
	    (cd /Library/Frameworks/$$tdir; find Resources -type f) | \
		while read rf; do \
		    rfpath="$$tdir/$$rf"; rfd=`dirname $$rfpath`; \
		    fwpath="frameworks/$$rfpath"; brfd=`basename $$rfd`; \
		    mkdir -p frameworks/$$rfd || exit 1; \
		    cp /Library/Frameworks/$$rfpath $$fwpath || exit 1; \
		    [ $$brfd != qt_menu.nib ] || continue;  \
		    $(INSTALL) -d $(MAC_FRAMEWORKS_DIR)/$$rfd || exit 1; \
		    $(INSTALL) $$fwpath $(MAC_FRAMEWORKS_DIR)/$$rfpath;\
		done \
	fi; done
endif

ifdef ICONLINKS
iconlinks:
	@for l in $(ICONLINKS) ; do \
	    if [ ! -L $$l -a ! -f $$l ] ; then \
		$(LN_S) $(TOPDIR)/images/$$l $$l ; \
	    fi \
	done
endif

ifdef WINDOWLINKS
windowlinks:
	@for l in $(WINDOWLINKS) ; do \
	    if [ ! -L $$l -a ! -f $$l ] ; then \
		$(LN_S) $(WINDOW)_$$l $$l ; \
	    fi \
	done
endif

# Suffix rule to support transition for XML to PDF (books)
%.pdf:	%.xml
ifeq ($(BOOK_TOOLCHAIN),publican)
	$(PUBLICAN) build --langs=en-US --formats=pdf
endif
ifeq ($(BOOK_TOOLCHAIN),xmlto)
	$(XMLTO) --with-fop pdf $^
endif
ifeq ($(BOOK_TOOLCHAIN),dblatex)
	$(DBLATEX) --type pdf $^
endif

source :
	$(SOURCE_MAKERULE)

endif # _BUILDRULES_INCLUDED_

$(_FORCE):

.PHONY : depend
