#-*-makefile-*-

LTSUTILS = python $(TOPLVL)/projects/common/lts/main.py $(PKG_NAME)

#help lts-show: Display a summary of active LTS branches.
lts-show:
	@$(LTSUTILS) sanity-check
	@while read b; do \
		if git show-ref $$b > /dev/null; then \
			echo $$b $$(git log --oneline -1 $$b); \
		else \
			echo $$b Not found; \
		fi; \
	done < $(TOPLVL)/projects/common/lts/active-branches

#help lts-backport: Fast-forward the previous active branch to the current
#help branch.
lts-backport:
	@$(LTSUTILS) sanity-check
	@newer=$$(git symbolic-ref HEAD); \
	newer=$${newer#refs/heads/}; \
	if ! $(LTSUTILS) prev-branch --checkout; then \
		echo Could not check out previous active branch.; \
		exit 0; \
	fi; \
	if $(LTSUTILS) is-same-version $$newer; then \
		$(LTSUTILS) fast-forward $$newer; \
	else \
		echo Most likely a patch needs to be manually re-applied for this version.; \
		echo Alternatively, use \"git merge --ff-only $$newer\" to upgrade the package version.; \
	fi

#help lts-build: Build RPM in Koji, or reuse existing build from older LTS
#help branch.
lts-build:
	@$(LTSUTILS) sanity-check
	@tag=$$(git tag --points-at); \
	if [[ -z $$tag ]]; then \
		echo This commit does not have a tag.; \
		echo Proceeding to run \"make lts-koji\".; \
		$(LTSUTILS) prompt || exit; \
		$(MAKE) lts-koji; \
		exit; \
	fi; \
	older=$$($(LTSUTILS) prev-branch); \
	if [[ -z $$older ]]; then \
		echo Could not determine previous active branch.; \
		exit 1; \
	fi; \
	current=$$($(LTSUTILS) current-branch); \
	if [[ $$(git rev-parse $$older) != $$(git rev-parse $$current) ]]; then \
		echo Previous branch and current branch have different commits.; \
		for b in $$older $$current; do echo $$b $$(git log --oneline -1 $$b); done; \
		echo Proceeding to run \"make lts-koji\".; \
		$(LTSUTILS) prompt || exit; \
		$(MAKE) lts-koji; \
		exit; \
	fi; \
	if $(LTSUTILS) can-reuse-binary $$older; then \
		echo Reusing binary from $$older.; \
		$(KOJI_CMD) tag-build $$current $(SRPMVERS); \
	else \
		echo Could not reuse binary from $$older.; \
		echo Proceeding to run \"make bump lts-koji\".; \
		$(LTSUTILS) prompt || exit; \
		$(MAKE) bump lts-koji; \
	fi
	@$(LTSUTILS) next-branch --checkout

#help lts-koji: Same as "koji" but for working on LTS branches. Should not
#help be called directly, use lts-build instead.
lts-koji: prekoji-checks kojidef
	@$(LTSUTILS) sanity-check
	@$(MAKE) spdxcheck
	@$(MAKE) checkblacklist
	@current=$$($(LTSUTILS) current-branch); \
	if ! grep $$current $(TOPLVL)/projects/common/lts/active-branches; then \
		echo "Error: Must be on an active branch to submit to koji" >&2; \
		exit 1; \
	fi; \
	if ! git diff --quiet HEAD ${SPECFILE}; then \
		echo "Error: All changes to ${SPECFILE} must be committed first" >&2; \
		exit 1; \
	fi; \
	if git rev-parse --verify --quiet origin/$$current > /dev/null; then \
		git pull --rebase; \
	fi
	git tag $(SRPMVERS)-lts
	git push origin $$($(LTSUTILS) current-branch) refs/tags/$(SRPMVERS)-lts
	$(KOJI_CMD) build $$KOJI_NOWAIT $$($(LTSUTILS) current-branch) $(PKG_BASE_URL)/$(PKG_NAME)?#$(SRPMVERS)-lts
