cd ./src/main/c

echo "SAVE STEPS $SAVE_STEPS"

LIB_ENDING=".so"
BASE_INCLUDE_DIR=""
OS_INCLUDE_DIR=""

#determine os
if [[ "$OSTYPE" == "linux-gnu"* ]]; then
    #linux
    LIB_ENDING=".so"
    BASE_INCLUDE_DIR="${JAVA_HOME}/include"
    OS_INCLUDE_DIR="${JAVA_HOME}/include/linux"
elif [[ "$OSTYPE" == "darwin"* ]]; then
    # Mac OSX
    LIB_ENDING=".so"
elif [[ "$OSTYPE" == "cygwin" ]]; then
    # POSIX compatibility layer and Linux environment emulation for Windows
    LIB_ENDING=".dll"
elif [[ "$OSTYPE" == "msys" ]]; then
    # Lightweight shell and GNU utilities compiled for Windows (part of MinGW)
    LIB_ENDING=".dll"
    BASE_INCLUDE_DIR="${JAVA_HOME}/include"
    OS_INCLUDE_DIR="${JAVA_HOME}/include/win32"
elif [[ "$OSTYPE" == "win32" ]]; then
    # I'm not sure this can happen.
    LIB_ENDING=".dll"
elif [[ "$OSTYPE" == "freebsd"* ]]; then
    # ...
    LIB_ENDING=".so"
else
    # Unknown.
    LIB_ENDING=".so"
fi

# echo $PWD

#clean compile dir
rm -f ./*.o
rm -f ./*.so
rm -f ./*.dll


#compile object files
# COMPILE_FLAGS="-c -fPIC -m64 -mavx -mavx2 -march=native -Ofast -msse -msse2 -msse3 -mmmx -m3dnow"
# INPUT_FILES="./src/densitystep.c"
# OUTPUT_FILE="./densitystep.o"
# gcc $COMPILE_FLAGS -I"$BASE_INCLUDE_DIR" -I"$OS_INCLUDE_DIR" $INPUT_FILES -o $OUTPUT_FILE

# COMPILE_FLAGS="-c -fPIC -m64 -mavx -mavx2 -march=native -Ofast -msse -msse2 -msse3 -mmmx -m3dnow"
# INPUT_FILES="./src/velocitystep.c"
# OUTPUT_FILE="./velocitystep.o"
# gcc $COMPILE_FLAGS -I"$BASE_INCLUDE_DIR" -I"$OS_INCLUDE_DIR" $INPUT_FILES -o $OUTPUT_FILE

# COMPILE_FLAGS="-c -fPIC -m64 -mavx -mavx2 -march=native -Ofast -msse -msse2 -msse3 -mmmx -m3dnow"
# INPUT_FILES="./src/chunkmask.c"
# OUTPUT_FILE="./chunkmask.o"
# gcc $COMPILE_FLAGS -I"$BASE_INCLUDE_DIR" -I"$OS_INCLUDE_DIR" $INPUT_FILES -o $OUTPUT_FILE

COMPILE_FLAGS="-c -fPIC -m64 -mavx -mavx2 -march=native -Ofast -DSAVE_STEPS=$SAVE_STEPS"
INPUT_FILES="./src/javainterface.c"
OUTPUT_FILE="./javainterface.o"
gcc $COMPILE_FLAGS -I"$BASE_INCLUDE_DIR" -I"$OS_INCLUDE_DIR" $INPUT_FILES -o $OUTPUT_FILE

COMPILE_FLAGS="-c -fPIC -m64 -mavx -mavx2 -march=native -Ofast -DSAVE_STEPS=$SAVE_STEPS"
INPUT_FILES="./src/fluidsim.c"
OUTPUT_FILE="./fluidsim.o"
gcc $COMPILE_FLAGS -I"$BASE_INCLUDE_DIR" -I"$OS_INCLUDE_DIR" $INPUT_FILES -o $OUTPUT_FILE



#compile shared object file
OUTPUT_FILE="libfluidsim$LIB_ENDING"
COMPILE_FLAGS="-shared"
INPUT_FILES="fluidsim.o javainterface.o"
gcc $COMPILE_FLAGS $INPUT_FILES -o $OUTPUT_FILE

#move to resources
mkdir -p ../../../shared-folder
mv "./libfluidsim$LIB_ENDING" "../../../shared-folder/"

#clean compile dir
rm -f ./*.o
rm -f ./*.so
rm -f ./*.dll