PYTHON := /usr/bin/python3
VERSION := $(strip $(shell $(PYTHON) setup.py -V))
VERSION_LAST := $(strip $(shell cat VERSION 2>/dev/null || :))

default:
	@echo "Main target is 'dist'"
	@echo "VERSION = $(VERSION)"

check-mypy:
	$(PYTHON) -m mypy arcnagios plugins/check_arcrest_info

sdist: VERSION
	$(PYTHON) setup.py $@

TEMPLATED = nordugrid-arc-nagios-plugins.spec

$(TEMPLATED): %: %.in VERSION
	@VERSION=$(VERSION); \
	PREVERSION=`echo -n $(VERSION) | sed s/^[0-9.]*//`; \
	RPMRELEASE=$${PREVERSION:+0.}$${PREVERSION:-1}; \
	BASEVERSION=$${VERSION%%[a-z]*}; \
	echo "Updating $@ to version $(VERSION)."; \
	sed -e "s;@VERSION@;$(VERSION);g" \
	    -e "s;@BASEVERSION@;$$BASEVERSION;g" \
	    -e "s;@PREVERSION@;$$PREVERSION;g" \
	    -e "s;@RPMRELEASE@;$$RPMRELEASE;g" \
	    < $< > $@.new && mv -f $@.new $@

dist: $(TEMPLATED) sdist
	test -d dist && find dist -type f -name \*tar.gz -exec cp -p '{}' $(CURDIR) \;

rpm: dist
	rpmbuild -tb nordugrid-arc-nagios-plugins-$(VERSION).tar.gz

ifneq ($(VERSION),$(VERSION_LAST))
VERSION: always
ifeq ($(VERSION_LAST),)
	@echo 'Creating VERSION with content $(VERSION).'
else
	@echo 'Replacing VERSION content from $(VERSION_LAST) to $(VERSION).'
endif
	@echo >$@ $(VERSION)
always:
endif

.PHONY: dist rpm always
