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

: ${R_HOME=/usr/local/lib/R}
export R_HOME

: ${R_LD_LIBRARY_PATH=/usr/local/lib:${R_HOME}/library/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
if test -n "${R_LIBS}"; then
  R_LIBS_PARTS=`echo $R_LIBS| sed -e 's/:/ /g'`
  for i in $R_LIBS_PARTS; do
    LD_LIBRARY_PATH="${LD_LIBRARY_PATH}:$i/lib"
  done
fi
export LD_LIBRARY_PATH

R_binary="R.bin"

# Default printer paper size
# Choose one of the following:
# R_PAPERSIZE="a4"
# R_PAPERSIZE="letter"
# R_PAPERSIZE="none"
: ${R_PAPERSIZE=a4}
export R_PAPERSIZE

# Default print command
# Choose one of the following:
# R_PRINTCMD="lpr"
# R_PRINTCMD="lp"
# R_PRINTCMD=""
: ${R_PRINTCMD=lpr}
export R_PRINTCMD

# Default TeXMF stuff
R_LATEXCMD=${R_LATEXCMD:-${LATEX:-/usr/local/bin/latex}}
R_DVIPSCMD=${R_DVIPSCMD:-${DVIPS:-/usr/local/bin/dvips}}
R_MAKEINDEXCMD=${R_MAKEINDEX:-${MAKEINDEX:-/usr/local/bin/makeindex}}
export R_LATEXCMD R_DVIPSCMD R_MAKEINDEXCMD
: ${R_RD4DVI=ae}
: ${R_RD4PDF=ae,hyper}
export R_RD4DVI R_RD4PDF

# Default zip/unzip commands
: ${R_UNZIPCMD=/usr/local/bin/unzip}
: ${R_ZIPCMD=/usr/local/bin/zip}
export R_UNZIPCMD R_ZIPCMD

EDITOR=${EDITOR-${VISUAL-vi}}
export EDITOR

# Default environment file -- used if there's none in current
: ${R_ENVIRON="${HOME}/.Renviron"}
use_r_environ=true

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          Don't restore them
  --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
  --vsize=N             Set vector heap size to N bytes; \`4M' = 4 MegaB
  --nsize=N             Set 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
  LIBINDEX              Update the help index (text and html)
  build			Build add-on packages
  check			Check add-on packages
  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 six tools (i.e., BATCH, COMPILE, SHLIB, INSTALL, REMOVE and LIBINDEX)
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@lists.r-project.org>."

# 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|LIBINDEX)
      cmd="${1}"; shift; exec sh "${R_HOME}/bin/${cmd}" "${@}" ;;
    CMD)
      shift; PATH="${R_HOME}/bin:${PATH}" exec "${@}" ;;
    --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/[^=]*=//'` ;;
    --no-environ)
      use_r_environ=false ;;
    --vanilla)
      args="${args} ${1}"
      use_r_environ=false ;;
    -h|--help)
      echo "${usage}"; exit 0 ;;
    --version)
      exec ${R_HOME}/bin/${R_binary} --version ;;
    *)
      args="${args} ${1}" ;;
  esac
  shift
done

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

# Startup
if ${use_r_environ}; then # use the one in current dir, or default
  if test -r ./.Renviron; then
    . ./.Renviron
    export `sed 's/^ *#.*//; s/^\(.*\)=.*/\1/' ./.Renviron`
  elif test -r ${R_ENVIRON}; then
    . ${R_ENVIRON}
    export `sed 's/^ *#.*//; s/^\(.*\)=.*/\1/' ${R_ENVIRON}`
  fi
fi

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: ***
