TARBALL=$(shell realpath ../../build-root/clixon-latest.tar.xz)
BASENAME=$(shell basename $(TARBALL) | sed -e s/.tar.\*//)
VERSION=$(shell echo $(BASENAME) | cut -f2 -d-)
RELEASE=$(shell echo $(BASENAME) | cut -f3- -d- | sed -e s/-/_/g)
BR=$(shell realpath $(CURDIR)/../../build-root)
RPMBUILD=$(BR)/rpmbuild

all: RPM

spec:
	@echo $(TARBALL)
	mkdir -p $(RPMBUILD)/{RPMS,SRPMS,BUILD,SOURCES,SPECS}
	cp $(TARBALL) $(RPMBUILD)/SOURCES/clixon-$(VERSION)-$(RELEASE).tar.xz
	cp clixon.spec $(RPMBUILD)/SPECS

srpm: spec
	rpmbuild -bs \
	  --define "cligen_prefix @CLIGEN_PREFIX@" \
	  --define "_topdir $(RPMBUILD)" \
	  --define "_version $(VERSION)" \
	  --define "_release $(RELEASE)" \
	  $(RPMBUILD)/SPECS/clixon.spec
	mv $$(find $(RPMBUILD)/SRPMS -name \*.src.rpm -type f) $(BR)

# Define DEVELOPER environmrnt variable to prevent .spec to add cligent to the
# list of build requirements
RPM: spec
	rpmbuild -bb \
	  --define "cligen_prefix @CLIGEN_PREFIX@" \
	  $${DEVELOPER:+--define "developer yes"} \
	  --define "_topdir $(RPMBUILD)" \
	  --define "_version $(VERSION)" \
	  --define "_release $(RELEASE)" \
	  $(RPMBUILD)/SPECS/clixon.spec
	mv $$(find $(RPMBUILD)/RPMS -name \*.rpm -type f) $(BR)
