#*************************************************************************
# 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.
#*************************************************************************

# Include <top>/configure/RULES_BUILD 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* files 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
#
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* files
#
TOP_CFG_RULES = $(wildcard $(INSTALL_CFG)/RULES*)
ifneq ($(TOP_CFG_RULES),)
  include $(TOP_CFG_RULES)
endif

# Rules to install each FILE_TYPE
#
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)))))

# Cleaning FILE_TYPE files
#
clean: file_type_clean

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

.PHONY : file_type_clean

#  User specific rules
#
-include $(HOME)/configure/RULES_USER
