# RULES.Db,v 1.9.6.4 2001/08/20 16:36:50 jba Exp
#
#	Rules for making things related to databases
#
MAKEBPT		= $(EPICS_BASE_HOST_BIN)/makeBpt$(EXE)
DBLOADTEMPLATE	= $(EPICS_BASE_HOST_BIN)/dbLoadTemplate$(EXE)
DBEXPAND	= $(EPICS_BASE_HOST_BIN)/dbExpand$(EXE)
DBST		= dbst
MAKEDBDEPENDS	= $(PERL) $(TOP)/config/makeDbDepends.pl
REPLACEVAR	= $(PERL) $(TOP)/config/replaceVAR.pl
ifndef WIN32
TOUCH		= touch
else
TOUCH		= type NUL >>
endif

#-----------------------------------------------------------------
#	if we are not building base add base dbd dirs

ifneq ($(EPICS_BASE),$(TOP))
ifneq ($(EPICS_BASE),$(INSTALL_LOCATION))
EPICS_DBDFLAGS += -I $(EPICS_BASE)/dbd
endif
endif

#---------------------------------------------------------------
#	----------------------------------------------------
#	create names (lists) for installed things
#	----------------------------------------------------

INSTALL_BPTS = $(BPTS:%= $(INSTALL_DBD)/%)
INSTALL_DBDS = $(DBDINSTALL:%= $(INSTALL_DBD)/%)
INSTALL_DBDNAME = $(DBDNAME:%= $(INSTALL_DBD)/%)
INSTALL_DATA = $(INSTALLDB:%=$(INSTALL_DB)/%)
INSTALL_TEMPLATES = $(filter %.template,$(INSTALL_DATA))

#---------------------------------------------------------------
#	Main targets

all::	install

inc::	$(INSTALL_DBDS) $(INSTALL_BPTS) $(INSTALL_TEMPLATES)

rebuild:: clean install

install:: inc buildInstall 

buildInstall:: build $(INSTALL_DATA)

depends::

clean::
	@echo "Cleaning"
	@$(RM) $(DB) $(DBDNAME) *.template *.substitutions *.db.raw \
	*.db-stamp *.edf esiread.cnf

##################################################### "Foreign" templates

TEMPLATE_LINKS	= $(filter-out $(notdir $(USES_TEMPLATE)), $(USES_TEMPLATE))
TEMPLATE_FILES	= $(filter     $(notdir $(USES_TEMPLATE)), $(USES_TEMPLATE))
DB_STAMP	= $(patsubst %.db, %.db-stamp, $(DB))
DB_REALTARGET	= $(patsubst %.db-stamp, %.db, $@)

ifneq '$(TEMPLATE_LINKS)' ''
build:: $(notdir $(TEMPLATE_LINKS))
endif
build:: $(INSTALL_DBDNAME) $(TEMPLATE_FILES) $(DB_STAMP)

$(notdir $(TEMPLATE_LINKS)): %.template:
ifndef WIN32
	@$(RM) $(notdir $(TEMPLATE_LINKS))
	ln -s $(TEMPLATE_LINKS) .
# Workaround for dbLoadTemplate bug: terminate here if link target doesn't exist
	@cat $(TEMPLATE_LINKS) > /dev/null
else
	@$(RM) $(notdir $(TEMPLATE_LINKS))
	$(CP) $(TEMPLATE_LINKS) .
endif

##################################################### Inflated or plain databases

$(INSTALL_DB)/%.db: %.db-stamp
	@echo "Installing database $@"
	@$(INSTALL) -d -m 644 $(patsubst %.db-stamp, %.db, $<) $(@D)

# Must have DBDNAME defined to use dbst optimization
ifndef DBDNAME
DB_OPT = NO
endif

# dbst based database optimization
ifeq '$(DB_OPT)' 'YES'
.PRECIOUS: %.db.raw
%.db-stamp: %.db.raw $(INSTALL_DBD)/$(DBDNAME)
	@echo "Optimizing database $@"
	$(DBST) $(INSTALL_DBD)/$(DBDNAME) $< -d > $(DB_REALTARGET)
	@$(TOUCH) $@
%.db-stamp: %.t.db.raw $(INSTALL_DBD)/$(DBDNAME)
	@echo "Optimizing database $@"
	$(DBST) $(INSTALL_DBD)/$(DBDNAME) $< -d > $(DB_REALTARGET)
	@$(TOUCH) $@
else
# NO optimization => move it and keep a stamp
%.db-stamp: %.db.raw
	@$(MV) $< $(DB_REALTARGET)
	@$(TOUCH) $@
	@$(TOUCH) $<
%.db-stamp: %.t.db.raw
	@$(MV) $< $(DB_REALTARGET)
	@$(TOUCH) $@
	@$(TOUCH) $<
endif

%.t.db.raw: %.substitutions
	@echo "Inflating database from $<"
	@$(RM) $@
	@$(DBLOADTEMPLATE) $< > $@

##################################################### CapFast filter

%.edf:: ../%.sch $(DEPSCHS)
	@if [ ! -f cad.rc -a -r ../cad.rc ] ; then ln -s ../cad.rc ; fi
	$(SCH2EDIF) $(SCH2EDIF_SYSFLAGS) $(SCH2EDIF_FLAGS) $<

##################################################### Substitution files

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

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

ifdef CREATESUBSTITUTIONS
%.substitutions:: $(word $(words $(CREATESUBSTITUTIONS)),$(CREATESUBSTITUTIONS))
	@$(CREATESUBSTITUTIONS) $*
endif

# Better make it PRECIOUS (to get around make bug)
.PRECIOUS: %.substitutions

##################################################### Template databases

# Installed template files (dbLoadTemplate() on IOC side)
$(INSTALL_DB)/%.template: %.template
	@echo "Installing $@"
	@$(INSTALL) -d -m 644 $(@F) $(@D)

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

%.template: %.edf
	$(E2DB) $(E2DB_SYSFLAGS) $(E2DB_FLAGS) -n $@.VAR $<
	@$(REPLACEVAR) < $@.VAR > $@
	@$(RM) $@.VAR

##################################################### Flat databases

%.db.raw:: ../%.db
	$(CP) $< $@

%.db.raw: %.edf
	$(E2DB) $(E2DB_SYSFLAGS) $(E2DB_FLAGS) -n $@.VAR $<
	@$(REPLACEVAR) < $@.VAR > $@
	@$(RM) $@.VAR

##################################################### DBD stuff

$(INSTALL_DBD)/%: %
	@echo "Installing $@"
	@$(INSTALL) -d -m 644 $< $(@D)

$(INSTALL_DBD)/%:: ../%
	@echo "Installing $@"
	@$(INSTALL) -d -m 644 $< $(@D)

bpt%.dbd: bpt%.data
	$(RM) $@
	$(MAKEBPT) $<

bpt%.dbd:: ../bpt%.data
	$(RM) $@
	$(MAKEBPT) $<

# Patch for old applications
ifdef USER_DBDFLAGS
DBDFLAGS = $(USER_DBDFLAGS)
endif

ifdef DBDEXPAND
$(DBDNAME): ../$(DBDEXPAND)
	@echo "Expanding dbd"
	@$(RM) $@
	$(DBEXPAND) $(DBDFLAGS) $< > $@
endif

##################################################### Dependencies

DEPENDS: $(filter $(patsubst %.db, %.substitutions, $(DB)), $(wildcard *.substitutions))
	@$(MAKEDBDEPENDS) $^

-include DEPENDS