
srcdir = @srcdir@
VPATH  = @srcdir@

include ./config.mk

distdir = $(PACKAGE)-$(VERSION)
ALL_FRONTENDS = @ALL_FRONTENDS@

FRONTENDS = @FRONTENDS@
SUBDIRS = @INTLSUB@ support lib $(FRONTENDS)
MDVIMAPDIR = @MDVIMAPDIR@
MKTEXLSR = @MKTEXLSR@
POST_INSTALL = @POST_INSTALL@
POST_UNINSTALL = $(POST_INSTALL)

OTHERFILES = README TODO COPYING LICENSE NOTES BUGS \
	ABOUT-NLS WARNING Changes

ACFILES    = Makefile.in configure.in config.h.in version.h.in \
	configure config

DISTFILES = $(ACFILES) $(OTHERFILES) mdvi.conf data
DISTDIRS  = $(ALL_FRONTENDS) support lib intl

.PHONY: all $(SUBDIRS) $(ALL_FRONTENDS)
all: $(SUBDIRS)

$(SUBDIRS):
	if test -d "$@"; then $(MAKE) -C $@; else :; fi
$(ALL_FRONTENDS): @INTLSUB@ support lib

.PHONY: _dummy
_dummy $(INTLDEPS):
	$(MAKE) -C intl all

.PHONY: install-data install-bin install-recurse install
install: all install-data install-bin install-recurse
	$(POST_INSTALL)
install-bin:
install-data:
	$(MKINSTALLDIRS) $(MDVIMAPDIR)
	$(INSTALL_DATA) $(srcdir)/mdvi.conf $(MDVIMAPDIR)/mdvi.conf
	@cd $(srcdir)/data; for i in *; do \
	  echo "$(INSTALL_DATA) $$i $(MDVIMAPDIR)/$$i"; \
	  $(INSTALL_DATA) $$i $(MDVIMAPDIR)/$$i; \
	done	
install-recurse:
	@list="$(SUBDIRS)"; for i in $$list; do \
	  if test -d "$$i"; then $(MAKE) -C $$i install; else :; fi; \
	done

.PHONY: uninstall uninstall-data uninstall-bin uninstall-recurse uninstall
uninstall: uninstall-recurse uninstall-bin uninstall-data
	$(POST_UNINSTALL)
uninstall-bin:
uninstall-data:
	@cd data; for i in *; do \
	  $(RM) $(MDVIMAPDIR)/$$i; \
	done
	$(RM) $(MDVIMAPDIR)/mdvi.conf
	-rmdir $(MDVIMAPDIR)
uninstall-recurse:
	@list="$(SUBDIRS)"; for i in $$list; do \
	  if test -d "$$i"; then $(MAKE) -C $$i uninstall; else :; fi; \
	done

.PHONY: dist
dist: distdir $(distdir)/MD5SUMS
	@echo "Creating tarball $(distdir).tar.gz..."
	$(TAR) zcfoh $(distdir).tar.gz $(distdir) || exit 1
	cp $(distdir)/MD5SUMS $(distdir).sum
	rm -rf $(distdir)

# Creates a tarball, unpacks it, configures it, builds it in a VPATH
# configuration, installs it, and cleans up if everything went well.
# Otherwise, the output generated when building is saevd in the file
# distcheck.log for post-mortem examination.
#
CONFOPTS = \
	--with-type1-fonts \
	--with-truetype-fonts \
	--with-freetype-headers=/usr/local/include/freetype1

.PHONY: distcheck
distcheck: dist
	@echo "Checking $(distdir).tar.gz for completeness"
	$(TAR) zxf $(distdir).tar.gz || exit 1
	-@top=`pwd`; rm -rf distcheck; mkdir distcheck; \
	cd distcheck; inst=`pwd`/INSTALL=; mkdir BUILD=; cd BUILD=; \
	rm -f $$top/distcheck.log; \
	echo "Testing distribution (building output saved in \`distcheck.log')"; \
	echo "running ../../$(distdir)/configure $(CONFOPTS) \
	--prefix=$$inst" --with-texmf="$$inst/texmf"; \
	../../$(distdir)/configure $(CONFOPTS) \
	  --prefix=$$inst --with-texmf="$$inst/texmf" \
	>>$$top/distcheck.log 2>&1 || exit 1; \
	echo "running $(MAKE) all"; \
	$(MAKE) all >>$$top/distcheck.log 2>&1 || exit 1; \
	echo "running $(MAKE) install"; \
	$(MAKE) install >>$$top/distcheck.log 2>&1 || exit 1; \
	cd .. && echo success > SUCCESS
	@if test -f distcheck/SUCCESS; then \
	  echo "$(distdir) ready for distribution"; \
	else \
	  echo "$(distdir) does not build and install yet -- check log"; \
	fi
	rm -rf distcheck $(distdir)

.PHONY: snapshot
snapshot:
	$(MAKE) distdir=$(PACKAGE)-`date +"%Y%m%d"` dist

include $(FRAGDIR)/dist.mk

CLEANABLE += core
DISTCLEANABLE += config.mk nls.mk config.log config.cache \
	config.status config.h version.h Makefile

include $(FRAGDIR)/targets.mk
