# Copyright (c) 2009, Jens Peter Secher <jpsecher@gmail.com>
# Copyright (c) 2012, Darren Salt <linux@youmustbejoking.demon.co.uk>
#
# Permission to use, copy, modify, and/or distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.


# Set this at make invocation if you want extended version strings.
# For example, VENDOR_VERSION=foo results in "0.10 (foo)".
VENDOR_VERSION ?=


source_in := $(wildcard *.hx.in)
source_generated := $(basename $(source_in))
source := $(wildcard *.hx) $(source_generated) Makefile

binaries := mercurial-buildpackage mercurial-tagversion	\
            mercurial-importorig mercurial-importdsc \
            mercurial-pristinetar mercurial-port

helpers := macro-expand unittests

manpages := $(binaries:=.1)

HAXE = haxe -neko $@.n -cmd 'nekotools boot $@.n' -main


all: $(binaries) $(manpages)

mercurial-buildpackage: $(source)
	$(HAXE) MercurialBuildPackage

mercurial-pristinetar: $(source)
	$(HAXE) MercurialPristineTar

mercurial-tagversion: $(source)
	$(HAXE) MercurialTagVersion

mercurial-importorig: $(source)
	$(HAXE) MercurialImportOrig

mercurial-importdsc: $(source)
	$(HAXE) MercurialImportDsc

mercurial-port: $(source)
	$(HAXE) MercurialPort

testprocess: $(source)
	$(HAXE) TestProcess

macro-expand: $(source)
	$(HAXE) MacroExpand

mercurial-pristinetar.1: MercurialPristineTar.man macro-expand $(source)
	./macro-expand < $< > $@

mercurial-buildpackage.1: MercurialBuildPackage.man macro-expand $(source)
	./macro-expand < $< > $@

mercurial-importorig.1: MercurialImportOrig.man macro-expand $(source)
	./macro-expand < $< > $@

mercurial-importdsc.1: MercurialImportDsc.man macro-expand $(source)
	./macro-expand < $< > $@

mercurial-port.1: MercurialPort.man macro-expand $(source)
	./macro-expand < $< > $@

mercurial-tagversion.1: MercurialTagVersion.man macro-expand $(source)
	./macro-expand < $< > $@

test: $(source)
	haxe -neko $@.n -main Test -cmd 'nekotools boot $@.n'

unittests: $(source)
	haxe -neko $@.n -main UnitTests -cmd 'nekotools boot $@.n'

install: $(binaries) $(manpages)
	install $(binaries) $(DESTDIR)/usr/bin/
	install $(manpages) $(DESTDIR)/usr/share/man/man1

check: unittests
	./$<

clean:
	rm -f *.n $(binaries) $(helpers) $(manpages) $(source_generated)

# Generated sources
Constants.hx: Constants.hx.in
	sed -e 's/@VENDOR@/'"$(VENDOR_VERSION)"'/g' <$< >$@

# Misc
version:
	@echo "$(VERSION)"

.PHONY: clean check all install version
.DELETE_ON_ERROR:
