# This Makefile is used under Linux

MACHINE = $(shell uname)

ifeq ($(MACHINE), Darwin)
	MATLABDIR = /opt/local/matlab
	ARCH = -arch i386
	CBLAS = -latlas
else                                 
	ifeq ($(shell uname -n), senna)
		MATLABDIR = /opt/matlab/#using senna
	else
		MATLABDIR = /usr/local/lib/matlab7/#using Stuffen
		ARCH =
		CBLAS = -lgslcblas
	endif 
endif

#MATLABDIR ?= /opt/matlab/#using Senna
#CXX = /usr/bin/g++-4.1.2 #Compile using Stuffen
#CXX = /usr/bin/g++-4.1.1 #compile using Senna
CXX = /usr/bin/g++ #compile in Macbook

CFLAGS = -Wall -O3 -fPIC $(ARCH) $(CBLAS) -I$(MATLABDIR)/extern/include -I../src

MEX = $(MATLABDIR)/bin/mex
MEX_OPTION = CC\#$(CXX) CXX\#$(CXX) CFLAGS\#"$(CFLAGS)" CXXFLAGS\#"$(CFLAGS)"
# comment the following line if you use MATLAB on 32-bit computer
MEX_OPTION += -largeArrayDims $(CBLAS)
MEX_EXT = $(shell $(MATLABDIR)/bin/mexext)
             
BASIC_DIST  = m_BranchBound.cpp m_utils.h m_utils.cpp m_malloc.h
BASIC_DIST1 = m_TreeNode1.h m_TreeNode1.cpp  
BASIC_DIST2 = m_TreeNode2.h m_TreeNode2.cpp 

DIST1_src = m_TreeNode1b.h m_TreeNode1b.cpp m_TreeNode1e.h m_TreeNode1e.cpp 
DIST1_mexSrc = m_mexFindBox1b2.cpp ml_findBox1b2.m
DIST1_OUT = ../dist/BeyondSlideWin_v1.0/

	

all: binary	

binary: m_mexFindBox1a.$(MEX_EXT) m_mexFindBox1b.$(MEX_EXT) m_mexFindBox1b2.$(MEX_EXT)\
	m_mexFindBox1c.$(MEX_EXT) m_mexFindBox1d.$(MEX_EXT) \
	m_mexFindBox1f.$(MEX_EXT) m_mexFindBoxes.$(MEX_EXT) m_mexFindBoxes2c.$(MEX_EXT)\
	m_mexFindBox1g.$(MEX_EXT) m_mexFindBox1g2.$(MEX_EXT) \
	m_mexFindBox1h.$(MEX_EXT) \
	m_mexFindBox1i.$(MEX_EXT) \
	m_mexFindBox1j.$(MEX_EXT) \
	m_mexFindMulti1DSegs.$(MEX_EXT) m_mexFindMulti1DSegs2.$(MEX_EXT) \
	m_mexFindMulti1DSegs_seq.$(MEX_EXT) m_mexFindMulti1DSegs_seq2.$(MEX_EXT) \
	m_mexFindNormMulti1DSegs.$(MEX_EXT) \
	m_mexMultiLabelKSeg.$(MEX_EXT) \
	m_mexMultiLabelKSeg2.$(MEX_EXT) \
	m_mexMMCSeg.$(MEX_EXT) m_mexRandomSeg.$(MEX_EXT) m_mexKmeansSeg.$(MEX_EXT)\
	m_mexFindMulti1DSegs3.$(MEX_EXT) m_TreeNode3.o\
	moveto


m_TreeNode1.o: ../src/m_TreeNode1.cpp
	$(CXX) $(CFLAGS) -c $<                                           

m_TreeNode1a.o: ../src/m_TreeNode1a.cpp
	$(CXX) $(CFLAGS) -c $<                       

m_TreeNode1b.o: ../src/m_TreeNode1b.cpp
	$(CXX) $(CFLAGS) -c $<
			
m_TreeNode1c.o: ../src/m_TreeNode1c.cpp
	$(CXX) $(CFLAGS) -c $<                                            

m_TreeNode1d.o: ../src/m_TreeNode1d.cpp
	$(CXX) $(CFLAGS) -c $<	

m_TreeNode1e.o: ../src/m_TreeNode1e.cpp
	$(CXX) $(CFLAGS) -c $<

m_TreeNode1f.o: ../src/m_TreeNode1f.cpp
	$(CXX) $(CFLAGS) -c $<          
	
m_TreeNode1g.o: ../src/m_TreeNode1g.cpp
	$(CXX) $(CFLAGS) -c $<	              

m_TreeNode1h.o: ../src/m_TreeNode1h.cpp
	$(CXX) $(CFLAGS) -c $<   
	
m_TreeNode1i.o: ../src/m_TreeNode1i.cpp
	$(CXX) $(CFLAGS) -c $<	

m_TreeNode1j.o: ../src/m_TreeNode1j.cpp
	$(CXX) $(CFLAGS) -c $<  

m_TreeNode2.o: ../src/m_TreeNode2.cpp
	$(CXX) $(CFLAGS) -c $<   

m_TreeNode2a.o: ../src/m_TreeNode2a.cpp
	$(CXX) $(CFLAGS) -c $<   

m_TreeNode2b.o: ../src/m_TreeNode2b.cpp
	$(CXX) $(CFLAGS) -c $<

m_TreeNode2c.o: ../src/m_TreeNode2c.cpp
	$(CXX) $(CFLAGS) -c $<         
	
m_utils.o: ../src/m_utils.cpp
	$(CXX) $(CFLAGS) -c $<
	
m_TreeNode3.o: ../src/m_TreeNode3.cpp
	$(CXX) $(CFLAGS) -c $<  	

m_multiLabelKSeg.o: ../src/m_multiLabelKSeg.cpp ../src/m_multiLabelKSeg.h
	$(CXX) $(CFLAGS) -c $<
  

#m_BranchBound.o:
#	$(CXX) $(CFLAGS) -c ../src/m_BranchBound.cpp;
	


m_Multi1DSegs.o: ../src/m_Multi1DSegs.cpp
	$(CXX) $(CFLAGS) -c $<       
	
m_NormMulti1DSegs.o: ../src/m_NormMulti1DSegs.cpp
	$(CXX) $(CFLAGS) -c $<

m_mexFindBox1a.$(MEX_EXT): m_mexFindBox1a.cpp m_TreeNode1.o m_utils.o m_TreeNode1a.o 
	$(MEX) $(MEX_OPTION) $^

m_mexFindBox1b.$(MEX_EXT): m_mexFindBox1b.cpp m_TreeNode1.o m_utils.o m_TreeNode1e.o m_TreeNode1b.o
	$(MEX) $(MEX_OPTION) $^   
	
m_mexFindBox1b2.$(MEX_EXT): m_mexFindBox1b2.cpp m_TreeNode1.o m_utils.o m_TreeNode1e.o m_TreeNode1b.o
	$(MEX) $(MEX_OPTION) $^	
    
m_mexFindBox1c.$(MEX_EXT): m_mexFindBox1c.cpp m_TreeNode1.o m_utils.o m_TreeNode1c.o
	$(MEX) $(MEX_OPTION) $^

m_mexFindBox1d.$(MEX_EXT): m_mexFindBox1d.cpp m_TreeNode1.o m_utils.o m_TreeNode1d.o
	$(MEX) $(MEX_OPTION) $^

m_mexFindBox1f.$(MEX_EXT): m_mexFindBox1f.cpp m_TreeNode1.o m_utils.o m_TreeNode1f.o
	$(MEX) $(MEX_OPTION) $^

m_mexFindBox1g.$(MEX_EXT): m_mexFindBox1g.cpp m_TreeNode1.o m_utils.o m_TreeNode1g.o m_TreeNode1e.o
	$(MEX) $(MEX_OPTION) $^

m_mexFindBox1g2.$(MEX_EXT): m_mexFindBox1g2.cpp m_TreeNode1.o m_utils.o m_TreeNode1g.o m_TreeNode1e.o
	$(MEX) $(MEX_OPTION) $^

m_mexFindBox1h.$(MEX_EXT): m_mexFindBox1h.cpp m_TreeNode1.o m_utils.o m_TreeNode1e.o m_TreeNode1h.o
	$(MEX) $(MEX_OPTION) $^

m_mexFindBox1i.$(MEX_EXT): m_mexFindBox1i.cpp m_TreeNode1.o m_utils.o m_TreeNode1i.o 
	$(MEX) $(MEX_OPTION) $^

m_mexFindBox1j.$(MEX_EXT): m_mexFindBox1j.cpp m_TreeNode1.o m_utils.o m_TreeNode1j.o 
	$(MEX) $(MEX_OPTION) $^

m_mexFindBoxes.$(MEX_EXT): m_mexFindBoxes.cpp m_TreeNode2.o m_TreeNode1.o m_utils.o m_TreeNode1a.o m_TreeNode2a.o m_TreeNode2b.o
	$(MEX) $(MEX_OPTION) $^                             
	
m_mexFindBoxes2c.$(MEX_EXT): m_mexFindBoxes2c.cpp m_TreeNode2.o m_TreeNode2c.o m_utils.o 
	$(MEX) $(MEX_OPTION) $^
	
m_mexFindMulti1DSegs.$(MEX_EXT): m_mexFindMulti1DSegs.cpp m_TreeNode1.o m_TreeNode1e.o m_Multi1DSegs.o m_TreeNode1b.o m_utils.o
	$(MEX) $(MEX_OPTION) $^  
	
m_mexFindMulti1DSegs2.$(MEX_EXT): m_mexFindMulti1DSegs2.cpp m_TreeNode1.o m_TreeNode1e.o m_Multi1DSegs.o m_TreeNode1b.o m_utils.o
	$(MEX) $(MEX_OPTION) $^	

m_mexFindMulti1DSegs3.$(MEX_EXT): m_mexFindMulti1DSegs3.cpp m_multiLabelKSeg.o m_utils.o
	$(MEX) $(MEX_OPTION) $^ 

m_mexFindMulti1DSegs_seq.$(MEX_EXT): m_mexFindMulti1DSegs_seq.cpp m_TreeNode1.o m_TreeNode1e.o m_Multi1DSegs.o m_TreeNode1b.o m_utils.o
	$(MEX) $(MEX_OPTION) $^
	
m_mexFindMulti1DSegs_seq2.$(MEX_EXT): m_mexFindMulti1DSegs_seq2.cpp m_TreeNode1.o m_TreeNode1e.o m_Multi1DSegs.o m_TreeNode1b.o m_utils.o
	$(MEX) $(MEX_OPTION) $^ 
	
m_mexFindNormMulti1DSegs.$(MEX_EXT): m_mexFindNormMulti1DSegs.cpp m_Multi1DSegs.o m_NormMulti1DSegs.o m_TreeNode1.o m_TreeNode1e.o  m_TreeNode1b.o m_utils.o
	$(MEX) $(MEX_OPTION) $^	                
	
# m_mexGetTPFP4allNSegs.$(MEX_EXT): m_mexGetTPFP4allNSegs.cpp m_TreeNode1.o m_TreeNode1e.o m_Multi1DSegs.o m_TreeNode1b.o m_utils.o
# 	$(MEX) $(MEX_OPTION) $^	                

m_mexMultiLabelKSeg.$(MEX_EXT): m_mexMultiLabelKSeg.cpp m_multiLabelKSeg.o m_utils.o
	$(MEX) $(MEX_OPTION) $^	                

m_mexMultiLabelKSeg2.$(MEX_EXT): m_mexMultiLabelKSeg2.cpp m_multiLabelKSeg.o m_utils.o
	$(MEX) $(MEX_OPTION) $^                 
	              
m_mexMMCSeg.$(MEX_EXT): m_mexMMCSeg.cpp m_multiLabelKSeg.o m_utils.o
	$(MEX) $(MEX_OPTION) $^	   
	
m_mexKmeansSeg.$(MEX_EXT): m_mexKmeansSeg.cpp m_multiLabelKSeg.o m_utils.o
	$(MEX) $(MEX_OPTION) $^    

m_mexRandomSeg.$(MEX_EXT): m_mexRandomSeg.cpp m_multiLabelKSeg.o m_utils.o
	$(MEX) $(MEX_OPTION) $^	                

dist1:       
	mkdir -p $(DIST1_OUT)/src; mkdir -p $(DIST1_OUT)/mexSrc; \
	for file in $(BASIC_DIST) $(BASIC_DIST1) $(DIST1_src); do \
		cp ../src/$$file $(DIST1_OUT)/src/; \
	done; \
	for file in $(DIST1_mexSrc); do \
		cp $$file $(DIST1_OUT)/mexSrc/; \
	done; \
	cp m_Makefile $(DIST1_OUT)/mexSrc/Makefile

moveto:
	mkdir -p ../mexBin
	cp *.mex* ../mexBin 
clean:
	rm -f *~ *.o *.mex* *.obj
