VPATH=@VPATH@
VERSION=@VERSION@
DESTDIR?=
INSTALL_SCRIPT?=@INSTALL_SCRIPT@
INSTALL_DATA?=@INSTALL_DATA@
INSTALL_MAN?=@INSTALL_DATA@
BINDIR=${DESTDIR}/@BINDIR@
MANDIR=${DESTDIR}/@MANDIR@
DATADIR=${DESTDIR}/@DATADIR@
VERSION=@VERSION@
.PHONY: all todo test install deinstall dist clean mrproper

all:
	@echo void build

todo:
	cat TODO
	cat src/*.orig | grep -e TODO -e XXX

test:
	cd tests/ && sh test.sh

install:
	mkdir -p "$(BINDIR)"
	sed -e 's,@''VERSION@,$(VERSION),g' "$(VPATH)/src/acr" > "$(BINDIR)/acr"
	chmod +x "$(BINDIR)/acr"
	sed -e 's,@''VERSION@,$(VERSION),g' "$(VPATH)/src/acr-cat" > "$(BINDIR)/acr-cat"
	chmod +x "$(BINDIR)/acr-cat"
	sed -e 's,@''VERSION@,$(VERSION),g' "$(VPATH)/src/acr-sh" > "$(BINDIR)/acr-sh"
	chmod +x "$(BINDIR)/acr-sh"
	$(INSTALL_SCRIPT) "$(VPATH)/src/amr" "$(BINDIR)/amr"
	$(INSTALL_SCRIPT) "$(VPATH)/src/acr-wrap" "$(BINDIR)/acr-wrap"
	$(INSTALL_SCRIPT) "$(VPATH)/src/acr-install" "$(BINDIR)/acr-install"
	mkdir -p "$(MANDIR)/man1/" "$(MANDIR)/man5/"
	${INSTALL_MAN} "$(VPATH)/man/man1/acr.1" "$(MANDIR)/man1/"
	${INSTALL_MAN} "$(VPATH)/man/man1/acr-wrap.1" "$(MANDIR)/man1/"
	${INSTALL_MAN} "$(VPATH)/man/man1/amr.1" "$(MANDIR)/man1/"
	${INSTALL_MAN} "$(VPATH)/man/man1/acr-cat.1" "$(MANDIR)/man1/"
	#${INSTALL_MAN} "${VPATH}/man/man1/install-acr.1 $(MANDIR)/man1/"
	${INSTALL_MAN} "$(VPATH)/man/man5/configure.acr.5" "$(MANDIR)/man5/"
	${INSTALL_MAN} "$(VPATH)/man/man5/configure.amr.5" "$(MANDIR)/man5/"
	mkdir -p "$(DATADIR)/doc/acr/examples"
	for A in `find "$(VPATH)/examples" -maxdepth 1 -type f`; do \
		$(INSTALL_DATA) "$${A}" "$(DATADIR)/doc/acr/examples" ; done
	mkdir -p "$(DATADIR)/doc/acr/developers"
	for A in `find "$(VPATH)/doc" -maxdepth 1 -type f`; do \
		$(INSTALL_DATA) "$${A}" "$(DATADIR)/doc/acr" ; done
	for A in `find "$(VPATH)/doc/developers" -maxdepth 1 -type f`; do \
		$(INSTALL_DATA) "$${A}" "$(DATADIR)/doc/acr/developers" ; done
	mkdir -p "$(DATADIR)/doc/acr/vim/ftplugin"
	mkdir -p "$(DATADIR)/doc/acr/vim/syntax"
	$(INSTALL_SCRIPT) "$(VPATH)/src/vim/install.sh" "$(DATADIR)/doc/acr/vim/install.sh"
	$(INSTALL_DATA) "$(VPATH)/src/vim/ftplugin/acr.vim" "$(DATADIR)/doc/acr/vim/ftplugin/acr.vim"
	$(INSTALL_DATA) "$(VPATH)/src/vim/syntax/acr.vim" "$(DATADIR)/doc/acr/vim/syntax/acr.vim"
	$(INSTALL_DATA) "$(VPATH)/src/vim/vimrc" "$(DATADIR)/doc/acr/vim/vimrc"
	mkdir -p "$(DATADIR)/acr/modules/"
	-$(INSTALL_DATA) "$(VPATH)/modules"/* "$(DATADIR)/acr/modules/"

uninstall deinstall:
	-rm -f $(BINDIR)/acr-sh $(BINDIR)/acr $(BINDIR)/amr
	-rm -f $(BINDIR)/acr-cat $(BINDIR)/acr-install
	-rm -f $(BINDIR)/acr-wrap
	-rm -f $(MANDIR)/man1/acr.1 $(MANDIR)/man5/configure.acr.5
	-rm -f $(MANDIR)/man1/acr-cat.1 $(MANDIR)/man1/amr.1
	-rm -f $(MANDIR)/man1/acr-wrap.1
	-rm -f $(MANDIR)/man5/configure.amr.5
	-rm -rf $(DATADIR)/doc/acr/examples $(DATADIR)/doc/acr
	-rm -rf $(DATADIR)/acr

dist:
	rm -rf acr-$(VERSION)
	git clone . acr-$(VERSION)
	rm -rf acr-$(VERSION)/.git
	tar czvf acr-$(VERSION).tar.gz acr-$(VERSION)

clean:
	rm -f src/vim/install.sh
	rm -f Makefile

mrproper: clean
	rm -f configure
