BIN = re6st-conf re6st-registry re6stnet
NOPART = chrpath bison flex gnu-config lunzip m4 patch perl popt site_perl xz-utils
CLEAN_KEEP = babeld bin/buildout buildout.cfg download-cache extends-cache python3.tar.xz eggs/wheel-0.41.2-py3.11.egg eggs/slapos.libnetworkcache-0.25-py3.11.egg eggs/pip-23.2.1-py3.11.egg eggs/zc.buildout-3.0.1+slapos009-py3.11.egg eggs/setuptools-67.8.0-py3.11.egg
TARGET = opt/re6st
# Note that this file and related scripts (build_python3_if_needed,
# install-eggs) don't contain anything specific to re6stnet. They could be
# reused as is for SlapOS.

# We don't strip ELF files because all dists automatically do it.
# For those that produce debug packages, these packages are anyway quite small
# (debug information also compresses quite well).

BUILD = $(TARGET)

PATH := $(CURDIR)/$(BUILD)/parts/chrpath/bin:$(CURDIR)/$(BUILD)/parts/file/bin:$(CURDIR)/$(BUILD)/parts/perl/bin:$(PATH)

all: $(BUILD)/.installed.cfg
ifneq ($(wildcard upstream.mk),)
	make -f upstream.mk
%: _%
	make -f upstream.mk $@
_install%: _install;
Makefile:;
else
install: _install
endif

$(BUILD)/.installed.cfg: $(BUILD)/buildout.cfg
	cd $(BUILD) && $(shell ./build_python3_if_needed) bin/buildout \
	buildout:relative-paths=false babeld-repository:recipe=
	touch $@

PROGS = $(patsubst %,$(DESTDIR)/usr/bin/%,$(BIN))

_install: $(DESTDIR)/$(TARGET) $(PROGS);

$(PROGS): $(BUILD)/.installed.cfg
	mkdir -p $(@D)
	sed s,$(CURDIR)/$(BUILD),/$(TARGET),g $(BUILD)/bin/$(@F) > $@
	chmod +x $@

$(DESTDIR)/$(TARGET): $(BUILD)/.installed.cfg
	rm -rf $@ && mkdir -p $@/parts
	cd $(BUILD) && parts/python3/bin/python3 \
	$(CURDIR)/install-eggs $@ $(BIN)
	for x in $(filter-out $(NOPART),$(shell cd $(BUILD)/parts && echo *)); \
	do cp --preserve=links -r $(BUILD)/parts/$$x $@/parts; done
	cd $@ && $(CURDIR)/cleanup && chmod -R u+w .

# Fix paths in files, first ELF, then non-binary
	@set -ex $(CURDIR)/$(BUILD) /$(TARGET); set $$* "s:\\Q$$1\\E:$$2:g"; \
	x=`find $@ -type f \( -executable -o -name '*.so*' \)`; x=`file $$x`; \
	x=`echo "$$x" |sed -n 's/:.*ELF.*\(executable\|shared\).*//p'`; \
	x=`chrpath -k $$x |perl -ne "$$3"'; \
		s/(.*): R(UN)?PATH=(.*)/chrpath -r $$3 $$1/ and print'`; \
	eval "$$x"; \
	if x=`grep -rIlF $$1 $@`; then \
		echo "Fixing path to $$2 rather than $$1:" $$x; \
		perl -pi -e "$$3" $$x; fi

# Fix symlinks at the end because they'll become broken inside DESTDIR.
# Maybe we should generate relative symlinks, or let dists do it.
	set -e $(CURDIR)/$(BUILD)/; \
	find $@ -type l -lname "$$1*" -printf '%l %p\n' |cut -c"$${#1}"- | \
	while read s d; do echo "Fixing symlink $$d"; \
		rm $$d; ln -s /$(TARGET)$$s $$d; \
	done

uniq = $(if $1,$(firstword $1) $(call uniq,$(filter-out $(firstword $1),$1)))
_dir = $(patsubst %/,%,$(call uniq,$(filter-out ./, $(dir $1))))
_anc = $(if $1,$1 $(call _anc,$(call _dir,$1)))

clean:
	cd $(BUILD) && find -mindepth 1 \( \
		$(patsubst %, -path ./% -o,$(CLEAN_KEEP)) \
		$(patsubst %, ! -path ./%,$(call _anc,$(call _dir,$(CLEAN_KEEP)))) \
		-print0 \) -prune |xargs -r0 rm -r
