PKG_CONFIG ?= $(BINPREF)pkg-config
OPENSSL_LIBS := $(shell $(PKG_CONFIG) --libs openssl)

ifneq ($(OPENSSL_LIBS),)
$(info using OpenSSL from Rtools)
OPENSSL_CFLAGS := $(shell $(PKG_CONFIG) --cflags openssl)
else
RWINLIB = ../windows/libssl
TARGET = lib$(subst gcc,,$(COMPILED_BY))$(R_ARCH)
OPENSSL_CFLAGS = -I$(RWINLIB)/include
OPENSSL_LIBS = -L$(RWINLIB)/$(TARGET) -L$(RWINLIB)/lib -lssl -lcrypto
endif

STATLIB = bcrypt/libstatbcrypt.a
LIBBCRYPT = bcrypt/bcrypt_pbkdf.o bcrypt/blowfish.o
PKG_CPPFLAGS = $(OPENSSL_CFLAGS) -DOPENSSL_SUPPRESS_DEPRECATED -DSTRICT_R_HEADERS -DR_NO_REMAP
PKG_LIBS = $(OPENSSL_LIBS) -Lbcrypt -lstatbcrypt -lws2_32 -lgdi32 -lcrypt32


#all: clean
all: $(SHLIB)

$(OBJECTS): $(RWINLIB)

$(SHLIB): $(OBJECTS) $(STATLIB)

$(STATLIB): $(LIBBCRYPT)

$(RWINLIB):
	"${R_HOME}/bin${R_ARCH_BIN}/Rscript.exe" "../tools/winlibs.R"

clean:
	rm -f $(SHLIB) $(OBJECTS) $(STATLIB) $(LIBBCRYPT)

.PHONY: all clean
