#*************************************************************************
# Copyright (c) 2006 UChicago Argonne LLC, as Operator of Argonne
#     National Laboratory.
# Copyright (c) 2002 The Regents of the University of California, as
#     Operator of Los Alamos National Laboratory.
# EPICS BASE is distributed subject to a Software License Agreement found
# in the file LICENSE that is included with this distribution. 
#*************************************************************************
# $Revision-Id$
#

#---------------------------------------------------------------
# Include <top>/configure/RULES_BUILD definitions from tops defined in RELEASE* files
#
RELEASE_RULES_BUILDS = $(foreach top, $(RELEASE_TOPS), $(wildcard $($(top))/configure/RULES_BUILD))
ifneq ($(RELEASE_RULES_BUILDS),)
  include $(RELEASE_RULES_BUILDS)
endif

#---------------------------------------------------------------
# Include <top>/cfg/RULES* definitions from tops defined in RELEASE* files
#
RELEASE_CFG_RULES = $(foreach top, $(RELEASE_TOPS), $(wildcard $($(top))/cfg/RULES*))
ifneq ($(RELEASE_CFG_RULES),)
  include $(RELEASE_CFG_RULES)
endif

#---------------------------------------------------------------
# If this is not BASE then include TOP/configure/RULES_BUILD definitions
#
ifeq ($(wildcard $(TOP)/configure/CONFIG_BASE_VERSION),)
TOP_RULES_BUILDS = $(wildcard $(TOP)/configure/RULES_BUILD)
ifneq ($(TOP_RULES_BUILDS),)
  include $(TOP_RULES_BUILDS)
endif
endif

#---------------------------------------------
# Include existing and new $(INSTALL_CFG)/* definitions
#
TOP_CFG_FILES = $(sort $(wildcard $(INSTALL_CFG)/RULES*) \
                       $(wildcard $(INSTALL_CFG)/CONFIG*) \
                       $(addprefix $(INSTALL_CFG)/,$(CFG)))
ifneq ($(TOP_CFG_FILES),)
  include $(TOP_CFG_FILES)
endif

#---------------------------------------------------------------

define FILE_TYPE_template

$(1) += $$(if $$(strip $$($(1)_$$(OS_CLASS))),$$(subst -nil-,,$$($(1)_$$(OS_CLASS))), $$($(1)_DEFAULT))

INSTALLS_$(1) = $$($(1):%=$$(INSTALL_$(1))/%)

$$(INSTALL_$(1))/%: ../%
	$(ECHO) "Installing $(1) file $$@"
	@$$(INSTALL) -d -m  $$(INSTALL_PERMISSIONS) $$< $$(dir $$@)

$$(INSTALL_$(1))/%: %
	$(ECHO) "Installing $(1) file $$@"
	@$$(INSTALL) -d -m  $$(INSTALL_PERMISSIONS) $$< $$(dir $$@)

buildInstall : $$(INSTALLS_$(1))

endef

$(foreach type, $(FILE_TYPE),$(eval $(call FILE_TYPE_template,$(strip $(type)))))
#---------------------------------------------

clean::
	@$(RM) $(foreach type, $(FILE_TYPE), $($(type)))

