# Targets include:
#   clean
#   realclean
#   install
#   build
#   test
#   uninstall

# Order is important: utils needs libmooproxy and bindings, and 
# obj needs bindings. mood needs libmooproxy
LIBDIRS=libmooproxy libmoocallstack
PROGDIRS=mood utils
MOODIRS=obj doc benchmarks
ALLDIRS=$(LIBDIRS) bindings $(PROGDIRS) $(MOODIRS)

include makeinfo

build: build-moo
	# These links are provided for non-debian folks.
	ln -sf debian/changelog CHANGES
	ln -sf debian/copyright COPYRIGHT

build-libs:
	set -e ; $(foreach DIR,$(LIBDIRS),$(MAKE) -C $(DIR);)

build-bindings: build-libs
	$(MAKE) -C bindings

build-progs: build-libs build-bindings
	set -e ; $(foreach DIR,$(PROGDIRS),$(MAKE) -C $(DIR);)

build-moo: build-progs
	set -e ; $(foreach DIR,$(MOODIRS),$(MAKE) -C $(DIR);)

install: build
	set -e ; $(foreach DIR,$(ALLDIRS),$(MAKE) -C $(DIR) $@;)
	# Install mooix.conf, expanding a few make variables in it too.
	# The make variables work when it is sourced by various Makefiles
	# at build time, but it they won't do at runtime.
	install -d $(PREFIX)/$(sysconfdir)
	sed -e 's,$$(localstatedir),$(localstatedir),' \
	    -e 's,$$(libdir),$(libdir),' \
	    -e 's,$$(bindir),$(bindir),' \
	    -e 's,$$(sbindir),$(sbindir),' \
	    < mooix.conf > $(PREFIX)/$(sysconfdir)/mooix.conf
	# This is a homedir for guest account.
	$(INSTALL) -d $(PREFIX)/$(datadir)/mooix/guesthome
	touch $(PREFIX)/$(datadir)/mooix/guesthome/.hushlogin

uninstall:
	set -e ; $(foreach DIR,$(ALLDIRS),$(MAKE) -C $(DIR) $@;)
	rm -f $(PREFIX)/$(sysconfdir)/mooix.conf
	rm -f $(PREFIX)/$(datadir)/mooix/guesthome/.hushlogin
	-rmdir -p $(PREFIX)/$(sysconfdir) $(PREFIX)/$(datadir)/mooix/guesthome
	@echo "** Uninstallation complete. However, mooix users and groups were not removed. Also, perl and python bindings may not be entirely removed."

realclean: clean
	set -e ; $(foreach DIR,$(ALLDIRS),$(MAKE) -C $(DIR) $@;)
	rm -f makeinfo config.status config.cache config.log

configure: configure.ac
	autoconf

config.status: configure
	./configure

makeinfo: autoconf/makeinfo.in config.status
	./config.status

fulltest:
	$(MAKE) -C obj $@

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

$(ALLDIRS):
	$(MAKE) -C $@ $(ACTION)

.PHONY: $(ALLDIRS) build realclean
