#############################################################
# Makefile to build  highsee
# ddaniel, 20 May 2021
#

TOPDIR=..
LIBNAME=highsee
HAS_ONLY_RELEASE_MODE_PRECOMPILED=0
include $(TOPDIR)/Makefiles/Repo-defines.mk

LIBHIGHSEE_VER=23_02_14

HIGHSEE_SHARED_LIBS:=\
	lib/libHighSeeApps*

#
# NOTE: the following dependencies are already part of FC35 and thus do not need to
#       be copied in the Conan package shipped by this repo:
#
#                        FC33        FC35          LINKEDVER
#    liblog4cxx         v0.10.0     v0.11.0
#    libapr/aprutil     v1.7.0      v1.7.0
#    libgcrypt          v1.8.8      v1.9.3
#    libgpg-error       v1.41       v1.42
#
HIGHSEE_DEPENDENCIES_LIBS:=\
	lib/liblog4cxx* \
	lib/libapr-1* \
	lib/libaprutil-1* \
	lib/libgcrypt* \
	lib/libgpg-error*

.PHONY: all clean distclean

all:
	rm -rf highsee_oracle85_$(LIBHIGHSEE_VER)
	mkdir -p $(PRECOMPILED_DIR) highsee_oracle85_$(LIBHIGHSEE_VER)
	tar -xvf highsee_oracle85_$(LIBHIGHSEE_VER).tar -C highsee_oracle85_$(LIBHIGHSEE_VER) && \
		cd highsee_oracle85_$(LIBHIGHSEE_VER) &&  \
		mkdir -p ../$(BUILDID)/ && \
		cp -fvL $(HIGHSEE_SHARED_LIBS) ../$(BUILDID)/ && \
		mkdir -p ../include/highsee && \
		cp -fv include/* ../include/highsee && \
		mkdir -p ../conf && \
		cp -rfv conf/* ../conf && \
		mkdir -p ../etc && \
		cp -fv etc/* ../etc 
	sed -i "s/Log Level: 3/Log Level: 5/" ./etc/configure
	sed -i "s/HS Chunker Disable NAT-ed traffic: Yes/HS Chunker Disable NAT-ed traffic: No/" ./etc/configure
	sed -i "s/HS Chunker Validate sessions with NAT-ed traffic: No/HS Chunker Validate sessions with NAT-ed traffic: Yes/" ./etc/configure
	# IMPORTANT: some Highsee shared libraries need to load other .so files (e..g Highsee.so depends from Highsee_cg.so)
	#            and since Fedora35 the dynamic linker wants every .so location explicitly mentioned as RPATH... so to simplify
	#            deployments (especially with different CFG values), we just tell the linker to look in the same folder where
	#            e.g. Highsee.so is present to find the associated Highsee_cg.so file
	for thelib in $$(ls $(BUILDID)/*.so*); do \
		echo "Patching $$thelib by setting as RPATH the special ORIGIN path" ; \
		patchelf --set-rpath '$$ORIGIN' $$thelib ; \
	done

clean_build_intermediate_artifacts:
	rm -rf highsee_oracle85_$(LIBHIGHSEE_VER)

clean_build_outputs: clean
	rm -f $(BUILDID)/*.so*
	rm -f $(BUILDID)/*.a*

include $(TOPDIR)/Makefiles/ThirdPartyTargetHelpers.mk
