#################
# Name of Mathematica executable
MATHEXE=math
#################
SHELL=/bin/bash
VERSION := $(shell echo -e $$ Version | tr -d " " | $(MATHEXE) -noprompt | tr -d "\"\n" )
MLINKBASE := $(shell echo -e $$ InstallationDirectory | tr -d " " | $(MATHEXE) -noprompt | tr -d "\"\n")
MLINKDEVKIT := $(MLINKBASE)/SystemFiles/Links/MathLink/DeveloperKit
SYSID :=  $(shell echo -e $$ SystemID | tr -d " " | $(MATHEXE) -noprompt | tr -d "\"\n" )
CADDSDIR := $(MLINKDEVKIT)/$(SYSID)/CompilerAdditions
EXTRA_CFLAGS= -O3 -Wno-unused-result
INCDIR = ${CADDSDIR}
LIBDIR = ${CADDSDIR}
MPREP = ${CADDSDIR}/mprep


ifeq ($(SYSID), Linux)
	BIT := 32
#	libs_for_mathlink = -lML32i3 -lm -lpthread -lrt -lstdc++ 
	static_libs_for_mathlink = -Wl, -Bstatic -lML32i3 -lstdc++ 
	dynamic_libs_for_mathlink = -Wl, -Bdynamic -lm -lrt 
endif

ifeq ($(SYSID), Linux-x86-64) 
	BIT := 64
#	libs_for_mathlink = -lML64i3 -lm -lpthread -lrt -lstdc++  
	static_libs_for_mathlink = -Wl,-Bstatic -lML64i3 -lstdc++ 
	dynamic_libs_for_mathlink = -Wl,-Bdynamic -lm -lrt 
endif

.SILENT:

FLink${BIT} : FLink.o gateToMath.o  gateToFermat.o
	echo -e "\e[0;34mMathematica ${VERSION} used to compile FLink${BIT}\e[0;30m"; \
	${CC} -I${INCDIR} FLink.o gateToMath.o gateToFermat.o -L${LIBDIR} -lpthread $(static_libs_for_mathlink) $(dynamic_libs_for_mathlink) -o $@


.c.o :
	${CC} -c ${EXTRA_CFLAGS} -I${INCDIR} $<

gateToMath.c : gateToMath.tm
	${MPREP} $? -o $@

FLink.o : FLink.c
gateToMath.o : gateToMath.c
gateToFermat.o :  gateToFermat.c

clean:
	rm -f gateToFermat.o gateToMath.o gateToMath.c FLink.o

cleanall: clean
	rm FLink${BIT}
