#
# -- Makefile for shell scripts which include their own version
#    number and need to be built as RPMs.
#
.SUFFIXES:
.PHONY: clean mrclean distclean all rpm tarball update specfile

PACKAGE = $(shell cat PACKAGE)
VERSION = $(shell cat VERSION)
TARBALL = $(PACKAGE)-$(VERSION).tar.gz

default: rpm

rpm: specfile tarball
	rpmbuild \
	  --define    "_rpmdir ./build/" \
	  --define "_sourcedir ./build/" \
	  --define "_srcrpmdir ./build/" \
	  -ta build/$(TARBALL)

tarball: builddir README VERSION
	git tar-tree HEAD $(PACKAGE)-$(VERSION) \
	  | gzip -c \
	  > build/$(TARBALL)

builddir:
	mkdir -p build

clean:
	rm -rf build/*

specfile: $(PACKAGE).spec

$(PACKAGE).spec: VERSION $(PACKAGE).spec.in
	@sed -e "s|@VERSION@|$(VERSION)|" $(PACKAGE).spec.in > $@

update: $(PACKAGE).spec README VERSION $(PACKAGE)

ChangeLog: $(PACKAGE)
	@sed -ne '/# -- ChangeLog/,/^$$/p' $(PACKAGE) > $@

README: $(PACKAGE)
	@./$(PACKAGE) --long-usage > $@

VERSION: $(PACKAGE)
	@awk -F= '/^VERSION=/{ print $$2 }' $(PACKAGE) > $@

mrclean: clean
distclean: clean
