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

: ${R_HOME=/opt/SDGR/0.65.0/lib/R}
export R_HOME

R_BINARY="R.X11"

# 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=lpr}
export R_PRINTCMD

# Default LaTeX and dvips commands
R_LATEXCMD=${R_LATEXCMD:-${LATEX:-/opt/TeX/bin/latex}}
R_DVIPSCMD=${R_DVIPSCMD:-${DVIPS:-/opt/TeX/bin/dvips}}
export R_LATEXCMD R_DVIPSCMD

# Default zip/unzip commands
: ${R_UNZIPCMD=/net/user/mgd/bin/unzip}
: ${R_ZIPCMD=/opt/src/mgd/packages/development/egcs/bin/zip}
export R_UNZIPCMD R_ZIPCMD

# Default environment file
: ${R_ENVIRON="${HOME}/.Renviron"}
USE_R_ENVIRON=true

# Argument loop
DBG=
while test -n "${1}"; do
  case ${1} in
    RHOME)
      echo ${R_HOME}; exit 0 ;;
    CMD)
      shift; PATH="${PATH}:${R_HOME}/bin" exec "${@}" ;;
    BATCH|COMPILE|INSTALL|REMOVE|SHLIB)
      CMD="$1"; shift; exec sh "${R_HOME}/bin/${CMD}" "${@}" ;;
    -g|--gnome)
      R_BINARY="R.gnome" ;;
    -d|--debugger)
      DBG="${2}"; shift 2 ;;
    --no-environ)
      USE_R_ENVIRON=false ;;
    --vanilla)
      ARGS="${ARGS} ${1}"
      USE_R_ENVIRON=false ;;    
    -h|--help|-\?)
      $0 --version
      cat <<_EOF_

Usage:  R [OPTIONS] [< INFILE] [> OUTFILE]
   or   R command      [arguments]
   or   R CMD Rcommand [arguments]

Options:
  --gnome                   Run the gnome binary.
  --save                    Do save data sets at the end of the session.
  --no-save                 Don't save them.
  --restore                 Do restore previously saved data sets at startup.
  --no-restore              Don't restore them.
  --no-readline             Don't use readline for command-line editing.
  --no-site-file            Don't read the site-wide Rprofile.
  --no-init-file            Don't read the .Rprofile or ~/.Rprofile files.
  --no-environ              Don't read the ~/.Renviron file.
  --vanilla		    Combine --no-save, --no-restore, --no-site-file,
                            --no-init-file, and --no-environ.
  --vsize N                 Set vector heap size to N bytes; \`4M' = 4 MegaB.
  --nsize N                 Set number of cons cells to N. ___See ?Memory___
  -h, --help, -?            Print short help message and exit.
  -q, --quiet, --silent     Don't print startup message.
  --slave                   Make R run as quietly as possible.
  --version                 Print version info and exit.
  --verbose                 Print more information about progress.
  -d NAME, --debugger NAME  Run R through debugger NAME.

Commands:
  RHOME				Give R's home directory
  BATCH infile [ outfile ]	Run Batch job
  COMPILE|INSTALL|REMOVE|SHLIB  Package management & dyn.load compilation

Rcommands:
  Rdconv
  Rdindex, Sd2Rd
  Rd2dvi, Rd2txt
   ...

Report bugs to R-bugs@biostat.ku.dk.

_EOF_
      exit 0 ;;
    *)
      ARGS="${ARGS} ${1}" ;;
  esac
  shift
done

# Startup
if ${USE_R_ENVIRON}; then
  [ -r ${R_ENVIRON} ] && . ${R_ENVIRON}
fi

if test -z "${DBG}"; 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* ${DBG})"
    echo ""
  fi
  exec ${DBG} ${R_HOME}/bin/${R_BINARY}
fi

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