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

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

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

include $(top_builddir)/Makeconf

SCRIPTS_S = BATCH LINK Rcmd Rdiff Rd2dvi Rd2txt help pager
SCRIPTS_B_IN = COMPILE.in INSTALL.in REMOVE.in Rdconv.in Rdindex.in \
	Rprof.in SHLIB.in Sd2Rd.in build.in check.in
SCRIPTS_B = $(SCRIPTS_B_IN:.in=)

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

.SUFFIXES:
.SUFFIXES: .in

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

all: Makefile R

Makefile: $(srcdir)/Makefile.in $(top_builddir)/config.status
	@cd $(top_builddir) && \
	  CONFIG_FILES=$(subdir)/$@ CONFIG_HEADERS= \
	  $(SHELL) ./config.status
	@(for f in $(SCRIPTS_B); do \
	    echo "$${f}: $(srcdir)/$${f}.in"; \
	  done) >> $@

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

$(SCRIPTS_B): $(top_builddir)/config.status

# NOTE: the front-end shell script now (2000-08-02) also contains the
# version information.  As we keep this information outside configure
# (as otherwise developers would have to reconfigure and hence rebuild
# every day), we now first create `R.sh' from `R.sh.in' via configure,
# and then use sed to create the front end (`R.fe') which gets installed
# as `R'.
R.sh: $(srcdir)/R.sh.in $(top_builddir)/config.status
R.fe: R.sh $(top_srcdir)/VERSION $(top_srcdir)/date-stamp
	@echo "creating $(subdir)/$@"
	@(v=`cat $(top_srcdir)/VERSION`; \
	  v="$${v} (`sed 's|/|-|g' $(top_srcdir)/date-stamp`)"; \
	  y=`cut -d/ -f1 < $(top_srcdir)/date-stamp`; \
	  sed "s@R_HOME_DIR=.*@R_HOME_DIR=`cd $(top_builddir); pwd`@; \
	       s@REPLACE_BY_VERSION@$${v}@; \
	       s@REPLACE_BY_YEAR@$${y}@" < R.sh > $@)
	@chmod +x $@

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

install: installdirs install-cmds
	@sed "s@R_HOME_DIR=.*@R_HOME_DIR=$(rhome)@" < R.fe \
	  > $(Rexecbindir)/R
	@rm -f $(bindir)/R
	@sed "s@R_HOME_DIR=.*@R_HOME_DIR=$(rhome)@" < R.fe \
	  > $(bindir)/R
	@chmod 755 $(bindir)/R $(Rexecbindir)/R
installdirs:
	@$(MKINSTALLDIRS) $(bindir)
install-cmds:
	@$(MKINSTALLDIRS) $(Rexecbindir)
	@for f in $(SCRIPTS_S); do \
	  $(INSTALL_SCRIPT) $(srcdir)/$${f} $(Rexecbindir); \
	done
	@for f in $(SCRIPTS_B); do \
	  $(INSTALL_SCRIPT) $${f} $(Rexecbindir); \
	done
	@$(INSTALL_SCRIPT) $(top_builddir)/libtool $(Rexecbindir)
install-strip:
	$(MAKE) INSTALL_PROGRAM="${INSTALL_PROGRAM} -s" install
uninstall:
	@for f in $(SCRIPTS_S) $(SCRIPTS_B) libtool R; do \
	  rm -f $(Rexecbindir)/$${f}; \
	done
	@rmdir $(Rexecbindir) 2>/dev/null \
	  || echo "  subdir $(Rexecbindir) not removed"
	@rm -f $(bindir)/R

mostlyclean: clean
clean:
distclean:
	@rm -f Makefile R.fe R.sh $(SCRIPTS_B)
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

## Automagically generated dependencies:

