# Copyright (c) 2009, Jens Peter Secher <jpsecher@gmail.com>
#
# 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.

source := $(wildcard *.hx) Makefile

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

helpers := macro-expand unittests

manpages := $(binaries:=.1)

all: $(binaries) $(manpages)

mercurial-buildpackage: $(source)
	haxe -neko $@.n -main MercurialBuildPackage -cmd 'nekotools boot $@.n'

mercurial-pristinetar: $(source)
	haxe -neko $@.n -main MercurialPristineTar -cmd 'nekotools boot $@.n'

mercurial-tagversion: $(source)
	haxe -neko $@.n -main MercurialTagVersion -cmd 'nekotools boot $@.n'

mercurial-importorig: $(source)
	haxe -neko $@.n -main MercurialImportOrig -cmd 'nekotools boot $@.n'

mercurial-importdsc: $(source)
	haxe -neko $@.n -main MercurialImportDsc -cmd 'nekotools boot $@.n'

mercurial-port: $(source)
	haxe -neko $@.n -main MercurialPort -cmd 'nekotools boot $@.n'

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

macro-expand: $(source)
	haxe -neko $@.n -main MacroExpand -cmd 'nekotools boot $@.n'

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)

.PHONY: clean check all install

