#
# This makefile makes the examples in this directory.  There's a good chance
# that you'll need to tweak this a bit to get it working on your system.
#
# To use:
#	Do "make matlab", "make octave", or "make tela" to make the
#	appropriate binary.
#	
PERL	  = perl
TOP	  = ../../../..
MV	  = mv
MATWRAP = $(PERL) -w $(TOP)/bin/matwrap


#
# The wrapper generator looks at these three .h files to get all the
# prototypes:
#
HFILES	  = misc.h hodgkin_huxley.h single_axon.h
OFILES	  = hodgkin_huxley.o single_axon.o
#
# Additional libraries needed:
#
LIBS = -lm


all::
	@echo 'Type "make matlab" to make the matlab example, "make octave"'
	@echo 'to make the octave example, and "make tela" to make the'
	@echo 'tela example.'
	@false

########################################################################
#
# Instructions for making the matlab modules:
# Note that all of the matlab stub functions go into the directory 'wrap_m'.
#
matlab::
	test -d $(MATLAB_STUBDIR) || mkdir $(MATLAB_STUBDIR)
	$(MAKE) single_axon_matlab.mexaxp

#
# Force mex to use the gnu C++ compiler.
#
MEXVARS = ../mex_cpp.sh

#
# Command to run MEX:
#
MEX = mex -f $(MEXVARS)

#
# Where to put the matlab stub functions.
# This directory must be in your matlab path.
#
MATLAB_STUBDIR = wrap_m

single_axon_matlab.mexaxp: $(HFILES) $(OFILES)
	$(MATWRAP) -language matlab $(HFILES) \
		-o single_axon_matlab.c -outdir $(MATLAB_STUBDIR)
	$(MEX) single_axon_matlab.c $(OFILES)

########################################################################
#
# Instructions for making the octave modules:
#
MKOCTFILE = mkoctfile

octave:: single_axon_octave.oct

single_axon_octave.oct: $(HFILES) $(OFILES)
	$(MATWRAP) -language octave $(HFILES) \
		-o single_axon_octave.cc -stub single_axon_stub.m
	$(MKOCTFILE) single_axon_octave.cc $(OFILES) $(LIBS)

#
# Instructions for making the corrected mkoctfile script:
#
$(TOP)/share/matwrap/mkoctfile_fixed:
	$(TOP)/share/matwrap/fix_mkoctfile.sh $(TOP)/share/matwrap

########################################################################
#
# Instruction for making the tela modules:
#
TELAKKA	  = telakka

tela:: tela_single_axon

tela_single_axon: $(HFILES) $(OFILES)
	$(MATWRAP) -language tela $(HFILES) -o tela_single_axon.ct
	@echo Ignore warning messages about symbol index out of range,
	@echo if they appear.
	$(TELAKKA) tela_single_axon.ct $(OFILES) $(LIBS)
	$(MV) tela tela_single_axon

########################################################################
#
#
CXX		= g++
CXXFLAGS	= -Wall -O6 -fPIC # -g

single_axon.o: single_axon.cxx $(HFILES)
	$(CXX) $(CXXFLAGS) -c $< -o $@

hodgkin_huxley.o: hodgkin_huxley.cxx misc.h hodgkin_huxley.h
	$(CXX) $(CXXFLAGS) -c $< -o $@

clean::
	$(RM) -rf $(MATLAB_STUBDIR) *.o *_matlab.c *_octave.cc *.mexaxp *.oct \
	*.mex4 *_stub.m *_matlab.c octave-core so_locations *.ct tela_single_axon
