PROJECT=scmxx
ACCONFIGHEADERSOUT=$(shell grep ^AC_CONFIG_HEADER configure.in | cut -f 2 -d'[' | cut -f 1 -d']')
ACCONFIGHEADERSIN=$(patsubst  %, %.in, $(ACCONFIGHEADERSOUT))
ACCONFIGFILESOUT=$(shell grep ^AC_CONFIG_FILES configure.in | cut -f 2 -d'(' | cut -f 1 -d')')
ACCONFIGFILESIN=$(patsubst  %, %.in, $(ACCONFIGHEADERSOUT))
AHCONFIGFILE=$(shell echo $(ACCONFIGHEADERSIN) | cut -f 1 -d " ")

.PHONY: all
all: configure $(AHCONFIGFILE) $(PROJECT).spec winsetup-cygwin.iss winsetup-mingw.iss

aclocal.m4:
	aclocal --force

configure: configure.in acsite.m4
	autoconf -Wall
	@rm -rf autom4te.cache

config.status: configure config.guess config.sub install-sh $(AHCONFIGFILE)
	./configure --no-create $(CONFIG_OPTS)

$(AHCONFIGFILE): configure.in acsite.m4
	autoheader -Wall
	@touch $@
	@rm -rf autom4te.cache

.INTERMEDIATE: version.txt
version.txt: configure
	echo -n $(shell ./configure --version | grep ^$(PROJECT) | cut -f 3 -d " ") >$@

$(PROJECT).spec: version.txt $(PROJECT).spec.in
	sed -e 's/^Version:.*$$/Version:     $(shell cat version.txt)/' $@.in >$@

%.iss: version.txt %.iss.in
	sed -e 's/^AppVerName.*$$/AppVerName=SCMxx $(shell cat version.txt)/' \
	    -e 's/^OutputBaseFilename=.*$$/OutputBaseFilename=scmxx-$(shell cat version.txt)-win32-setup/' \
	    $@.in >$@

.PHONY: run-config.status
run-config.status: config.status $(ACCONFIGHEADERSIN) $(ACCONFIGFILESIN)
	./config.status

$(ACCONFIGFILESOUT): config.status
	./config.status

.PHONY: clean
clean: Makefile
	-$(MAKE) clean
	-rm -f *~
	-rm -rf autom4te.cache config.cache config.log config.status
	-rm -f $(ACCONFIGHEADERSOUT)
	-rm -f $(ACCONFIGFILESOUT)

.PHONY: distclean
distclean: Makefile
	-$(MAKE) distclean
	-rm -f *~
	-rm -rf autom4te.cache config.cache config.log config.status
	-rm -f $(ACCONFIGHEADERSOUT)
	-rm -f $(ACCONFIGFILESOUT)
	-rm -f configure $(AHCONFIGFILE) $(PROJECT).spec
