# RULES.Db
# Usage: 
# Place this file in your $TOP/configure directory.
# Include this file after $(TOP)/configure/RULES in your *App/Db Makefile.
#
# This file does two things:
# Provides a rule for translating what the msi document calls dbTemplate
# format substitution files into db files.
#
# Provides for the extraction of request files and archive request files
# from templates.
#

# I am going to STEAL RELEASE_DBDFLAGS because a dbd dir will be found
# everywhere we need files from a db dir anyway. (99% of the time)
vpath %.template $(patsubst %/dbd,%/db, $(filter %/dbd, $(RELEASE_DBDFLAGS)))
vpath %.db $(patsubst %/dbd,%/db, $(filter %/dbd, $(RELEASE_DBDFLAGS)))
RELEASE_DBFLAGS+=$(patsubst %/dbd,%/db, $(RELEASE_DBDFLAGS))
RELEASE_DBFLAGS+=-I $(TOP)/db


# translate a a dbTemplate style substitution file.
$(COMMON_DIR)/%.db$(RAW): %.substitutions $(USES_TEMPLATE)
	@echo "Inflating database from $< using local rule"
	@$(RM) $@
	@echo "$(MSI) -S$< > $(@F) </dev/null"
	@$(MSI) -I.. $(RELEASE_DBFLAGS) -S$< </dev/null > $@

$(COMMON_DIR)/%.template$(RAW): %.substitutions $(USES_TEMPLATE)
	@echo "Inflating database from $< using local rule"
	@$(RM) $@
	@echo "$(MSI) -S$< > $(@F) </dev/null"
	@$(MSI) -I.. $(RELEASE_DBFLAGS) -S$< </dev/null > $@
	
# If you want a specific dependency rule for your db the target must fit
# the above pattern:
# $(COMMON_DIR)/my.db: myFirst.template myNext.template ..
# The vpath and $(RELEASE_DBFLAGS) Will locate them for you so that you don't
# need full paths anywhere.


# extract a auto save restore file from a completed db file
$(INSTALL_DB)/%.req: $(COMMON_DIR)/%.req
	cp $< $@
$(COMMON_DIR)/%.req: $(COMMON_DIR)/%.db$(RAW)
	@ echo "Extracting $@ from $<"
	@ grep "@req" $< | sed ' s/# *@req *// ' > $@

$(COMMON_DIR)/%.req: %.db
	@ echo "Extracting $@ from $<"
	@ grep "@req" $< | sed ' s/# *@req *// ' > $@

$(COMMON_DIR)/%.req: %.template
	@ echo "Extracting $@ from $<"
	@ grep "@req" $< | sed ' s/# *@req *// ' > $@

$(INSTALL_DB)/%.arReq: $(COMMON_DIR)/%.arReq
	cp $< $@
$(COMMON_DIR)/%.arReq: $(COMMON_DIR)/%.db$(RAW)
	@ echo "Extracting $@ from $<"
	@ grep "@arReq" $< | sed ' s/# *@arReq *// ' > $@
$(COMMON_DIR)/%.arReq: %.db
	@ echo "Extracting $@ from $<"
	@ grep "@arReq" $< | sed ' s/# *@arReq *// ' > $@
$(COMMON_DIR)/%.arReq: %.template
	@ echo "Extracting $@ from $<"
	@ grep "@arReq" $< | sed ' s/# *@arReq *// ' > $@

#####================ CA security stuff=======================
$(INSTALL_DB)/%.acf: %.acf
	@echo "Installing $@"
	@$(INSTALL) -d -m 644 $(@F) $(@D)

%.acf: ../%.acf
	@$(CP) $< $@

%.acf: %.acs
	gcc -E -x c -undef -nostdinc $(ACFLAGS) $(EPICS_DBFLAGS) -o$@ -c $<
	ascheck -S"P=$(IOCNAME)" <$@

$(INSTALL_DB)/%.acs: %.acs
	@echo "Installing $@"
	@$(INSTALL) -d -m 644 $(@F) $(@D)

%.acs: ../%.acs
	@$(CP) $< $@
#=============================================================
