############################################################################
# Makefile to install the system libraries, i.e., to copy the sources
# from `lib-trunk` to `lib`.
#
# This Makefile will be executed when PAKCS is built in the directory
# `lib-trunk`.
############################################################################

# directory containing the repository library files:
ifndef CURRYLIBSDIR
CURRYLIBSDIR=$(ROOT)/lib-trunk
endif

CURRYLIBSSRCDIR=$(CURRYLIBSDIR)/src

MODULE_FOLDERS    :=$(shell cd $(CURRYLIBSSRCDIR) && find * -type d)
CURRY_FILES       :=$(shell cd $(CURRYLIBSSRCDIR) && find * -name "*.curry")
PAKCS_FILES       :=$(shell cd $(CURRYLIBSSRCDIR) && find * -name "*.pakcs")
PAKCS_CURRY_FILES :=$(addsuffix .curry, $(basename $(PAKCS_FILES)))
PAKCS_PL_FILES    :=$(shell cd $(CURRYLIBSSRCDIR) && find * -name "*.pakcs.pl")
NON_PAKCS_BASENAMES=$(basename $(filter-out $(CURRY_PAKCS_FILES), $(CURRY_FILES)))
CURRYONLY_FILES    =$(addsuffix .curry, $(filter-out $(basename $(PAKCS_FILES)), $(NON_PAKCS_BASENAMES)))

# name of this makefile:
CURRENT_MAKEFILE = $(ROOT)/Makefile_install_lib

##########################################################################
# Install the library sources into the Curry system library directory:
.PHONY: install
install:
	mkdir -p $(LIBDIR)
	cd $(LIBDIR) && $(MAKE) -f $(CURRENT_MAKEFILE) $(MODULE_FOLDERS) $(CURRYONLY_FILES) $(PAKCS_CURRY_FILES) $(PAKCS_PL_FILES) $(LIBDIR)/VERSION

$(MODULE_FOLDERS): %: $(CURRYLIBSSRCDIR)/%
	mkdir -p $@

$(CURRYONLY_FILES): %.curry: $(CURRYLIBSSRCDIR)/%.curry
	cp $< $@

$(PAKCS_FILES): %.pakcs: $(CURRYLIBSSRCDIR)/%.pakcs
	cp $< $@

$(PAKCS_CURRY_FILES): %.curry: $(CURRYLIBSSRCDIR)/%.curry %.pakcs
	cp $< $@

$(PAKCS_PL_FILES): %.pakcs.pl: $(CURRYLIBSSRCDIR)/%.pakcs.pl
	cp $< $@

$(LIBDIR)/VERSION: $(CURRYLIBSDIR)/VERSION
	cp $< $@
