#  
# Makefile for Brutus test/sample code.
# Copyright (C) 2004-2007 OMC Denmark ApS.
# 
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.


# To let sub make decide whether they are invoked 
# directly or from this Makefile
TOP_INVOKED := yes

# Directories
IDL_ROOT := $(shell pwd)/../../brutus_loader/idl
IDL_OUTPUT_DIR := $(shell pwd)/idl_output

# Compilation directives
CC := gcc
OPT = -Os
DEBUG := -ggdb -O1
CFLAGS := -Wall -DORBIT2=1 -D_REENTRANT $(DEBUG) -Werror-implicit-function-declaration -std=gnu89 -DORBIT2_EXTENSIONS
#INCLUDE := -I$(shell pwd) -I$(IDL_OUTPUT_DIR) -I/opt/gnome/include/glib-2.0 -I/opt/gnome/lib/glib-2.0/include -I/opt/gnome/include/orbit-2.0 
#INCLUDE := -I$(shell pwd) -I$(IDL_OUTPUT_DIR) -I/usr/include/orbit-2.0 -I/usr/include/glib-2.0 -I/usr/lib/glib-2.0/include
#INCLUDE := -I$(shell pwd) -I$(IDL_OUTPUT_DIR) -I$(HOME)/opt/include/orbit-2.0/ -I/usr/include/glib-2.0 -I/usr/lib/glib-2.0/include
#LDFLAGS= -Wl,--export-dynamic -L/opt/gnome/lib -L/opt/gnome/lib/glib-2.0 -lORBit-2 -lgthread-2.0 -lglib-2.0 -lgmodule-2.0 -ldl -lpopt -lgobject-2.0 -lpthread -Wl,--rpath -Wl,$(LIBDIR)
#LDFLAGS= -Wl,--export-dynamic -lORBit-2 -lgthread-2.0 -lglib-2.0 -lgmodule-2.0 -ldl -lgobject-2.0 -lpthread -Wl,--rpath -Wl,$(LIBDIR)

# Mac OS X: DarwinPorts
#INCLUDE := -I$(shell pwd) -I$(IDL_OUTPUT_DIR) -I/opt/local/include/glib-2.0 -I/opt/local/lib/glib-2.0/include -I/opt/local/include/orbit-2.0 
#LDFLAGS= -L/opt/local/lib -lORBit-2 -lgthread-2.0 -lglib-2.0 -lgmodule-2.0 -ldl -lgobject-2.0 -lpthread

# Mac OS X: DarwinPorts + local ORBit2 install
INCLUDE := -I$(shell pwd) -I$(IDL_OUTPUT_DIR) -I/opt/local/include/glib-2.0 -I/opt/local/lib/glib-2.0/include -I$(HOME)/opt/include -I$(HOME)/opt/include/orbit-2.0 
LDFLAGS=  -lpthread -L/opt/local/lib -L$(HOME)/opt/lib -lORBit-2 -lglib-2.0 -lgthread-2.0
# -lgmodule-2.0 -ldl -lgobject-2.0

# ORBit2
ORBIT_IDL := orbit-idl-2
ORBIT_IDLFLAGS := --showcpperrors --onlytop --output-dir=$(IDL_OUTPUT_DIR) --headerguardprefix=_BRUTUS_

LORICA_IDL_FILES := \
	$(IDL_OUTPUT_DIR)/ReferenceMapper.idl \
	$(IDL_OUTPUT_DIR)/ServerAgent.idl

IDL_FILES := \
	$(IDL_ROOT)/BrutusCheck.idl \
	$(IDL_ROOT)/BrutusLogOn.idl \
	$(IDL_ROOT)/IABContainer.idl \
	$(IDL_ROOT)/IABLogon.idl \
	$(IDL_ROOT)/IABProvider.idl \
	$(IDL_ROOT)/IAddrBook.idl \
	$(IDL_ROOT)/IAttach.idl \
	$(IDL_ROOT)/IDistList.idl \
	$(IDL_ROOT)/IExchangeExportChanges.idl \
	$(IDL_ROOT)/IExchangeFavorites.idl \
	$(IDL_ROOT)/IExchangeImportContentsChanges.idl \
	$(IDL_ROOT)/IExchangeImportHierarchyChanges.idl \
	$(IDL_ROOT)/IExchangeManageStore.idl \
	$(IDL_ROOT)/IExchangeModifyTable.idl \
	$(IDL_ROOT)/IExchangeRuleAction.idl \
	$(IDL_ROOT)/IMAPIAdviseSink.idl \
	$(IDL_ROOT)/IMAPIContainer.idl \
	$(IDL_ROOT)/IMAPIControl.idl \
	$(IDL_ROOT)/IMAPIFolder.idl \
	$(IDL_ROOT)/IMAPIForm.idl \
	$(IDL_ROOT)/IMAPIFormAdviseSink.idl \
	$(IDL_ROOT)/IMAPIFormContainer.idl \
	$(IDL_ROOT)/IMAPIFormFactory.idl \
	$(IDL_ROOT)/IMAPIFormInfo.idl \
	$(IDL_ROOT)/IMAPIFormMgr.idl \
	$(IDL_ROOT)/IMAPIMessageSite.idl \
	$(IDL_ROOT)/IMAPIProgress.idl \
	$(IDL_ROOT)/IMAPIProp.idl \
	$(IDL_ROOT)/IMAPISession.idl \
	$(IDL_ROOT)/IMAPIStatus.idl \
	$(IDL_ROOT)/IMAPISupport.idl \
	$(IDL_ROOT)/IMAPITable.idl \
	$(IDL_ROOT)/IMAPIViewAdviseSink.idl \
	$(IDL_ROOT)/IMAPIViewContext.idl \
	$(IDL_ROOT)/IMSLogon.idl \
	$(IDL_ROOT)/IMSProvider.idl \
	$(IDL_ROOT)/IMailUser.idl \
	$(IDL_ROOT)/IMessage.idl \
	$(IDL_ROOT)/IMsgServiceAdmin.idl \
	$(IDL_ROOT)/IMsgStore.idl \
	$(IDL_ROOT)/IPersistMessage.idl \
	$(IDL_ROOT)/IProfAdmin.idl \
	$(IDL_ROOT)/IProfSect.idl \
	$(IDL_ROOT)/IPropData.idl \
	$(IDL_ROOT)/IProviderAdmin.idl \
	$(IDL_ROOT)/ISequentialStream.idl \
	$(IDL_ROOT)/ISpoolerHook.idl \
	$(IDL_ROOT)/IStream.idl \
	$(IDL_ROOT)/ITableData.idl \
	$(IDL_ROOT)/ITnef.idl \
	$(IDL_ROOT)/IXPLogon.idl \
	$(IDL_ROOT)/IXPProvider.idl \
	$(IDL_ROOT)/ReferenceMapper.idl \
	$(IDL_ROOT)/ServerAgent.idl \
	$(IDL_ROOT)/defines.idl \
	$(IDL_ROOT)/return_codes.idl \
	$(IDL_ROOT)/types.idl \
	$(IDL_ROOT)/uid.idl 

# Construct the stub targets
BARE_NAMES := $(notdir $(IDL_FILES))
BARE_CLIENT_STUB_SOURCES := $(BARE_NAMES:.idl=-stubs.c)
CLIENT_STUB_SOURCES := $(addprefix $(IDL_OUTPUT_DIR)/,$(BARE_CLIENT_STUB_SOURCES))
BARE_COMMON_SOURCES := $(BARE_NAMES:.idl=-common.c)
COMMON_SOURCES := $(addprefix $(IDL_OUTPUT_DIR)/,$(BARE_COMMON_SOURCES))
STUB_SOURCES := $(CLIENT_STUB_SOURCES) $(COMMON_SOURCES)

# Sub projects
DIRS := servant_impl \
        bresult \
	proptags \
	brutus_utils \
        config \
        init_orb \
	client


# Don't export these..
unexport LORICA_IDL_FILES
unexport IDL_FILES
unexport IDL_OUTPUT_DIR
unexport ORBIT_IDLFLAGS
unexport BARE_NAMES
unexport BARE_CLIENT_STUB_SOURCES
unexport CLIENT_STUB_SOURCES
unexport BARE_COMMON_SOURCES
unexport COMMON_SOURCES
unexport STUB_SOURCES
unexport DIRS

# Export all other variables to sub-makes
export


#
# I do not include stubs as a dependancy as it 
# is comparatively rare that the idl files change.
# To rebuild all of these each time you issued a 
# "make" or "make all" would be unbearably 
# burdensome and time consuming.
#
# Only the test/sample code is therefore build.
#
all: 
	@for dir in $(DIRS); do \
		(cd $$dir && $(MAKE) all); \
	done


stubs: idl
	(cd $(IDL_OUTPUT_DIR) && $(MAKE))
	(cd servant_impl && $(MAKE) stubs)


# Must clean everything out if the idl files are being rebuild
# to be sure of having updated client stub stubs
idl: very_clean $(STUB_SOURCES)


# Compile the IDL files to get the client stubs
$(STUB_SOURCES): $(IDL_FILES) $(LORICA_IDL_FILES)
	@echo
	@for file in $(IDL_FILES); do \
		(echo Compiling $$file ; cd $(IDL_ROOT) && $(ORBIT_IDL) $(ORBIT_IDLFLAGS) $$file); \
	done
	@for file in $(LORICA_IDL_FILES); do \
		(echo Compiling $$file ; cd $(IDL_OUTPUT_DIR) && $(ORBIT_IDL) $(ORBIT_IDLFLAGS) $$file); \
	done

$(LORICA_IDL_FILES):
	@echo
	@for file in $(notdir $(LORICA_IDL_FILES)); do \
		(cd $(IDL_OUTPUT_DIR) ; wget http://svn.42tools.net/repos/lorica/trunk/src/lorica/$$file); \
	done

very_clean: clean clean_idl


clean: 
	@for dir in $(DIRS); do \
		(cd $$dir && $(MAKE) clean); \
	done


clean_idl:
	-rm -f $(IDL_OUTPUT_DIR)/*.c $(IDL_OUTPUT_DIR)/*.[iho] $(IDL_OUTPUT_DIR)/*.idl
	-mkdir -p $(IDL_OUTPUT_DIR)
	-cd servant_impl/ && $(MAKE) very_clean


.PHONY: all stubs idl very_clean clean clean_idl



