#===========================================================================
#
# Top SHARE Makefile
#
# author: Scott Binegar 
#
# Modification History:
#
#   Date       OPR         Who       Reason
# --------   -------   ------------  -----------------------------------------
# 09/20/01   n/a       S. Binegar    first version
# 02/20/02   44741     S. Binegar    add jar files to install
# 01/16/03   47179     Chris H.      blindly made changes for Jill
# 01/31/03   47450     Sontag        Added lines for AIX
# 03/31/05   53337     Sontag        Get ACE/TAO libs during make install
# 07/07/05   53718     MSwam         Let os defs set ACE/TAO version
# 11/17/05   53718     MSwam         New ACE/TAO not yet ready for Ops
# 12/07/05   54864     MSwam         Try again with ACE/TAO 541/141
# 04/24/06   55700     Sontag        Define TOPDIR; revamp TAO lib install
# 05/05/06   54226     Sontag        Add use of ACE_INST
# 09/28/06   52073     Sontag        Port GUI's to OSX
# 01/29/08   57952     Sontag        Handle x86_osx
# 11/12/09   63939     MSwam         Export some values to sub-makes
# 02/11/11   64549     RBelt         Added more autotools to install_auto_tools
#                      MSwam         new SHARE/OWL version
#===========================================================================
#
# Eliminate implicit rules
.SUFFIXES:

# determine the operating system type and related parameters
include platform.defs

ifndef TOPDIR
  export TOPDIR:=$(shell pwd)
endif

# Build source and sddl files
all: src 
	@echo "Finished build"

# Build the source
src : FORCE
	@echo "Building Source..."
	@cd src && $(MAKE)

# Make clean
clean:
	@cd src && $(MAKE) clean

FORCE:

#======================= Install definitions ======================
#
# Make sure the Installation directory is defined
#
ifneq ($(INSTALL_DIR),)

bin_files = $(filter-out %/CVS , $(wildcard bin/$(OS_SUB_DIR)/*))
dest_bin_files = $(addprefix $(INSTALL_DIR)/, $(bin_files))

com_files = $(filter-out %/CVS , $(wildcard com/*))
dest_com_files = $(addprefix $(INSTALL_DIR)/, $(com_files))

dat_files = $(filter-out %/CVS , $(wildcard dat/*))
dest_dat_files = $(addprefix $(INSTALL_DIR)/, $(dat_files))

definitions_files = $(filter-out %/CVS %/unix, $(wildcard definitions/*)) \
	            $(filter-out %/CVS , $(wildcard definitions/unix/*))
dest_definitions_files = $(addprefix $(INSTALL_DIR)/, $(definitions_files))

hlp_files = $(filter-out %/CVS %/gif, $(wildcard hlp/*)) \
            $(filter-out %/CVS, $(wildcard hlp/gif/*)) 
dest_hlp_files = $(addprefix $(INSTALL_DIR)/, $(hlp_files))

Install_files = $(filter-out %/CVS , $(wildcard install/*))
dest_Install_files = $(addprefix $(INSTALL_DIR)/, $(Install_files))

lib_files = $(filter-out %/CVS , $(wildcard lib/$(OS_SUB_DIR)/*))
dest_lib_files = $(addprefix $(INSTALL_DIR)/, $(lib_files))

java_files = $(wildcard bin/java/*.jar)
dest_java_files = $(addprefix $(INSTALL_DIR)/, $(java_files))

update_dirs =  bin com dat definitions hlp Install lib java 

#
include ./src/$(OS_SUB_DIR).defs
#
LBDR = $(INSTALL_DIR)/lib/$(OS_SUB_DIR)
.PHONY : install
install : $(update_dirs)
	@chmod -f ugo+rx $(INSTALL_DIR)/bin/$(OS_SUB_DIR)/*
	@chmod -f ugo+r $(INSTALL_DIR)/lib/$(OS_SUB_DIR)/*
	@chmod -f ugo+rx $(INSTALL_DIR)/bin/java/*
	@echo "Install/Update complete"

# Create all of the destination directories
#

.PHONY : dirs
dirs :
	@echo "INSTALL_DIR = $(INSTALL_DIR)"
	@sleep 5
	@echo "Building dirs..."
	[ -d  $(INSTALL_DIR)/bin/$(OS_SUB_DIR) ] || mkdir -p $(INSTALL_DIR)/bin/$(OS_SUB_DIR)
	[ -d  $(INSTALL_DIR)/bin/java ] || mkdir -p $(INSTALL_DIR)/bin/java
	[ -d  $(INSTALL_DIR)/com ] || mkdir -p $(INSTALL_DIR)/com
	[ -d  $(INSTALL_DIR)/dat ] || mkdir -p $(INSTALL_DIR)/dat
	[ -d  $(INSTALL_DIR)/definitions/unix ] || mkdir -p $(INSTALL_DIR)/definitions/unix
	[ -d  $(INSTALL_DIR)/hlp/gif ] || mkdir -p $(INSTALL_DIR)/hlp/gif
	[ -d  $(INSTALL_DIR)/install ] || mkdir -p $(INSTALL_DIR)/install
	[ -d  $(INSTALL_DIR)/lib/$(OS_SUB_DIR) ] || mkdir -p $(INSTALL_DIR)/lib/$(OS_SUB_DIR)

bin : $(dest_bin_files)
com : $(dest_com_files)
dat : $(dest_dat_files)
definitions : $(dest_definitions_files)
hlp : $(dest_hlp_files)
Install : $(dest_Install_files)
lib : $(dest_lib_files)
java : $(dest_java_files)

dest_files := $(foreach dir, $(update_dirs), $(dest_$(dir)_files))

$(dest_files) : $(INSTALL_DIR)/% : %
	@echo 'Installing: $(<)'
	@cp -f '$(<)' '$(@)'

print:
	@echo $(lib_files)
	@echo $(bin_script_files)
	@echo $(dest_files)
	@echo $(dest_dat_files)

else # INSTALL_DIR not defined

ERR = $(error ERROR)

.PHONY: install
install:
	@echo "INSTALL_DIR not defined"
	$(ERR)

endif 
