####### Main Makefile for building the various biosig tools ####################
###
###  Copyright (C) 2010-2020 Alois Schloegl <alois.schloegl@gmail.com>
###  This file is part of the "BioSig for C/C++" repository
###  (biosig4c++) at http://biosig.sf.net/
###
################################################################################

exec_prefix ?= $(prefix)

first :: lib tools

java: lib
	make -C biosig4c++/java

python: lib
	make -C biosig4c++/python

lib:
	make -C biosig4c++ lib

tools: lib
	make -C biosig4c++ tools

mma mathematica: lib
	make -C biosig4c++/mma

mex4m matlab: lib
	make -C biosig4c++/mex mex4m

mex4o octave: lib
	make -C biosig4c++/mex mex4o

R: lib
	make -C biosig4c++/R

install ::
	make -C biosig4c++ install
	install -d $(DESTDIR)@prefix@/share/biosig/matlab
	cp -r biosig4matlab/* $(DESTDIR)@prefix@/share/biosig/matlab/
	-rm -rf $(DESTDIR)@prefix@/share/biosig/matlab/maybe-missing

uninstall ::
	make -C biosig4c++ uninstall
	-rm -rf $(DESTDIR)@prefix@/share/biosig

clean ::
	make -C biosig4c++/mma clean
	make -C biosig4c++ clean

distclean : clean
        # also configure.ac for list of files
	rm Makefile biosig4c++/Makefile biosig4c++/*/Makefile \
		biosig4c++/python/setup.py \
		biosig4c++/R/DESCRIPTION

ifneq (:,@JAVA@)
ifneq (:,@JAVAC@)
first :: lib
	-make -C biosig4c++/java
endif
clean ::
	-make -C biosig4c++/java clean
test ::
	-make -C biosig4c++/java test
endif

ifneq (:,@MATHEMATICA@)
first :: lib
	make -C biosig4c++/mma
install ::
	-make -C biosig4c++/mma install
uninstall ::
	-make -C biosig4c++/mma uninstall
endif

ifneq (:,@MATLAB@)
MATLAB_MEX := $(shell dirname @MATLAB@)/mex
first ::
	make -C biosig4c++/mex mex4m
install ::
	make -C biosig4c++/mex install
uninstall ::
	make -C biosig4c++/mex uninstall
endif

ifneq (:,@OCTAVE@)
ifneq (:,@MKOCTFILE@)
BIOSIG_MEX_DIR = $(DESTDIR)$(shell octave-config -p LOCALOCTFILEDIR)/biosig
BIOSIG_DIR     = $(DESTDIR)$(shell octave-config -p LOCALFCNFILEDIR)/biosig
first ::
	make octave
	make -C biosig4c++ mexbiosig
install ::
	# mexbiosig
	#-@OCTAVE@ --no-gui --eval "pkg install -global biosig4c++/mex/mexbiosig-@PACKAGE_VERSION@.src.tar.gz"
	# *.mex
	install -d $(BIOSIG_MEX_DIR)
	install biosig4c++/mex/*.mex $(BIOSIG_MEX_DIR)
	# biosig for octave and matlab
	install -d $(BIOSIG_DIR)
	cp -r biosig4matlab/*  $(BIOSIG_DIR)
	-rm -rf $(BIOSIG_DIR)/maybe-missing

uninstall ::
	# mexbiosig
	#-@OCTAVE@ --no-gui --eval "pkg uninstall -global mexbiosig"
	# *.mex
	-rm -rf $(BIOSIG_MEX_DIR)
	# biosig for octave and matlab
	-rm -rf $(BIOSIG_DIR)
endif
endif

ifneq (:,@PYTHON3@)
first biosig4c++/python/dist/Biosig-@PACKAGE_VERSION@.tar.gz ::
	-PYTHON=@PYTHON3@ make -C biosig4c++/python dist/Biosig-@PACKAGE_VERSION@.tar.gz
install :: biosig4c++/python/dist/Biosig-@PACKAGE_VERSION@.tar.gz
	-@PYTHON3@ -m pip install $<
	-$(foreach py, $(shell pyversions -i),  $(py) -m pip install $<;)
	-$(foreach py, $(shell py3versions -i), $(py) -m pip install $<;)
uninstall ::
	-@PYTHON3@ -m pip uninstall -y $<
	-$(foreach py, $(shell pyversions -i),  $(py) -m pip uninstall -y Biosig;)
	-$(foreach py, $(shell py3versions -i), $(py) -m pip uninstall -y Biosig;)
clean ::
	make -C biosig4c++/python clean
endif

ifneq (:,@R@)
first ::
	-make -C biosig4c++/R build
install ::
	-make -C biosig4c++/R install
clean ::
	-make -C biosig4c++/R clean
endif

all: first #win32 win64 #sigviewer #win32/sigviewer.exe win64/sigviewer.exe #biosig_client biosig_server mma java tcl perl php ruby #sigviewer

#---- automatic remaking ---------------------------------------------------#
#   https://www.gnu.org/software/autoconf/manual/autoconf-2.69/html_node/Automatic-Remaking.html#Automatic-Remaking
#---------------------------------------------------------------------------#
$(srcdir)/configure: configure.ac aclocal.m4
	autoconf

# autoheader might not change config.h.in, so touch a stamp file.
$(srcdir)/config.h.in: stamp-h.in
$(srcdir)/stamp-h.in: configure.ac aclocal.m4
	autoheader
	echo timestamp > '$(srcdir)/stamp-h.in'

config.h: stamp-h
stamp-h: config.h.in config.status
	./config.status

Makefile: Makefile.in config.status
	./config.status

config.status: configure
	./config.status --recheck

