#-------------------------------------------------------------------------------
# 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
OS := $(shell uname -s)
ifeq ($(OS),Linux)
	OS = linux
endif
ifeq (${OS},Darwin)
	OS = darwin
endif
ifeq (${OS},SunOS)
	OS = sunos
endif

# OS-specific compiler variables
ifeq ($(OS),sunos)   
	CC = CC
	VERSION_FLAG = -V
	MAKE = gmake
endif
ifeq ($(OS),linux)
	CC = gcc
	VERSION_FLAG = --version
	MAKE = make
endif
ifeq ($(OS),darwin)
	CC = gcc
	VERSION_FLAG = --version
	MAKE = make
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/slbmtestcc models/na1010pn models/na1010pn/regression_na1010pn.dat

slbmtestc: 
	@ bin/slbmtestc models/na1010pn models/na1010pn/regression_na1010pn.dat

slbmtestfort:
	@ bin/slbmtestfort

slbmtestjava: 
	@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; ${MAKE} cleanclasses; 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 ..

#-----------------------------------------------------------
# make sure lib and bin subdirectories exist
makedirs: 
	@ if [ ! -d ${CREATELIBDIR} ]; then \
	(echo ""; \
	echo "Creating Directory /${CREATELIBDIR} ..."; \
	mkdir ${CREATELIBDIR}); \
	fi

	@ if [ ! -d ${CREATEBINDIR} ]; then \
	(echo ""; \
	echo "Creating Directory /${CREATEBINDIR} ..."; \
	mkdir ${CREATEBINDIR}); \
	fi

#-----------------------------------------------------------
# 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 ""

