# root directory holding homebrew
# brew install boost openssl@3.5 cmake make

ifeq ($(shell uname -m), x86_64)
	BREWROOT = /usr/local
else
	BREWROOT = /opt/homebrew
endif

BOOSTROOT = ${BREWROOT}/opt/boost
SSLROOT = ${BREWROOT}/opt/openssl@3.5
UPNPROOT = ${BREWROOT}/opt/miniupnpc

CXXFLAGS ?= ${CXX_DEBUG} -Wall -Wno-overloaded-virtual
NEEDED_CXXFLAGS += -std=c++20
INCFLAGS ?= -I${SSLROOT}/include -I${BOOSTROOT}/include
LDFLAGS ?= ${LD_DEBUG}
DEFINES += -DMAC_OSX

ifeq ($(shell uname -m), x86_64)
    CXXFLAGS += -msse
endif

ifeq ($(USE_STATIC),yes)
	LDLIBS = -lz ${SSLROOT}/lib/libcrypto.a ${SSLROOT}/lib/libssl.a ${BOOSTROOT}/lib/libboost_program_options.a ${BOOSTROOT}/lib/libboost_json.a
ifeq ($(USE_UPNP),yes)
	LDLIBS += ${UPNPROOT}/lib/libminiupnpc.a
endif
	LDLIBS +=  -lpthread -ldl
else
	LDFLAGS += -L${SSLROOT}/lib -L${BOOSTROOT}/lib
	LDLIBS = -lz -lssl -lcrypto -lboost_program_options -lboost_json -lpthread
ifeq ($(USE_UPNP),yes)
	LDFLAGS += -L${UPNPROOT}/lib
	LDLIBS += -lminiupnpc
endif
endif

ifeq ($(USE_UPNP),yes)
	DEFINES += -DUSE_UPNP
	INCFLAGS += -I${UPNPROOT}/include
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
