<commit_before>DOCKER ?= docker
NAMESPACE = $(shell basename `pwd`)
LRM = $(NAMESPACE)_erlang_linux_release_builder
MAKER_EXISTS = $(shell $(DOCKER) images -q $(LRM) 2> /dev/null)
ROOT_VOLUME = $(NAMESPACE)_root
ROOT_MOUNT_POINT = /$(NAMESPACE)
DEPS_VOLUME = $(NAMESPACE)_deps
DEPS_MOUNT_POINT = $(ROOT_MOUNT_POINT)/deps
REL_VOLUME = $(NAMESPACE)_rel
REL_MOUNT_POINT = $(ROOT_MOUNT_POINT)/_rel
EBIN_VOLUME = $(NAMESPACE)_ebin
EBIN_MOUNT_POINT = $(ROOT_MOUNT_POINT)/ebin
EDOCKER_REPO = https://github.com/peffis/edocker.mk.git
BINARIES_TO_INCLUDE ?=
EXTRA_PPAS ?=
SOURCES_LIST_APPEND ?=
EXTRA_PACKAGES ?=
ARTEFACT_VOLUMES = $(DEPS_VOLUME) $(REL_VOLUME) $(EBIN_VOLUME)

define log_msg
	@printf "\033[1;37m===> "$(1)"\033[0m\n"
endef


volumes: $(ROOT_VOLUME) artefact_volumes


$(ROOT_VOLUME):
	$(call log_msg,"checking "$(ROOT_VOLUME)" volume")
	@if [ `$(DOCKER) volume inspect $(ROOT_VOLUME) 2> /dev/null | head -1` = "[]" ]; then \
		printf "created " && $(DOCKER) volume create $(ROOT_VOLUME); \
	fi
	@$(DOCKER) run --name tmp_builder -v $(ROOT_VOLUME):$(ROOT_MOUNT_POINT) bravissimolabs/alpine-git echo > /dev/null 2>&1
	@$(DOCKER) cp . tmp_builder:$(ROOT_MOUNT_POINT) > /dev/null 2>&1
	@$(DOCKER) stop tmp_builder > /dev/null 2>&1
	@$(DOCKER) rm tmp_builder > /dev/null 2>&1


artefact_volumes:
	@$(foreach v,$(ARTEFACT_VOLUMES), \
		printf "\033[1;37m===> checking ${v} volume\033[0m\n"; \
		if [ `$(DOCKER) volume inspect $(v) 2> /dev/null | head -1` = "[]" ]; then \
			printf "created " && $(DOCKER) volume create $(v); \
		fi;)


linux_release_build_machine: volumes
	$(call log_msg,"checking linux build machine")
	@if [ `$(DOCKER) images -q $(LRM) 2> /dev/null`"abc" = "abc" ]; then \
		echo "rebuilding release machine"; \
		echo "cloning edocker repo"; \
		mkdir .tmp_context; \
		$(DOCKER) run --rm -v $(ROOT_VOLUME):$(ROOT_MOUNT_POINT) bravissimolabs/alpine-git \
			git clone --verbose --progress -b using_volumes $(EDOCKER_REPO) $(ROOT_MOUNT_POINT)/.edocker; \
		echo "copying Dockerfile.builder"; \
		$(DOCKER) run --rm -v $(ROOT_VOLUME):$(ROOT_MOUNT_POINT) bravissimolabs/alpine-git \
			cat $(ROOT_MOUNT_POINT)/.edocker/builder/Dockerfile.builder > .Dockerfile.builder; \
		echo "building docker image"; \
		$(DOCKER) build --build-arg SOURCES_LIST_APPEND="${SOURCES_LIST_APPEND}" --build-arg EXTRA_PPAS="${EXTRA_PPAS}" --build-arg EXTRA_PACKAGES="${EXTRA_PACKAGES}" -t $(LRM) -f .tmp_context/Dockerfile.builder .tmp_context; \
		rm -rf .tmp_context; \
	fi


linux_release: linux_release_build_machine
	$(call log_msg,"making linux release")

	$(eval RELEASE_NAME := $(shell $(DOCKER) run --rm \
		-v $(ROOT_VOLUME):$(ROOT_MOUNT_POINT) \
		erlang $(ROOT_MOUNT_POINT)/.edocker/bin/release_name))

	$(eval ERTS_VERSION := $(shell $(DOCKER) run --rm \
		-v $(ROOT_VOLUME):$(ROOT_MOUNT_POINT) \
		erlang $(ROOT_MOUNT_POINT)/.edocker/bin/system_version))

	$(foreach binary,$(BINARIES_TO_INCLUDE),\
		$(DOCKER) run --rm \
			-v $(ROOT_VOLUME):$(ROOT_MOUNT_POINT) \
			-v $(REL_VOLUME):$(REL_MOUNT_POINT) \
			$(LRM) bash -c "cp \`which ${binary}\` ${REL_MOUNT_POINT}/${RELEASE_NAME}/bin/";)

	@$(DOCKER) run  --rm \
			-v $(ROOT_VOLUME):/$(RELEASE_NAME) \
			-v $(REL_VOLUME):/$(RELEASE_NAME)/_rel \
			-v $(DEPS_VOLUME):/$(RELEASE_NAME)/deps \
			-v $(EBIN_VOLUME):/$(RELEASE_NAME)/ebin \
			$(LRM) bash -c "cd /${RELEASE_NAME} && make && .edocker/bin/mkimage ${BINARIES_TO_INCLUDE}"

	@$(DOCKER) run  --rm \<commit_msg>fix broken tmp_context<commit_after>DOCKER ?= docker
NAMESPACE = $(shell basename `pwd`)
LRM = $(NAMESPACE)_erlang_linux_release_builder
MAKER_EXISTS = $(shell $(DOCKER) images -q $(LRM) 2> /dev/null)
ROOT_VOLUME = $(NAMESPACE)_root
ROOT_MOUNT_POINT = /$(NAMESPACE)
DEPS_VOLUME = $(NAMESPACE)_deps
DEPS_MOUNT_POINT = $(ROOT_MOUNT_POINT)/deps
REL_VOLUME = $(NAMESPACE)_rel
REL_MOUNT_POINT = $(ROOT_MOUNT_POINT)/_rel
EBIN_VOLUME = $(NAMESPACE)_ebin
EBIN_MOUNT_POINT = $(ROOT_MOUNT_POINT)/ebin
EDOCKER_REPO = https://github.com/peffis/edocker.mk.git
BINARIES_TO_INCLUDE ?=
EXTRA_PPAS ?=
SOURCES_LIST_APPEND ?=
EXTRA_PACKAGES ?=
ARTEFACT_VOLUMES = $(DEPS_VOLUME) $(REL_VOLUME) $(EBIN_VOLUME)

define log_msg
	@printf "\033[1;37m===> "$(1)"\033[0m\n"
endef


volumes: $(ROOT_VOLUME) artefact_volumes


$(ROOT_VOLUME):
	$(call log_msg,"checking "$(ROOT_VOLUME)" volume")
	@if [ `$(DOCKER) volume inspect $(ROOT_VOLUME) 2> /dev/null | head -1` = "[]" ]; then \
		printf "created " && $(DOCKER) volume create $(ROOT_VOLUME); \
	fi
	@$(DOCKER) run --name tmp_builder -v $(ROOT_VOLUME):$(ROOT_MOUNT_POINT) bravissimolabs/alpine-git echo > /dev/null 2>&1
	@$(DOCKER) cp . tmp_builder:$(ROOT_MOUNT_POINT) > /dev/null 2>&1
	@$(DOCKER) stop tmp_builder > /dev/null 2>&1
	@$(DOCKER) rm tmp_builder > /dev/null 2>&1


artefact_volumes:
	@$(foreach v,$(ARTEFACT_VOLUMES), \
		printf "\033[1;37m===> checking ${v} volume\033[0m\n"; \
		if [ `$(DOCKER) volume inspect $(v) 2> /dev/null | head -1` = "[]" ]; then \
			printf "created " && $(DOCKER) volume create $(v); \
		fi;)


linux_release_build_machine: volumes
	$(call log_msg,"checking linux build machine")
	@if [ `$(DOCKER) images -q $(LRM) 2> /dev/null`"abc" = "abc" ]; then \
		echo "rebuilding release machine"; \
		echo "cloning edocker repo"; \
		mkdir .tmp_context; \
		$(DOCKER) run --rm -v $(ROOT_VOLUME):$(ROOT_MOUNT_POINT) bravissimolabs/alpine-git \
			git clone --verbose --progress -b using_volumes $(EDOCKER_REPO) $(ROOT_MOUNT_POINT)/.edocker; \
		echo "copying Dockerfile.builder"; \
		$(DOCKER) run --rm -v $(ROOT_VOLUME):$(ROOT_MOUNT_POINT) bravissimolabs/alpine-git \
			cat $(ROOT_MOUNT_POINT)/.edocker/builder/Dockerfile.builder > .tmp_context/Dockerfile.builder; \
		echo "building docker image"; \
		$(DOCKER) build --build-arg SOURCES_LIST_APPEND="\"${SOURCES_LIST_APPEND}\"" --build-arg EXTRA_PPAS="${EXTRA_PPAS}" --build-arg EXTRA_PACKAGES="${EXTRA_PACKAGES}" -t $(LRM) -f .tmp_context/Dockerfile.builder .tmp_context; \
		rm -rf .tmp_context; \
	fi


linux_release: linux_release_build_machine
	$(call log_msg,"making linux release")

	$(eval RELEASE_NAME := $(shell $(DOCKER) run --rm \
		-v $(ROOT_VOLUME):$(ROOT_MOUNT_POINT) \
		erlang $(ROOT_MOUNT_POINT)/.edocker/bin/release_name))

	$(eval ERTS_VERSION := $(shell $(DOCKER) run --rm \
		-v $(ROOT_VOLUME):$(ROOT_MOUNT_POINT) \
		erlang $(ROOT_MOUNT_POINT)/.edocker/bin/system_version))

	$(foreach binary,$(BINARIES_TO_INCLUDE),\
		$(DOCKER) run --rm \
			-v $(ROOT_VOLUME):$(ROOT_MOUNT_POINT) \
			-v $(REL_VOLUME):$(REL_MOUNT_POINT) \
			$(LRM) bash -c "cp \`which ${binary}\` ${REL_MOUNT_POINT}/${RELEASE_NAME}/bin/";)

	@$(DOCKER) run  --rm \
			-v $(ROOT_VOLUME):/$(RELEASE_NAME) \
			-v $(REL_VOLUME):/$(RELEASE_NAME)/_rel \
			-v $(DEPS_VOLUME):/$(RELEASE_NAME)/deps \
			-v $(EBIN_VOLUME):/$(RELEASE_NAME)/ebin \
			$(LRM) bash -c "cd /${RELEASE_NAME} && make && .edocker/bin/mkimage ${BINARIES_TO_INCLUDE}"

	@$(DOCKER) run  --rm \
{'fraction_non_alphanumeric': 0.18457300275482094, 'fraction_numerical': 0.010149340292880962, 'mean_word_length': 4.700826446280992, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 33, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '699916', 'n_tokens_mistral': 3032, 'n_tokens_neox': 2820, 'n_words': 522}