# Mooix uses symlinks for object references. CVS of course doesn't like
# symlinks (neither does subversion at the time of this writing). To deal
# with that, there is a program that converts foo.lnk files into symlinks.
# The contents of the file becomes the destination of the link, although it
# will be adjusted to point to an absolute path when mooix is installed.

# Recurse into any directories with Makefiles.
SUBDIRS=$(shell find . -name Makefile -mindepth 2 -exec dirname {} \;)

-include ../makeinfo
.PHONY: ../makeinfo

build: links
	# Set perms to non-group-writable by default.
	chmod -R g-w .

	set -e ; $(foreach DIR,$(SUBDIRS), $(MAKE) -C $(DIR) $@;)

# Catch common mistakes, not really regression testing.
test: build
	@set -e; \
	FAIL=0; \
	for dir in $$(find . -type d | grep -v /.svn | grep -v /t\$$ |grep -v /t/ | grep -v CVS); do \
		if [ ! -e $$dir/.mooix ]; then \
			echo "** No .mooix file in $$dir" >&2; \
			FAIL=1; \
		elif [ $$dir != ./concrete/thing -a ! -L $$dir/parent ]; then \
			echo "** No parent link in $$dir" >&2; \
			FAIL=1; \
		fi; \
	done; \
	exit $$FAIL

# Runs the full test suite. Mooix must already be installed.
fulltest:
	@$(MAKE) --quiet links
	sudo ../utils/fulltest

links:
	# Generate symlinks from .lnk files.
	set -e; \
	for lnk in $$(find . -type f -name \*.lnk); do \
		file=$$(echo $$lnk | sed s/.lnk//); \
		rm -f $$file; \
		ln -sf $$(cat $$lnk) $$file; \
	done

clean:
	find . -type l -exec rm -f {} \;
	find . -name \*~ -exec rm -f {} \;
	set -e ; $(foreach DIR,$(SUBDIRS), $(MAKE) -C $(DIR) $@;)
	rm -f tmp-log

realclean:

install:
	$(INSTALL) -d $(PREFIX)/$(libdir) $(PREFIX)/$(localstatedir)/lib
	# cp can get confused about hard links on reinstalls,
	# so remove the whole old tree first
	rm -rf $(PREFIX)/$(libdir)/mooix
	mkdir $(PREFIX)/$(libdir)/mooix
	tar cf - . --exclude t -X Excludes | \
		(cd $(PREFIX)/$(libdir)/mooix; tar xpf -)
	PERL5LIB=../bindings/perl/ \
	sh -ex ../utils/splittree $(PREFIX)/$(libdir)/mooix \
		$(PREFIX)/$(localstatedir)/lib/mooix \
		$(libdir)/mooix $(localstatedir)/lib/mooix 2>tmp-log || \
		( echo splittree failed; cat tmp-log; false ) >&2

uninstall:
	rm -rf $(PREFIX)/$(libdir)/mooix
	rm -rf $(PREFIX)/$(localstatedir)/lib/mooix
	-rmdir -p $(PREFIX)/$(libdir) $(PREFIX)/$(localstatedir)/lib \
		$(PREFIX)/$(localstatedir)/lib

%:
	set -e ; $(foreach DIR,$(SUBDIRS),$(MAKE) -C $(DIR) $@;)

.PHONY: $(SUBDIRS) build
