#*************************************************************************
# Copyright (c) 2013 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: anj@aps.anl.gov-20131213220401-phr67azt5xh4qgjv
#

#---------------------------------------------------------------
# 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 our own $(INSTALL_CFG)/RULES* definitions
#
TOP_CFG_RULES = $(wildcard $(INSTALL_CFG)/RULES*)
ifneq ($(TOP_CFG_RULES),)
  include $(TOP_CFG_RULES)
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)))

