#===============#
# Compile flags #
#===============#
CC=g++
#DEBUG= -O0 -g
OPTIM = -O2
# CFLAGS=-std=c++11 $(DEBUG) # ----------- See OS-specific code
LFLAGS=-Wall
DEPFLAGS= -MMD -MF $(DEPDIR)/$*.d
GNUPLOT=1
	#The above is to use GNUPLOT for plotting instead of GLSC. Comment it out if it's not needed.

#===============#
#  OS specific  #
#===============#
ifeq ($(OS),Windows_NT)
	#Windows ... or specifically, cygwin
	ifdef GNUPLOT
		CFLAGS=-std=gnu++11 $(DEBUG)
		#Needed because of the use of popen in the gnuplot code
	else
		CFLAGS=-std=c++11 $(DEBUG)
	endif
	OBJ=.win.o
	BIN=.exe
	OpenGL=-lglut -lglu -lopengl32
	DEPDIR=.windeps
else
	#Linux
	CFLAGS=-std=c++11 $(DEBUG)
	OBJ=.o
	BIN=
	OpenGL=-I ~/.local/include -L ~/.local/lib -lGL
	DEPDIR=.deps
endif

#===============#
#   Programs    #
#===============#
LNprogs=StatLN${BIN} LinearLN${BIN} RandLN${BIN}
progs=${LNprogs}

#===============#
#    Objects    #
#===============#
.SECONDEXPANSION:
StatLN${BIN}.OBJS=LN_stat${OBJ}
LinearLN${BIN}.OBJS=LN_linear${OBJ}
RandLN${BIN}.OBJS=LN_random${OBJ}
LNOBJs=${StatLN.OBJS} ${LinearLN.OBJS} ${RandLN.OBJS}
ALLOBJS=${LNOBJs}

#===============#
#   Libraries   #
#===============#
ifdef GNUPLOT
	GRAPHICS_OBJ=-DGNUPLOT=1
else
	GRAPHICS_BIN=-I. -L. -lglsc3d_3 ${OpenGL} -lSDL2 -lfreetype
		#-I ./ isn't needed if you install GLSC3D yourself, I just suspect that no-one has...
endif
MATLAB=-I /opt/matlab/extern/include -L /opt/matlab/bin/glnxa64 -leng -lmx -lm -lmat -lut -Wl,-rpath,/opt/matlab/bin/glnxa64
${LNprogs}: LIB=-lboost_system -lboost_iostreams ${GRAPHICS_BIN}

#==================#
# Generic commands #
#==================#
all: $(progs)
clean:
	rm -f *${OBJ} */*${OBJ} ${progs} .depend ${DEPDIR}/*
	rmdir ${DEPDIR}

#=====================#
# Program compilation #
#=====================#
${progs}: $$($$@.OBJS)
	${CC} ${OPTIM} ${CFLAGS} -o $@ $< ${LIB}
%.o: %.cpp $(DEPDIR) $(DEPDIR)/%.d
	${CC} ${OPTIM} ${CFLAGS} ${LFLAGS} ${LIB} ${DEPFLAGS} ${GRAPHICS_OBJ} -o $@ -c $<
%.win.o: %.cpp $(DEPDIR) $(DEPDIR)/%.d
	${CC} ${OPTIM} ${CFLAGS} ${LFLAGS} ${LIB} ${DEPFLAGS} ${GRAPHICS_OBJ} -o $@ -c $<

#===============#
# Header files  #
#===============#
#These lines ensure the makefile doesn't see the .d files as having no target, and also includes the things as dependencies. The files ar eactually made in the %.o recipe.
#See "header file notes" for explanation, in particular the third source.
${DEPDIR}:
	mkdir -p ${DEPDIR}
$(DEPDIR)/%.d: ;
-include $(patsubst %${OBJ},$(DEPDIR)/%.d,$(ALLOBJS))
