# RULES.launch
# RULES for building and installing launcher configuration files

# Display how this makefile was called
#$(info RULES.launch: '$(MAKECMDGOALS)')

####################################################
# Where to find launch files
vpath %.launch       $(USR_VPATH) $(GENERIC_SRC_DIRS) $(dir $(LAUNCH))
vpath %.tpl-launch      $(USR_VPATH) $(GENERIC_SRC_DIRS) $(dir $(LAUNCH)) $(COMMON_DIR)
vpath %.sub-launch	  $(USR_VPATH) $(GENERIC_SRC_DIRS) $(dir $(LAUNCH)) $(COMMON_DIR)

# Install launch files here
INSTALL_LAUNCH = $(INSTALL_LOCATION)/launch

####################################################
# launch include Flags (msi -I path)
define  LAUNCH_DIRS_template
  RELEASE_LAUNCH_DIRS += $$(wildcard $$(strip $$($(1)))/launch)
endef
$(foreach top, $(RELEASE_TOPS), \
	$(eval $(call LAUNCH_DIRS_template,$(top)) ))
LAUNCH_SEARCH_DIRS = . .. $(COMMON_DIR) $(SRC_DIRS) $(INSTALL_LAUNCH) $(TOP)/launch $(RELEASE_LAUNCH_DIRS)
LAUNCH_INCLUDES = $(addprefix -I, $(LAUNCH_SEARCH_DIRS))
LAUNCHFLAGS  = $($*_LAUNCHFLAGS) $(USR_LAUNCHFLAGS) $(LAUNCH_INCLUDES)

####################################################
# Generate COMMON_LAUNCHS list
COMMON_LAUNCHS  += $(filter $(COMMON_DIR)/%, \
	$(foreach file, $(LAUNCH), \
		$(firstword $(SOURCE_LAUNCH) $(COMMON_DIR)/$(file) ) ) )
SOURCE_LAUNCH = $(wildcard $(file) $(SOURCE_LAUNCH_bbb) )
SOURCE_LAUNCH_bbb = $(foreach dir, $(GENERIC_SRC_DIRS), $(SOURCE_LAUNCH_aaa)  )
SOURCE_LAUNCH_aaa = $(addsuffix /$(file), $(dir) )

# Generate INSTALL_LAUNCHS list
INSTALL_LAUNCHS += $(addprefix $(INSTALL_LAUNCH)/, $(notdir $(LAUNCH)))

####################################################
# Generate LAUNCH_DEPENDS_FILES list
LAUNCH_DEPENDS_FILES += $(addsuffix $(DEP), $(patsubst $(COMMON_DIR)/%,%,$(COMMON_LAUNCHS)))

####################################################
# Build targets

install:        $(INSTALL_LAUNCHS)

build:          $(COMMON_LAUNCHS)

####################################################
# Build and install Rules

$(INSTALL_LAUNCH)/%:	%
	@$(ECHO) "Installing launch file $@"
	@$(INSTALL) -d -m 644 $< $(@D)

$(COMMON_DIR)/%.launch %.launch: %.sub-launch
	@$(RM) $(notdir $@)$(DEP)
	$(MKMF) -m $(notdir $@)$(DEP) $(LAUNCH_INCLUDES) $@ $< 
	@$(ECHO) "Inflating launch file from $<"
	@$(RM) $@
	$(MSI3_15) $(LAUNCHFLAGS) -S$< > $@

$(INSTALL_LAUNCH)/%: $(COMMON_DIR)/%
	@$(ECHO) "Installing inflated launch file $@"
	@$(INSTALL) -d -m 644 $< $(@D)

#####################################################
# Include launch dependency files
ifneq (,$(strip $(LAUNCH_DEPENDS_FILES)))
-include $(LAUNCH_DEPENDS_FILES)
endif

