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

CREATELIBDIR		= lib
CREATEBINDIR		= bin


# Determine OS
ifeq ($(OS),Windows_NT)
else
OS := $(shell uname -s)
ifeq ($(OS),Linux)
	OS = linux
endif
ifeq (${OS},Darwin)
	OS = darwin
endif
ifeq (${OS},SunOS)
	OS = sunos
endif
endif

# OS-specific compiler variables
ifeq ($(OS),sunos)   
	CC = CC
	VERSION_FLAG = -V
	MAKE = gmake
	RM = rm -f
	P = /
endif
ifeq ($(OS),linux)
	CC = gcc
	VERSION_FLAG = --version
	MAKE = make
	RM = rm -f
	P = /
endif
ifeq ($(OS),darwin)
	CC = gcc
	VERSION_FLAG = --version
	MAKE = make
	RM = rm -f
	P = /
endif
ifeq ($(OS),Windows_NT)
	CC = cl
	VERSION_FLAG =
	MAKE = make
  RM = del /Q
	P = \\
endif


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

# compile all libraries and tests for all 4 languages.

all : info cc c fortran javajni done

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

cc : makedirs slbmcc testcc

c : slbmcshell testc

fortran : slbmfshell testfort

javajni : slbmjni

# build java jar file slbmjni.jar.  This target not performed 
# by 'make all' since jar file always delivered with SLBM_Root
javajar:
	@ cd SLBM_JNI && ${MAKE} javajar && cd ..

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

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} slbmjni && 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}na1010pn models${P}na1010pn${P}regression_na1010pn.dat

slbmtestc: 
	-@ bin${P}slbmtestc models${P}na1010pn models${P}na1010pn${P}regression_na1010pn.dat

slbmtestfort:
	-@ bin${P}slbmtestfort

slbmtestjava: testjava
	-@java -jar bin/slbmtest.jar models/na1010pn models/na1010pn/regression_na1010pn.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.

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

#-----------------------------------------------------------
# make sure lib and bin subdirectories exist
makedirs:
	-mkdir $(CREATELIBDIR)
	-mkdir $(CREATEBINDIR)

#-----------------------------------------------------------
# echo program name and compiler version
info :
	@ echo "========================================="
	@ echo "Building" $(PROG_NAM) "on" $(OS)
	@ $(CC) $(VERSION_FLAG)
	@ echo ""

#-----------------------------------------------------------
done :
	@ echo "========================================="
	@ echo ""
	@echo $(PROG_NAM) "built successfully!"
	@ echo ""

