#!/bin/sh
# Shell wrapper for R executable.

R_HOME_DIR=/usr/local/lib/R
if test -n "${R_HOME}" && \
   test "${R_HOME}" != "${R_HOME_DIR}"; then
  echo "WARNING: ignoring environment value of R_HOME"
fi
R_HOME=${R_HOME_DIR}
export R_HOME

## NOTE:
## We must set this here rather than in the main binary.
: ${R_LD_LIBRARY_PATH=${R_HOME}/bin:/usr/local/lib:/usr/local/lib:/usr/local/lib:/usr/local/lib/gcc-lib/alpha-dec-osf4.0/2.95.2:/usr/lib/cmplrs/cc:/usr/local/lib}
if test -z "${LD_LIBRARY_PATH}"; then
  LD_LIBRARY_PATH="${R_LD_LIBRARY_PATH}"
else
  LD_LIBRARY_PATH="${R_LD_LIBRARY_PATH}:${LD_LIBRARY_PATH}"
fi
export LD_LIBRARY_PATH

usage="
Usage: R [options] [< infile] [> outfile]
   or: R CMD command [arguments]

Start R, a system for statistical computation and graphics, with the
specified options, or invoke an R tool via the \`R CMD' interface.

Options:
  -h, --help            Print short help message and exit
  --version             Print version info and exit
  RHOME			Print path to R home directory and exit
  --save                Do save data sets at the end of the session
  --no-save             Don't save them
  --no-environ          Don't read the .Renviron or ~/.Renviron files
  --no-site-file        Don't read the site-wide Rprofile
  --no-init-file        Don't read the .Rprofile or ~/.Rprofile files
  --restore             Do restore previously saved data sets at startup
  --no-restore-data     Don't restore previously saved data sets
  --no-restore-history  Don't restore the R history file
  --no-restore          Don't restore anything
  --vanilla		Combine --no-save, --no-restore, --no-site-file,
			--no-init-file and --no-environ
  --no-readline         Don't use readline for command-line editing
  --min-vsize=N         Set vector heap min to N bytes; \`4M' = 4 MegaB
  --max-vsize=N         Set vector heap max to N bytes;
  --min-nsize=N         Set min number of cons cells to N
  --max-nsize=N         Set max number of cons cells to N
  -q, --quiet           Don't print startup message
  --silent              Same as --quiet
  --slave               Make R run as quietly as possible
  --verbose             Print more information about progress
  -d, --debugger=NAME   Run R through debugger NAME
  -g, --gui=TYPE	Use TYPE as GUI; possible values are \`X11'
			(default), \`none' and \`gnome'

Commands:
  BATCH			Run R in batch mode
  COMPILE		Compile files for use with R
  SHLIB			Build shared library for dynamic loading
  INSTALL		Install add-on packages
  REMOVE		Remove add-on packages
  build			Build add-on packages
  check			Check add-on packages
  LINK			Front-end for creating executable programs
  Rprof			Post-process R profiling files
  Rdconv		Convert Rd format to various other formats
  Rd2dvi		Convert Rd format to DVI/PDF
  Rd2txt		Convert Rd format to pretty text
  Rdindex		Extract index information from Rd files
  Sd2Rd			Convert S documentation to Rd format

The first five tools (i.e., BATCH, COMPILE, SHLIB, INSTALL, and REMOVE)
can also be invoked without the \`CMD' option.

Please use \`R CMD command --help' to obtain further information about
the usage of \`command'.

Report bugs to <r-bugs@r-project.org>."

version="R 1.3.0 Under development (unstable) (2001-06-18).
Copyright (C) 2001 R Development Core Team

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under the terms of the GNU
General Public License.  For more information about these matters,
see http://www.gnu.org/copyleft/gpl.html."

### Argument loop
args=
debugger=
while test -n "${1}"; do
  case ${1} in
    RHOME|--print-home)
      echo "${R_HOME}"; exit 0 ;;
    BATCH|COMPILE|INSTALL|REMOVE|SHLIB)
      exec sh "${R_HOME}/bin/Rcmd" "${@}" ;;
    CMD)
      shift;
      exec sh "${R_HOME}/bin/Rcmd" "${@}" ;;
    --gnome)
      echo "option \`${1}' is defunct, use \`--gui=gnome'"
      exit 1
      ;;
    -d|--debugger)
      if test -n "`echo ${2} | sed 's/^-.*//'`"; then      
	debugger="${2}"; shift
      else
	echo "ERROR: option \`${1}' requires an argument"
	exit 1
      fi
      ;;
    --debugger=*)
      debugger=`echo "${1}" | sed -e 's/[^=]*=//'` ;;
    -h|--help)
      echo "${usage}"; exit 0 ;;
    --version)
      echo "${version}"; exit 0 ;;
    *)
      args="${args} ${1}" ;;
  esac
  shift
done

## R_HOME may have moved, so check
## (or you get "GUI X11 is not supported")
if test -x ${R_HOME}; then
  :
else
  echo "R_HOME (\`${R_HOME}') not found"
  exit 1
fi

## Startup
R_binary="R.bin"
if test -z "${debugger}"; then
  exec ${R_HOME}/bin/${R_binary}  ${args}
else
  if test -n "${args}"; then
    echo "*** Further command line arguments (\`${args}') disregarded"
    echo "*** (maybe use \`run ${args}' from *inside* ${debugger})"
    echo ""
  fi
  exec ${debugger} ${R_HOME}/bin/${R_binary}
fi

### Local Variables: ***
### mode: sh ***
### sh-indentation: 2 ***
### End: ***
