#!/bin/sh

CC=`"${R_HOME}/bin${R_ARCH_BIN}/Rcmd.exe" config CC`
MACHINE=`${CC} -dumpmachine 2>/dev/null`

case "${MACHINE}" in
  aarch64*|arm64*)
    MSSE2=""
    SSE2=""
    SSE2_BITSHUFFLE_OBJ=""
    SSE2_BITSHUFFLE=""
    SSE2_SHUFFLE_OBJ=""
    SSE2_SHUFFLE=""
    SSE2_OBJ_PATH=""
    ;;
  *)
    MSSE2="-msse2"
    SSE2="-DSHUFFLE_SSE2_ENABLED"
    SSE2_BITSHUFFLE_OBJ="bitshuffle-sse2.o"
    SSE2_BITSHUFFLE='bitshuffle-sse2.o: bitshuffle-sse2.c\n\t$(CC) $(FLAGS) -c bitshuffle-sse2.c'
    SSE2_SHUFFLE_OBJ="shuffle-sse2.o"
    SSE2_SHUFFLE='shuffle-sse2.o: shuffle-sse2.c\n\t$(CC) $(FLAGS) -c shuffle-sse2.c'
    SSE2_OBJ_PATH="lib/blosc-1.21.6/"
    ;;
esac

## use R to remove AVX2 references from BLOSC Makefiles
"${R_HOME}/bin${R_ARCH_BIN}/Rscript.exe" -e 'lines <- gsub("@M?AVX2[A-Z_]*@", "", readLines("src/compression_tools/blosc/lib/blosc-1.21.6/Makefile.in"));
             lines <- gsub("@MSSE2@", "'"${MSSE2}"'", lines);
             lines <- gsub("@SSE2@", "'"${SSE2}"'", lines);
             lines <- gsub("@SSE2_BITSHUFFLE_OBJ@", "'"${SSE2_BITSHUFFLE_OBJ}"'", lines);
             lines <- gsub("@SSE2_BITSHUFFLE@", "'"${SSE2_BITSHUFFLE}"'", lines);
             lines <- gsub("@SSE2_SHUFFLE_OBJ@", "'"${SSE2_SHUFFLE_OBJ}"'", lines);
             lines <- gsub("@SSE2_SHUFFLE@", "'"${SSE2_SHUFFLE}"'", lines);
             lines <- gsub("@NO_RLIB@", "", lines);
             out_file <- file("src/compression_tools/blosc/lib/blosc-1.21.6/Makefile", open="wb");
             writeLines(lines, con = out_file, sep = "\n");
             close(out_file);'

"${R_HOME}/bin${R_ARCH_BIN}/Rscript.exe" -e 'lines <- gsub("@M?AVX2[A-Z_]*@", "", readLines("src/compression_tools/blosc/Makefile.in"));
             lines <- gsub("@MSSE2@", "'"${MSSE2}"'", lines);
             lines <- gsub("@SSE2@", "'"${SSE2}"'", lines);
             lines <- gsub("@SSE2_BITSHUFFLE_OBJ@", "'"${SSE2_BITSHUFFLE_OBJ}"'", lines);
             lines <- gsub("@SSE2_BITSHUFFLE@", "'"${SSE2_BITSHUFFLE}"'", lines);
             lines <- gsub("@SSE2_SHUFFLE_OBJ@", "'"${SSE2_SHUFFLE_OBJ}"'", lines);
             lines <- gsub("@SSE2_SHUFFLE@", "'"${SSE2_SHUFFLE}"'", lines);
             lines <- gsub("@SSE2_OBJ_PATH@", "'"${SSE2_OBJ_PATH}"'", lines);
             out_file <- file("src/compression_tools/blosc/Makefile", open="wb");
             writeLines(lines, con = out_file, sep = "\n");
             close(out_file);'

sed \
-e "s^@NO_RLIB@^^" \
src/compression_tools/blosc/lib/snappy-1.2.2/Makefile.in > src/compression_tools/blosc/lib/snappy-1.2.2/Makefile

CFLAGS=`"${R_HOME}/bin${R_ARCH_BIN}/Rcmd.exe" config CFLAGS`
CPICFLAGS=`"${R_HOME}/bin${R_ARCH_BIN}/Rcmd.exe" config CPICFLAGS`
CXX=`"${R_HOME}/bin${R_ARCH_BIN}/Rcmd.exe" config CXX`
CPPFLAGS=`"${R_HOME}/bin${R_ARCH_BIN}/Rcmd.exe" config CPPFLAGS`
RANLIB=`"${R_HOME}/bin${R_ARCH_BIN}/Rcmd.exe" config RANLIB`
R_CPPFLAGS=`"${R_HOME}/bin${R_ARCH_BIN}/Rcmd.exe" config --cppflags`

sed \
-e "s^@CC@^${CC}^" \
-e "s^@CFLAGS@^${CFLAGS}^" \
-e "s^@CPICFLAGS@^${CPICFLAGS}^" \
-e "s^@CXX@^${CXX}^" \
-e "s^@CPPFLAGS@^${CPPFLAGS}^" \
-e "s^@RANLIB@^${RANLIB}^" \
-e "s^@R_CPPFLAGS@^${R_CPPFLAGS}^" \
src/Makevars.win.in > src/Makevars.win

