#CXX	= distcc i386-obrien-freebsd5-g++

PLATFORM = $(shell file /bin/ls | cut -d' ' -f3 | cut -d'-' -f1)

BSD_VERSION = $(shell uname -v 2>&1 | cut -d' ' -f2 | cut -d'.' -f1)
SVN_VERSION = $(shell svnversion -n -c . | cut -d':' -f2)

BIN     = ./libserverkey.a

# ---------------------------------------------------------------------------
# ### LINUX-BLOCK-BEGIN (Makefile platform detection)
# FreeBSD takes the else branch, which reproduces the original assignments
# (CXX, the commented-out ifeq/else, IFLAGS and CFLAGS) verbatim.
# ---------------------------------------------------------------------------
UNAME_S := $(shell uname -s)

# Where the 32-bit third-party libraries and their headers live.  Same relative
# convention game/src/Makefile uses for "-I../../../extern/include", with the
# Linux subdirectory appended.  Override on the command line if the tree moves.
EXTERN_DIR ?= ../../../extern/Linux40250

ifeq ($(UNAME_S),Linux)

CXX	= g++

GCC_VERSION = $(shell $(CC) --version 2>&1 | grep "(GCC)" | cut -d' ' -f3  | cut -d'.' -f1)

# OpenSSL headers come from libssl-dev:i386 in /usr/include/openssl, which is
# already on the default search path, so only the extern tree needs adding.
# RSACrypto.cpp calls ::SHA1(), deprecated in OpenSSL 3 but still exported;
# -Wno-deprecated-declarations keeps the build output readable without hiding
# the fact (see the note in the port write-up).
IFLAGS = -I$(EXTERN_DIR)/include
CFLAGS  = $(IFLAGS) -Wall -O2 -pipe -m32 -std=c++23 -D_THREAD_SAFE -fno-exceptions -Wno-deprecated-declarations

ARFLAGS_SK = cr

else

CXX	= clang++-devel

GCC_VERSION = $(shell $(CC) --version 2>&1 | grep "(GCC)" | cut -d' ' -f3  | cut -d'.' -f1)

# ifeq ($(GCC_VERSION), 4)
IFLAGS =
CFLAGS  = $(IFLAGS) -Wall -O2 -pipe -m32 -std=c++23 -D_THREAD_SAFE -fno-exceptions
# else
# IFLAGS =
# CFLAGS  = $(IFLAGS) -Wall -O2 -pipe -mcpu=i686 -D_THREAD_SAFE -fno-exceptions
# endif

ARFLAGS_SK = cru

endif
# ### LINUX-BLOCK-END (Makefile platform detection)

LIBS =

OBJFILES = base64_ssl.o RSACrypto.o

default:
	$(MAKE) $(BIN)

$(BIN): $(OBJFILES)
	ar $(ARFLAGS_SK) $(BIN) $(OBJFILES) $(LIBS)
	ranlib $(BIN)
	chmod 700 $(BIN)

clean:
	rm -f *.o
	rm -f $(BIN)

dep:
	touch Depend
	$(CXX) $(CFLAGS) -MM *.cpp > Depend

$(OBJFILES):
	$(CXX) $(CFLAGS) -c $<

sinclude Depend
