COREPATH ?=../..
DEFSPATH ?=$(COREPATH)/..
.DEFAULT_GOAL := all

include $(DEFSPATH)/Makefile.defs

LIB_LDFLAGS  += $(module_ldflags)
CPPFLAGS += -I $(COREPATH)
CFLAGS   += -I $(COREPATH)
CFLAGS   += $(module_cflags)
CPPFLAGS += $(module_cflags)
CXXFLAGS += $(module_cflags)

app_module_dir ?= $(COREPATH)/lib

lib_name = $(plug_in_name).so
lib_full_name ?= $(app_module_dir)/$(lib_name)

module_conf_files =  $(notdir $(wildcard etc/*.conf))
AUDIO_FILES=$(notdir $(wildcard wav/*.wav))
EXTRA_LIB_DIR?=lib/
EXTRA_LIB_FILES=$(filter-out CMakeLists.txt, $(notdir $(wildcard $(EXTRA_LIB_DIR)*)))

srcs     = $(wildcard *.cpp)
hrds     = $(wildcard *.h)
objs     = $(srcs:.cpp=.o) 
depends  = $(srcs:.cpp=.d)

.PHONY: all
all: $(extra_target)
	-@$(MAKE) deps    && \
	  $(MAKE) $(lib_full_name)

.PHONY: module_package
module_package: $(extra_target)
	-@$(MAKE) deps    && \
	  $(MAKE) $(lib_name)


.PHONY: clean
clean: $(extra_clean)
	-@rm -f $(objs) $(depends) $(lib_name)

.PHONY: deps
deps: $(depends) $(extra_depends)

.PHONY: install
install: all $(extra_install) $(DESTDIR)$(modules-prefix)/$(modules-dir)
	if [ -f "$(lib_name)" ]; then \
	 $(INSTALL-TOUCH) $(DESTDIR)$(modules-prefix)/$(modules-dir)/$(lib_name) ; \
	 $(INSTALL-MODULES) $(lib_name) $(DESTDIR)$(modules-prefix)/$(modules-dir); \
	fi

.PHONY: install-cfg
install-cfg: $(DESTDIR)$(app-cfg-target)
	-@for r in $(module_conf_files); do \
			echo installing $$r ;  \
			$(INSTALL-TOUCH) $(DESTDIR)$(app-cfg-target)$$r ; \
			sed -e "s#/usr/.*lib/sems/audio/#$(audio-target)#g" \
			-e "s#/usr/.*lib/sems/plug-in/#$(modules-target)#g" \
			-e "s#/usr/.*lib/sems/ivr/#$(lib-target)ivr/#g" \
			-e "s#/usr/.*lib/sems/dsm/#$(lib-target)dsm/#g" \
			-e "s#/usr/.*lib/sems/py_sems/#$(lib-target)py_sems/#g" \
			-e "s#/usr/.*etc/sems#$(app-cfg-target)#g" \
			< etc/$$r > $(DESTDIR)$(app-cfg-target)$$r; \
	done

$(DESTDIR)(audio-prefix)/$(audio-dir):
	mkdir -p $(DESTDIR)$(audio-prefix)/$(audio-dir)

.PHONY: $(plug_in_name)_audio
$(plug_in_name)_audio: $(DESTDIR)$(audio-prefix)/$(audio-dir)
	mkdir -p $(DESTDIR)$(audio-prefix)/$(audio-dir)$(plug_in_name)
	-@for f in $(AUDIO_FILES) ; do \
		if [ -n "wav/$$f" ]; then \
			$(INSTALL-TOUCH) $(DESTDIR)$(audio-prefix)/$(audio-dir)$(plug_in_name)/$$f; \
			$(INSTALL-AUDIO) wav/$$f $(DESTDIR)$(audio-prefix)/$(audio-dir)$(plug_in_name)/$$f; \
		fi ; \
	done

.PHONY: $(plug_in_name)_lib
$(plug_in_name)_lib: 
	mkdir -p $(DESTDIR)$(lib-prefix)/$(lib-dir)/$(plug_in_name)
	-@for f in $(EXTRA_LIB_FILES) ; do \
		if [ -n "$(EXTRA_LIB_DIR)/$$f" ]; then \
			$(INSTALL-TOUCH) $(DESTDIR)$(lib-prefix)/$(lib-dir)/$(plug_in_name)/$$f; \
			$(INSTALL-AUDIO) $(EXTRA_LIB_DIR)$$f $(DESTDIR)$(lib-prefix)/$(lib-dir)/$(plug_in_name)/$$f; \
		fi ; \
	done

%.d: %.cpp Makefile $(COREPATH)/plug-in/Makefile.app_module $(DEFSPATH)/Makefile.defs
	$(CXX) -MM $< $(CXXFLAGS) $(CPPFLAGS) > $@

%.d: %.c Makefile $(COREPATH)/plug-in/Makefile.app_module $(DEFSPATH)/Makefile.defs
	$(CC) -MM $< $(CFLAGS) $(CPPFLAGS) > $@

%.o: %.cpp %.d $(DEFSPATH)/Makefile.defs
	$(CXX) $(CXXFLAGS) $(CPPFLAGS) -c $< -o $@

%.o: %.c %.d $(DEFSPATH)/Makefile.defs
	$(CC) $(CFLAGS) $(CPPFLAGS) -c $< -o $@

$(lib_full_name): $(lib_name)
ifneq ($(lib_full_name),$(lib_name))
ifneq ($(lib_full_name),./$(lib_name))
	cp $(lib_name) $(lib_full_name)
endif
endif

$(lib_name): $(objs) $(module_extra_objs) Makefile
	$(LD) -o $(lib_name) $(objs) $(module_extra_objs) $(LIB_LDFLAGS)

ifeq ($(lib_full_name),$(MAKECMDGOALS))
include $(depends)
endif

ifeq (,$(MAKECMDGOALS))
include $(depends)
endif