#-------------------------------------------------------------------------------
# Define Program, Program Directory, Program Version, Install Directories, and
# Target Names ... set mode to default (empty)
#-------------------------------------------------------------------------------

# Determine OS
ifeq ($(OS),Windows_NT)
else
  OS := $(shell uname -s)
endif

ifeq ($(ARCH),32bit)
else
ARCH=64bit
endif

# OS-specific compiler variables
ifeq ($(OS),SunOS)   
	CC = CC
	VERSION = CC -V
	MAKE = gmake
	RM = rm -rf
	P = /
	EXT = so
	MV = mv
endif
ifeq ($(OS),Linux)
	CC = gcc
	VERSION = gcc --version
	MAKE = make
	RM = rm -rf
	P = /
	EXT = so
	MV = mv
endif
ifeq ($(OS),Darwin)
	CC = gcc
	VERSION = gcc --version
	MAKE = make
	RM = rm -rf
	P = /
	EXT = dylib
	MV = mv
endif
ifeq ($(OS),Windows_NT)
	CC = cl
	VERSION =
	MAKE = make
  RM = del /Q
	P = \\
	EXT = dll
	MV = MOVE /Y
endif


#-------------------------------------------------------------------------------
# targets
#-------------------------------------------------------------------------------

# compile all libraries and tests for all 4 languages.

all : info setup geotess cc c fortran javajni done

setup:
	-@mkdir lib && mkdir bin

#-----------------------------------------------------------
# for each language independently, compile library and test

cc : slbmcc testcc

c : slbmcshell testc

fortran : slbmfshell testfort

javajni : slbmjni testjava

# build java jar file slbmjni.jar.  
javajar:
	@ cd SLBM_JNI && ${MAKE} javajar && cd ..

#-----------------------------------------------------------
# compile just the libraries:

geotess: 
	@ cd GeoTessCPP && ${MAKE} all && cd .. && $(MV) GeoTessCPP${P}lib${P}libgeotesscpp.* lib${P} && $(RM) GeoTessCPP${P}lib

slbmcc: 
	@ cd SLBM && ${MAKE} all && cd .. 

slbmcshell:
	@ cd SLBM_C_shell && ${MAKE} all && cd ..

slbmfshell:
	@ cd SLBM_Fort_shell && ${MAKE} all && cd ..

slbmjni:
	@ cd SLBM_JNI && ${MAKE} all && cd ..

#-----------------------------------------------------------
# compile just the tests

testcc:
	@ cd SLBM_test_cc && ${MAKE} all && cd ..

testc:
	@ cd SLBM_test_c && ${MAKE} all && cd ..

testfort:
	@ cd SLBM_test_fort && ${MAKE} all && cd ..
	
testjava:
	@ cd SLBM_test_java && ${MAKE} all && cd ..

#-----------------------------------------------------------
# run the tests:

slbmtest: slbmtestcc slbmtestc slbmtestfort slbmtestjava

slbmtestcc: 
	-@ bin${P}slbmtestcc models${P}rstt.2.3.geotess models${P}rstt.2.3_regression_linear.dat

slbmtestc: 
	-@ bin${P}slbmtestc models${P}rstt.2.3.geotess models${P}rstt.2.3_regression_linear.dat

slbmtestfort:
	-@ bin${P}slbmtestfort models${P}rstt.2.3.geotess models${P}rstt.2.3_regression_linear.dat

slbmtestjava:
	-@java -jar bin${P}slbmtest.jar models${P}rstt.2.3.geotess models${P}rstt.2.3_regression_linear.dat

#-----------------------------------------------------------
# clean all the object files in all libraries and test. This happens automatically when
# a module compiles successfully so it is generally not necessary to call this.

clean_objs: 
	@ cd GeoTessCPP && ${MAKE} clean_objs && cd ..
	@ cd SLBM && ${MAKE} clean_objs && cd ..
	@ cd SLBM_C_shell && ${MAKE} clean_objs && cd ..
	@ cd SLBM_Fort_shell && ${MAKE} clean_objs && cd ..
	@ cd SLBM_JNI && ${MAKE} clean_objs && cd ..
	@ cd SLBM_test_cc && ${MAKE} clean_objs && cd ..
	@ cd SLBM_test_c && ${MAKE} clean_objs && cd ..
	@ cd SLBM_test_fort && ${MAKE} clean_objs && cd ..
	@ cd SLBM_test_java && ${MAKE} cleanclasses && cd ..
	@ cd lib && $(RM) *
	@ cd bin && $(RM) *

#-----------------------------------------------------------
info :
	@ echo "========================================="
	@ echo "Building SLBM_Root on $(OS)"
	@ $(VERSION)
	@ echo ""

#-----------------------------------------------------------
done :
	@ echo "========================================="
	@ echo ""
	-@ environment_variables.sh
	@ echo ""
	@ echo "SLBM_Root built successfully!"
	@ echo ""

