# set defaults instead redefine
CXXFLAGS ?= ${CXX_DEBUG} -Wall -Wextra -Wno-unused-parameter -pedantic -Wno-psabi
LDFLAGS ?= ${LD_DEBUG}

## NOTE: The NEEDED_CXXFLAGS are here so that custom CXXFLAGS can be specified at build time
## **without** overwriting the CXXFLAGS which we need in order to build.
## For example, when adding 'hardening flags' to the build
## (e.g. -fstack-protector-strong -Wformat -Werror=format-security), we do not want to remove
## -std=c++11. If you want to remove this variable please do so in a way that allows setting
## custom FDLAGS to work at build-time.

# detect proper flag for c++20/с++23 support by compilers
CXXVER := $(shell $(CXX) -dumpversion)
ifneq ($(findstring clang,$(CXX)),)
	NEEDED_CXXFLAGS += -std=c++20
	LDLIBS = -lboost_json
else ifeq ($(shell expr match ${CXXVER} "10"),2) # gcc 10
# for Debian 11
	NEEDED_CXXFLAGS += -std=c++17
else ifeq ($(shell expr match ${CXXVER} "11"),2) # gcc 11
	NEEDED_CXXFLAGS += -std=c++20
else ifeq ($(shell expr match ${CXXVER} "12"),2) # gcc 12
# for Debian 12 with boost 1.74
	NEEDED_CXXFLAGS += -std=c++17 -Wno-bidi-chars
else ifeq ($(shell expr match ${CXXVER} "1[3-9]"),2) # gcc 13+
	NEEDED_CXXFLAGS += -std=c++23 -Wno-bidi-chars
# assume boost >= 1.75
	LDLIBS = -lboost_json
else # not supported
$(error Compiler too old)
endif

NEEDED_CXXFLAGS += -fPIC

ifeq ($(USE_STATIC),yes)
# NOTE: on glibc you will get this warning:
#   Using 'getaddrinfo' in statically linked applications requires at runtime
#   the shared libraries from the glibc version used for linking
	LIBDIR := /usr/lib/$(SYS)
	LDLIBS += $(LIBDIR)/libboost_program_options.a
	LDLIBS += $(LIBDIR)/libboost_json.a
	LDLIBS += $(LIBDIR)/libssl.a
	LDLIBS += $(LIBDIR)/libcrypto.a
	LDLIBS += $(LIBDIR)/libz.a
ifeq ($(USE_UPNP),yes)
	LDLIBS += $(LIBDIR)/libminiupnpc.a
endif
	LDLIBS += -lpthread -ldl
else
	LDLIBS += -lssl -lcrypto -lz -lboost_program_options -lpthread -latomic
ifeq ($(USE_UPNP),yes)
	LDLIBS += -lminiupnpc
endif
endif

# UPNP Support (miniupnpc 1.5 and higher)
ifeq ($(USE_UPNP),yes)
	DEFINES += -DUSE_UPNP
endif

install: all
	install -d ${PREFIX}/bin
	install -m 755 ${I2PD} ${PREFIX}/bin
	install -d ${PREFIX}/etc ${PREFIX}/etc/i2pd ${PREFIX}/etc/i2pd/tunnels.conf.d
	install -m 644 contrib/i2pd.conf contrib/subscriptions.txt contrib/tunnels.conf ${PREFIX}/etc/i2pd
	install -d ${PREFIX}/share ${PREFIX}/share/doc ${PREFIX}/share/doc/i2pd
	install -m 644 ChangeLog LICENSE README.md contrib/i2pd.conf contrib/subscriptions.txt contrib/tunnels.conf ${PREFIX}/share/doc/i2pd
	install -d ${PREFIX}/share/i2pd
	@cp -R contrib/certificates ${PREFIX}/share/i2pd/
	install -d ${PREFIX}/share/man ${PREFIX}/share/man/man1
	@gzip -kf debian/i2pd.1 && install debian/i2pd.1.gz ${PREFIX}/share/man/man1
	install -d ${PREFIX}/var ${PREFIX}/var/lib ${PREFIX}/var/lib/i2pd
	@ln -sf ${PREFIX}/share/i2pd/certificates ${PREFIX}/var/lib/i2pd/certificates
	@ln -sf ${PREFIX}/etc/i2pd/tunnels.conf.d ${PREFIX}/var/lib/i2pd/tunnels.d
	@ln -sf ${PREFIX}/etc/i2pd/i2pd.conf ${PREFIX}/var/lib/i2pd/i2pd.conf
	@ln -sf ${PREFIX}/etc/i2pd/subscriptions.txt ${PREFIX}/var/lib/i2pd/subscriptions.txt
	@ln -sf ${PREFIX}/etc/i2pd/tunnels.conf ${PREFIX}/var/lib/i2pd/tunnels.conf
