#
# ${R_HOME}/src/scripts/Makefile

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

top_builddir = ../..
subdir = src/scripts

include $(top_builddir)/Makeconf

## don't add '#! R_SHELL' for scripts intend to be run by 'R CMD script'
SCRIPTS_S = BATCH COMPILE INSTALL LINK REMOVE Rd2pdf Rdconv Rdiff Rprof \
  SHLIB Stangle Sweave build check config
## <NOTE>
## Need target rules for all elements of SCRIPTS_B_IN/SCRIPTS_B.
SCRIPTS_B_IN = Rcmd.in javareconf.in mkinstalldirs.in pager.in rtags.in
SCRIPTS_B = $(SCRIPTS_B_IN:.in=)
## </NOTE>

distdir = $(top_builddir)/$(PACKAGE)-$(VERSION)/$(subdir)
DISTFILES = Makefile.in Makefile.win $(SCRIPTS_S) $(SCRIPTS_B_IN) R.sh.in

CLEANFILES =
DISTCLEANFILES = Makefile R.sh $(SCRIPTS_B)

all: Makefile R

Makefile: $(srcdir)/Makefile.in $(top_builddir)/config.status
	@cd $(top_builddir) && $(SHELL) ./config.status $(subdir)/$@

R: $(SCRIPTS_B) R.sh
	@$(MAKE) DESTDIR="" rhome=$(top_builddir) install-cmds
	@$(INSTALL_SCRIPT) R.sh $(top_builddir)/bin/R

## <NOTE>
## Seems we need target rules for creating FOO from FOO.in: pattern
## rules ('%:%.in') are not portable, and some versions of Make do not
## allow separated dependencies for single suffix rules.
Rcmd: $(srcdir)/Rcmd.in $(top_builddir)/config.status
	@cd $(top_builddir) && $(SHELL) ./config.status $(subdir)/$@
javareconf: $(srcdir)/javareconf.in $(top_builddir)/config.status
	@cd $(top_builddir) && $(SHELL) ./config.status $(subdir)/$@
makeinstalldirs: $(srcdir)/makeinstalldirs.in $(top_builddir)/config.status
	@cd $(top_builddir) && $(SHELL) ./config.status $(subdir)/$@
pager: $(srcdir)/pager.in $(top_builddir)/config.status
	@cd $(top_builddir) && $(SHELL) ./config.status $(subdir)/$@
rtags: $(srcdir)/rtags.in $(top_builddir)/config.status
	@cd $(top_builddir) && $(SHELL) ./config.status $(subdir)/$@
## The following target helps in automatically generating the above
## target rules (provided that $(ECHO) understands backslash escapes).
Makerules:
	@(dep="\$$(top_builddir)/config.status"; \
	  cmd="@cd \$$(top_builddir)"; \
	  cmd="$${cmd} && \$$(SHELL) ./config.status \$$(subdir)/\$$@"; \
	  for f in $(SCRIPTS_B); do \
	    $(ECHO) "$${f}: \$$(srcdir)/$${f}.in $${dep}"; \
	    $(ECHO) "	$${cmd}"; \
	  done)
## </NOTE>

R.sh: $(srcdir)/R.sh.in $(top_builddir)/config.status
	@cd $(top_builddir) && $(SHELL) ./config.status $(subdir)/$@
	@chmod +x $@

$(top_builddir)/libtool:
	@(cd $(@D); $(MAKE) $(@F))

install: installdirs install-cmds
	@rm -f $(DESTDIR)$(bindir)/R
	@(d=`$(ECHO) '$(rhome)' | sed 's,/,\\\/,g';`; \
	  d2=`$(ECHO) '$(rsharedir)' | sed 's,/,\\\/,g';`; \
	  d3=`$(ECHO) '$(rincludedir)' | sed 's,/,\\\/,g';`; \
	  d4=`$(ECHO) '$(rdocdir)' | sed 's,/,\\\/,g';`; \
	  sed -e "1,/R_HOME_DIR=/s/\\(R_HOME_DIR=\\).*/\\1\"$${d}\"/;" -e "s/\\(R_SHARE_DIR=\\).*/\\1\"$${d2}\"/;" -e  "s/\\(R_INCLUDE_DIR=\\).*/\\1\"$${d3}\"/;" -e "s/\\(R_DOC_DIR=\\).*/\\1\"$${d4}\"/;"\
	  < R.sh > "$(DESTDIR)$(Rexecbindir)/R")
	@$(INSTALL_SCRIPT)  "$(DESTDIR)$(Rexecbindir)/R" "$(DESTDIR)$(bindir)/R"
	@chmod 755 "$(DESTDIR)$(bindir)/R" "$(DESTDIR)$(Rexecbindir)/R"
## why of all the scripts does this alone chmod just one copy?
installdirs:
	@$(MKINSTALLDIRS) $(DESTDIR)$(bindir)
install-cmds:
	@$(MKINSTALLDIRS) "$(DESTDIR)$(Rexecbindir)"
	@for f in $(SCRIPTS_S); do \
	  source="$(srcdir)/$${f}"; \
	  target="$(DESTDIR)$(Rexecbindir)/$${f}"; \
	  (test -r "$${target}" && \
	   cmp "$${source}" "$${target}" > /dev/null) || \
	    $(INSTALL_SCRIPT) "$${source}" "$${target}"; \
	done
	@for f in $(SCRIPTS_B); do \
	  $(INSTALL_SCRIPT) $${f} "$(DESTDIR)$(Rexecbindir)"; \
	done
	@$(INSTALL_SCRIPT) $(top_builddir)/libtool "$(DESTDIR)$(Rexecbindir)"
## there are no programs to install in this dir
install-strip:
	@$(MAKE) INSTALL_PROGRAM="${INSTALL_PROGRAM} -s" install
uninstall:
	@for f in $(SCRIPTS_S) $(SCRIPTS_B) libtool R; do \
	  rm -f "$(DESTDIR)$(Rexecbindir)/$${f}"; \
	done
	@rm -f $(DESTDIR)$(bindir)/R

mostlyclean: clean
clean:
	-@test -z "$(CLEANFILES)" || rm -f $(CLEANFILES)
distclean: clean
	-@test -z "$(DISTCLEANFILES)" || rm -f $(DISTCLEANFILES)
maintainer-clean: distclean

TAGS info dvi check:

distdir: $(DISTFILES)
	@for f in $(DISTFILES); do \
	  test -f $(distdir)/$${f} \
	    || ln $(srcdir)/$${f} $(distdir)/$${f} 2>/dev/null \
	    || cp -p $(srcdir)/$${f} $(distdir)/$${f}; \
	done
