prefix=@prefix@

TAR=@TAR@
ZIP=@ZIP@
MKDIR_P=mkdir -p
PACKAGE_NAME=@PACKAGE_NAME@
PACKAGE_VERSION=@PACKAGE_VERSION@
DIST=$(PACKAGE_NAME)-$(PACKAGE_VERSION)
RELEASE_DATE=@RELEASE_DATE@
IN_SOURCE_TREE=@IN_SOURCE_TREE@

# Language implementations
LANGS=src/c src/java
# SUBDIRS is LANGS plus the grammar directory, listed first
SUBDIRS=src/buildtools src/grammar $(LANGS)

edit=sed -e 's/@DIST\@/$(DIST)/g' -e 's/@RELEASE_DATE\@/$(RELEASE_DATE)/g'


%.gz: %
	rm -f $@
	gzip --best $<

all:
	if $(IN_SOURCE_TREE); then (cd src/grammar; $(MAKE)); else :; fi
	for d in $(LANGS); do (cd $$d; $(MAKE)); done

dist: $(DIST).tar.gz $(DIST).zip

$(DIST).tar: $(DIST)
	rm -f $@
	$(TAR) cf $@ $(DIST)

$(DIST).zip: $(DIST)
	rm -f $@
	$(ZIP) -r $@ $(DIST)

$(DIST).jar: $(DIST)
	cd src/java; $(MAKE) unity.jar
	ln src/java/unity.jar ./$(DIST).jar

$(DIST): unity.html
	for d in src/grammar $(LANGS); do \
		$(MKDIR_P) $(DIST)/$$d; \
		(cd $$d; $(MAKE) dist); \
		cp -R $$d/dist/* $(DIST)/$$d; \
	done
	cp Makefile.in configure configure.ac Notes README.md LICENCE install-sh unity.html unity.html.in $(DIST)
	cp src/package-info.txt.in $(DIST)/src
# add the README.hacking note;
# this is also used as the marker of a post-distribution tree, in configure.ac
	cp src/dist-note.txt $(DIST)/src/README.hacking

webpage-tarball.tar: unity.html $(DIST).tar.gz $(DIST).zip $(DIST).jar README.md RELEASE-NOTES
	rm -f webpage-tarball.tar
	if test -d webpage-tempdir; then rm -Rf webpage-tempdir; else :; fi; mkdir webpage-tempdir
	cp -R $(DIST)/src/c/docs webpage-tempdir/c-docs
	cp -R $(DIST)/src/java/docs webpage-tempdir/javadocs
	D=`pwd`; cd $(DIST)/src/c; $(ZIP) -r $$D/webpage-tempdir/$(DIST)-doxygen.zip docs
	D=`pwd`; cd $(DIST)/src/java; $(ZIP) -r $$D/webpage-tempdir/$(DIST)-javadocs.zip docs
	mkdir webpage-tempdir/grammar
	cp src/grammar/unity-*.txt src/grammar/testcases.csv src/grammar/testcases-*.csv src/grammar/known-units.csv webpage-tempdir/grammar
	$(edit) README.md >webpage-tempdir/README
	cd webpage-tempdir; \
		ln ../unity.html index.html; \
		for f in RELEASE-NOTES; do ln ../$$f $$f; done; \
		$(TAR) cf ../webpage-tarball.tar .
	rm -Rf webpage-tempdir
	@echo "Upload $(DIST).zip and $(DIST).jar to https://bitbucket.org/nxg/unity/downloads"

TAGS:
	etags `find . -name \*.c -o -name \*.java`

unity.html: unity.html.in Makefile
	rm -f unity.html
	$(edit) unity.html.in >unity.html

install:
	for d in $(LANGS); do (cd $$d; $(MAKE) install); done

# Install the unity jar into a local Maven repository as a snapshot
install-mvn:
	cd src/java; $(MAKE) unity.jar
	mvn install:install-file -Dfile=src/java/unity.jar \
		-DgroupId=uk.me.nxg -DartifactId=unity \
		-Dversion=$(PACKAGE_VERSION) -Dpackaging=jar

Makefile pom.xml: config.status \
		Makefile.in \
		src/c/Makefile.in \
		src/c/test/Makefile.in \
		src/java/Makefile.in \
		src/java/test/Makefile.in
	./config.status
config.status: configure
	./config.status --recheck
configure: configure.ac
	autoconf

check: all
	for d in $(LANGS); do (cd $$d; $(MAKE) check); done

clean:
	for d in $(SUBDIRS); do (cd $$d; $(MAKE) clean); done
	rm -Rf $(DIST)* webpage-tarball.tar webpage-tempdir unity.html pom.xml
