#  
#  Makefile for Brutus test/sample code.
#  Copyright (C) 2004-2008 OMC Denmark ApS.
#
#  This program is free software: you can redistribute it and/or modify
#  it under the terms of the GNU Affero 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 Affero General Public License for more details.
#
#  You should have received a copy of the GNU Affero General Public License
#  along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

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

# ACE/TAO settings
ifndef ACE_ROOT
    $(message ACE_ROOT not defined, assuming system wide installation)
    ORB_INCLUDE := -I/usr/include/
    ORB_LDFLAGS := -L/usr/lib
    TAO_IDL := /usr/bin/tao_idl
else
    $(message ACE_ROOT defined, assuming local ACE/TAO source installation)
    ifndef TAO_ROOT
        TAO_ROOT := $(ACE_ROOT)/TAO
    endif
    ORB_INCLUDE := -I$(ACE_ROOT) -I$(TAO_ROOT) 
    ORB_LDFLAGS := -L$(ACE_ROOT)/lib
    TAO_IDL := $(ACE_ROOT)/bin/tao_idl
endif

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

# TAO
TAO_IDLFLAGS := -o $(IDL_OUTPUT_DIR) -in -Ce  -ci "C.i" -si "S.i"

# Compilation directives
CC := gcc
OPT = -Os
DEBUG := -g -O0 -fno-inline
CFLAGS := -Wall $(DEBUG) 
INCLUDE := $(ORB_INCLUDE) -I$(shell pwd) -I$(IDL_OUTPUT_DIR)
LDFLAGS := $(ORB_LDFLAGS) 
LIBS := -lTAO_AnyTypeCode -lTAO_PortableServer -lTAO -lACE

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_STUB_SOURCES := $(BARE_NAMES:.idl=C.cpp)
STUB_SOURCES := $(addprefix $(IDL_OUTPUT_DIR)/,$(BARE_STUB_SOURCES))


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


# Don't export these..
unexport LORICA_IDL_FILES
unexport IDL_FILES
unexport BARE_NAMES
unexport BARE_STUB_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) && $(TAO_IDL) $(TAO_IDLFLAGS) $$file); \
	done
	@for file in $(LORICA_IDL_FILES); do \
		(echo Compiling $$file ; cd $(IDL_OUTPUT_DIR) && $(TAO_IDL) $(TAO_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)/*.cpp $(IDL_OUTPUT_DIR)/*.[iho]
	-mkdir -p $(IDL_OUTPUT_DIR)
	-cd servant_impl/ && $(MAKE) very_clean


.PHONY: all stubs idl very_clean clean clean_idl



