#
# ${R_HOME}/Makefile

VPATH = @srcdir@
srcdir = @srcdir@
top_srcdir = @top_srcdir@

top_builddir = .

include $(top_builddir)/Makeconf

distdir = $(PACKAGE)-$(VERSION)
INSTFILES = COPYING COPYRIGHTS FAQ MIRROR-SITES NEWS RESOURCES 
DISTFILES = $(INSTFILES) \
	BUGS INSTALL PLATFORMS PROJECTS README VERSION Y2K \
	Makeconf.in Makefile.in \
	aclocal.m4 config.site configure configure.in configure.mac \
	date-stamp
SUBDIRS = src afm demos doc etc tests tools

GZIP = --best

all: Makefile R docs

Makefile: $(srcdir)/Makefile.in $(top_builddir)/config.status
	cd $(top_builddir) \
	  && CONFIG_FILES=$@ CONFIG_HEADERS= $(SHELL) ./config.status

R:
	@if [ "$(srcdir)" != "." ]; then \
	  for f in $(INSTFILES); do \
	    $(INSTALL_DATA) $(srcdir)/$${f} $(top_builddir); \
	  done; \
	fi
	@for d in afm demos doc etc src; do \
	  (cd $${d} && $(MAKE) R) || exit 1; \
	done
	@if [ ! -f src/library/stamp-docs ]; then \
	  echo "You should \`make docs' now ..."; \
	fi

src/include/Rconfig.h: $(srcdir)/configure \
  $(srcdir)/src/include/Rconfig.h.in
	$(top_srcdir)/configure --srcdir $(top_srcdir) @config_opts@

docs help html latex examples:: FORCE
	-@(cd src/library && $(MAKE) $@)
FORCE:

install: all installdirs
	@for d in afm demos doc etc; do \
	  (cd $${d} && $(MAKE) $@) || exit 1; \
	done
	@(cd $(srcdir); \
	  for f in $(INSTFILES); do \
	    $(INSTALL_DATA) $${f} $(rhome); \
	  done)
	@echo "Installing executables ..."
	@$(INSTALL_PROGRAM) bin/R.X11 $(rhome)/bin
	@if test -x bin/R.gnome; then \
	  $(INSTALL_PROGRAM) bin/R.gnome $(rhome)/bin; \
	fi
	@sed "s@R_HOME=.*@R_HOME=$(rhome)\}@" < bin/R > $(rhome)/bin/R
	@sed "s@R_HOME=.*@R_HOME=$(rhome)\}@" < bin/R > $(bindir)/R
	@chmod 755 $(bindir)/R $(rhome)/bin/R
	@for f in `ls bin/* | grep -v '^bin/R\**$$' \
                            | grep -v '^bin/R.X11\**$$' \
                            | grep -v '^bin/R.gnome\**$$'`; do \
	  $(INSTALL) $${f} $(rhome)/bin; \
	done
	@for f in Rd2txt Rdconv Rdindex Sd2Rd; do \
	  $(INSTALL) bin/$${f} $(bindir); \
	done
	@echo "Installing headers ..."
	@for f in include/*.h; do \
	  $(INSTALL_DATA) $${f} $(rhome)/include; \
	done
	@echo "Installing library ..."
	@cp -r library $(rhome) || \
	  (cd library; \
	    $(TAR) cf - [a-z]* | (cd $(rhome)/library; $(TAR) xof -))
	@echo "  Building help index ..."
	@(cd $(rhome)/library; cat */TITLE > LibIndex 2> /dev/null)
	@echo "  Building HTML index ..."
	@$(rhome)/bin/build-help --rhome $(rhome) --htmllists
installdirs:
	@$(MKINSTALLDIRS) $(bindir)
	@$(MKINSTALLDIRS) $(rhome)/bin
	@$(MKINSTALLDIRS) $(rhome)/include
	@$(MKINSTALLDIRS) $(rhome)/library
install-strip:
	$(MAKE) INSTALL_PROGRAM="${INSTALL_PROGRAM} -s" install
uninstall:
	@echo "Uninstalling library ..."
	@(cd $(rhome)/library; \
	  for p in $(R_PKGS); do rm -rf $${p}; done)
	@echo "  Rebuilding help index ..."
	@(cd $(rhome)/library; \
	  cat */TITLE > LibIndex 2> /dev/null || echo "" > LibIndex)
	@echo "  Rebuilding HTML index ..."
	@if test -x $(rhome)/bin/build-help; then \
	  $(rhome)/bin/build-help --rhome $(rhome) --htmllists; \
	fi
	@echo "Uninstalling headers ..."
	@rm -rf $(rhome)/include
	@echo "Uninstalling executables ..."
	@rm -rf $(rhome)/bin
	@for f in R Rd2txt Rdconv Rdindex Sd2Rd; do \
	  rm -f $(bindir)/$${f}; \
	done
	@for f in $(INSTFILES); do \
	  rm -f $(rhome)/$${f}; \
	done
	@for d in afm demos doc etc; do \
	  (cd $${d} && $(MAKE) $@); \
	done

mostlyclean: clean
clean:
	@for d in $(SUBDIRS); do \
	  (cd $${d} && $(MAKE) $@); \
	done
	@if [ "$(srcdir)" != "." ]; then \
	  rm -f $(INSTFILES);
	fi
distclean: clean
	@for d in $(SUBDIRS); do \
	  (cd $${d} && $(MAKE) $@); \
	done
	@rm -rf bin include library
	@rm -f Makeconf Makefile Makefile.bak Makefrag.* \
	  config.cache config.log config.status
maintainer-clean: distclean

dist: dist-unix
dist-unix: distdir
	-chmod -R a+r $(distdir)
	GZIP=$(GZIP) $(TAR) chzf $(distdir).tar.gz $(distdir)
	rm -rf $(distdir)
dist-win:
distdir: $(DISTFILES)
	@rm -rf $(distdir)
	@mkdir $(distdir)
	@-chmod 755 $(distdir)
	@for f in $(DISTFILES); do \
	  test -f $(distdir)/$${f} \
            || ln $(srcdir)/$${f} $(distdir)/$${f} 2>/dev/null \
	    || cp -p $(srcdir)/$${f} $(distdir)/$${f}; \
	done
	@for d in $(SUBDIRS); do \
	  test -d $(distdir)/$${d} \
	    || mkdir $(distdir)/$${d} \
	    || exit 1; \
	  chmod 755 $(distdir)/$${d}; \
	  (cd $${d} && $(MAKE) distdir) \
	    || exit 1; \
        done
	@for d in debian; do \
	  ((cd $(srcdir); $(TAR) -c -f - --exclude=CVS $${d}) \
	    | (cd $(distdir); $(TAR) -x -f -)) \
	    || exit 1; \
	done

dvi info pdf::
	-@(cd doc && $(MAKE) $@)
install-dvi install-info install-pdf::
	-@(cd doc/manual && $(MAKE) $@)
uninstall-dvi uninstall-info uninstall-pdf::
	-@(cd doc/manual && $(MAKE) $@)

TAGS:

check test:: test-All
test-All test-Examples test-Specific::
	@(cd tests && $(MAKE) $@)
check-force: test-All-force
test-All-force:
	@(cd tests && $(MAKE) test-All FORCE=FORCE)
test-Examples-force:
	@(cd tests && $(MAKE) test-Examples FORCE=FORCE)
test-Specific-force:
	@(cd tests && $(MAKE) test-Specific FORCE=FORCE)
