# Makefile for constructing RPMs.
SHELL := bash
.SHELLFLAGS := -eu -o pipefail -c
NAME = cephadm
DIST ?= el8
BASE := "https://github.com/ceph/ceph"
BRANCH := $(shell git rev-parse --abbrev-ref HEAD | cut -d'-' -f5)
LAST_VERSION ?= $(shell cat SPECS/cephadm.spec.in | awk '/Version/ { print $$2}')
LAST_RELEASE ?= $(shell cat SPECS/cephadm.spec.in | awk '/Release/ {print $$2}' | cut -d'%' -f1)
LOCALHASH := $(shell cat SPECS/cephadm.spec.in | awk '/Source0/ { print $$2}' | cut -d'/' -f7)
SPECTOOL :=$(shell which spectool)
RPMBUILD :=$(shell which rpmbuild)
RELEASE := $(if $(RELEASE),$(RELEASE),$(LAST_RELEASE))
VERSION := $(if $(VERSION),$(VERSION),$(LAST_VERSION))
NVR := $(NAME)-$(VERSION)-$(RELEASE).$(DIST)
all: spec src srpm
pre: ## Get the last remote hash for the specified branch
$(eval REMOTEHASH := $(shell git ls-remote $(BASE) $(BRANCH) | awk '{print $$1}'))
$(eval RELEASE=$(shell echo $$(($(RELEASE)+1))))
echo: pre ## Print the passed (or computed) parameters
@echo PWD $(PWD)/SOURCES
@echo BASE $(BASE)
@echo BRANCH $(BRANCH)
@echo CURRENT VERSION $(LAST_VERSION)
@echo NEXT VERSION $(VERSION)
@echo REMOTE HASH $(REMOTEHASH)
@echo LOCAL HASH $(LOCALHASH)
@echo RELEASE $(RELEASE)
spec: pre ## Patch and build the cephadm spec file
sed SPECS/cephadm.spec.in \
-e "s/$(LOCALHASH)/$(REMOTEHASH)/" \
-e 's/Version: $(LAST_VERSION)/Version: $(VERSION)/' \
-e 's/Release: $(LAST_RELEASE)/Release: $(RELEASE)/' \
> SPECS/cephadm.spec
cp SPECS/cephadm.spec SPECS/cephadm.spec.in
src: ## Download the cephadm sources according to the last hash
$(SPECTOOL) -g SPECS/cephadm.spec -C SOURCES -f
srpm: ## Build the srpm
$(RPMBUILD) -bs $(PWD)/SPECS/cephadm.spec \
--define "_sourcedir $(PWD)/SOURCES" \
--define "_srcrpmdir $(PWD)" \
--define "dist .$(DIST)"
clean: ## Clean the workdir
rm -f *.src.rpm
rm -f SPECS/cephadm.spec
help: ## Print the Help
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' Makefile | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'
.PHONY: all