# Top-level Makefile for XR
# -------------------------

VER	   ?= 2.81
PREFIX	   ?= $(DESTDIR)/usr
BINDIR	   ?= $(PREFIX)/sbin
MANDIR	   ?= $(PREFIX)/share/man
TAR	   ?= /tmp/crossroads-$(VER).tar.gz
AUTHOR     ?= Karel Kubat <karel@kubat.nl>
MAINTAINER ?= Karel Kubat <karel@kubat.nl>
DISTSITE   ?= http://crossroads.e-tunity.com
BASE	   ?= $(shell pwd)

foo:
	@cat xr/etc/make.help

local:
	mkdir -p xr/build
	xr/etc/gettools /usr/local/bin xr/etc c-conf e-ver
	xr/etc/e-ver ChangeLog $(VER)
	BASE=$(BASE) AUTHOR='$(AUTHOR)' MAINTAINER='$(MAINTAINER)' \
	  DISTSITE='$(DISTSITE)' MEMDEBUG=$(MEMDEBUG)\
	  VER='$(VER)' PROF=$(PROF) PROFILER=$(PROFILER) $(MAKE) -C xr

localprof:
	PROF=-pg PROFILER=-DPROFILER make local

localmem:
	MEMDEBUG=-DMEMDEBUG make local

install: local $(BINDIR)/xrctl install-manpages
	mkdir -p $(BINDIR)
	BASE=$(BASE) AUTHOR='$(AUTHOR)' MAINTAINER='$(MAINTAINER)' \
	  DISTSITE='$(DISTSITE)' \
	  VER='$(VER)' BINDIR=$(BINDIR) $(MAKE) -C xr install
	@echo
	@echo '  The balancer program xr is now installed to $(BINDIR).'
	@echo '  The control script xrctl is installed there too. In order to'
	@echo '  use it, you will have to create /etc/xrctl.xml (if you have'
	@echo '  not done so yet). See "man xrctl.xml" for an example.'
	@echo 
	@echo '  Have fun with Crossroads $(VER),'
	@echo '    -- $(MAINTAINER)'
	@echo
$(BINDIR)/xrctl: xrctl/xrctl Makefile
	sed 's:__VER__:$(VER):' < xrctl/xrctl > $(BINDIR)/xrctl
	chmod +x $(BINDIR)/xrctl
install-manpages: $(MANDIR)/man1/xr.1 $(MANDIR)/man1/xrctl.1 \
		  $(MANDIR)/man5/xrctl.xml.5
$(MANDIR)/man1/xr.1: doc/xr.1
	mkdir -p $(MANDIR)/man1
	cp $< $@
$(MANDIR)/man1/xrctl.1: doc/xrctl.1
	mkdir -p $(MANDIR)/man1
	cp $< $@
$(MANDIR)/man5/xrctl.xml.5: doc/xrctl.xml.5
	mkdir -p $(MANDIR)/man5
	cp $< $@

uninstall:
	rm -f $(BINDIR)/xr $(BINDIR)/xrctl
	@echo
	@echo 'The balancer binary xr and the control script xrctl have been'
	@echo 'removed from $(BINDIR).'
	@echo
	@if [ -f /etc/xrctl.xml ] ; then \
	  echo 'The configuration /etc/xrctl.xml still exists. Remove this' ; \
	  echo 'by hand if you are sure you will not be needing it.'; \
	else \
	  echo 'Configuration /etc/xrctl.xml was not found. Maybe you have'; \
	  echo 'it in a different location or under a different name.'; \
	  echo 'If so, consider removing it by hand.'; \
	fi;
	@echo
	@echo 'XR was uninstalled!'

clean:
	rm -rf xr/build/*
	find . -name gmon.out -exec rm {} \;

tar: clean
	rm -rf $(TAR) /tmp/crossroads-$(VER)
	cd ..; cp -r crossroads /tmp/crossroads-$(VER)
	cd /tmp; tar czf $(TAR) \
	  --exclude .git --exclude .svn --exclude crossroads-$(VER)/xr/build \
	  crossroads-$(VER)
	rm -rf /tmp/crossroads-$(VER)
	@echo
	@echo 'Sources now tarred into $(TAR)'

commit: local clean
	test `svn status | grep '^\?' | wc -l` -eq 0 || \
		(echo 'SVN not fully up to date: run "svn status"' && exit 1)
	test -f doc/xr.pdf || \
		(echo 'PDF version of the docs not present!' && exit 1)
	test doc/xr.pdf -nt doc/xr.odt || \
		(echo 'PDF version of the docs is stale!!' && exit 1)
	perl -c xrctl/xrctl
	svn -m $(VER) commit
