#! /bin/sh
#
# ${R_HOME}/bin/INSTALL for installing add-on packages

# src/scripts/INSTALL.  Generated from INSTALL.in by configure.

revision='$Revision: 1.161 $'
version=`set - ${revision}; echo ${2}`
version="R add-on package installer ${version}

Copyright (C) 2000-2004 The R Core Development Team.
This is free software; see the GNU General Public Licence version 2
or later for copying conditions.  There is NO warranty."

usage="Usage: R CMD INSTALL [options] pkgs

Install the add-on packages specified by pkgs.  The elements of pkgs can
be relative or absolute paths to directories with the package (bundle)
sources, or to gzipped package 'tar' archives.  The library tree to
install to can be specified via '--library'.  By default, packages are
installed in the library tree rooted at the first directory given in the
environment variable R_LIBS if this is set and non-null, and into the
default R library tree (${R_HOME}/library) otherwise.

Options:
  -h, --help		print short help message and exit
  -v, --version		print INSTALL version info and exit
      --configure-args=ARGS
                        set arguments for the package's configure script
			(if any)
      --configure-vars=VARS
                        set variables for the configure script (if any)
  -c, --clean		remove all files created during installation
  -s, --save[=ARGS]     save the package source as an image file, and
                        arrange for this file to be loaded when the
                        package is attached; if given, ARGS are passed
                        to R when creating the save image
      --no-save         do not save the package source as an image file
      --lazy            use lazy loading
      --no-lazy         do not use lazy loading
      --lazy-data       use lazy loading for data
      --no-lazy-data    do not use lazy loading for data (current default)
  -d, --debug		turn on shell and build-help debugging
  -l, --library=LIB	install packages to library tree LIB
      --no-configure    do not use the package's configure script
      --no-docs		do not build and install documentation
      --with-package-versions
                        allow for multiple versions of the same package
      --use-zip-data	collect data files in zip archive
      --use-zip-help	collect help and examples into zip archives
      --use-zip		combine '--use-zip-data' and '--use-zip-help'
      --fake		do minimal install for testing purposes
      --no-lock		install on top of any existing installation
                        without using a lock directory

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

## <NOTE>
## This is a *shell* script.
## According to the R Coding Standards (see R-exts), Perl can be assumed
## for *source*, but not for *binary* installations.
## </NOTE>

## <NOTE>
## We try to be defensive about possible white space in file names,
## hence all the strange quoting ...
## </NOTE>

umask 022

R_VERSION='2.0.1'
GETWD='/bin/pwd'
R_GZIPCMD='/usr/bin/gzip'
NO_PERL5=false
NO_PERL5_MSG="\
*** Formatting and installing R help pages needs Perl version 5, which
*** does not seem to be installed on your system or is not in your path.
*** Please install either Perl 5 on your system and re-configure R or
*** get the PDF reference manual from the nearest CRAN server.
*** The CRAN master site can be found at
***    http://cran.r-project.org/"

if mkdir -p . 2>/dev/null; then
  MKDIR_P="mkdir -p"
else
  MKDIR_P="${R_HOME}/bin/mkinstalldirs"
fi

warning () { echo "WARNING: $*" >&2 ; }
error ()   { echo "ERROR: $*" >&2 ; }

startdir=`${GETWD}`
tmpdir="${TMPDIR-/tmp}/R.INSTALL.$$"
if (test -d "${tmpdir}" && test -w "${tmpdir}") || \
    ${MKDIR_P} "${tmpdir}" 2> /dev/null; then
  tmpdir=`cd "${tmpdir}" && ${GETWD}`
else
  error "cannot write to or create temporary directory '${tmpdir}'"
  exit 1
fi

: ${R_OSTYPE=unix}
## <NOTE>
## Unix only ... but Windows has INSTALL as a Perl script.
R_EXE="${R_HOME}/bin/R"
## </NOTE>

## <NOTE>
## This could be made a bit more general: if R CMD INSTALL is run by
## another tool (e.g., when building or checking a package, its messages
## should be a section level deeper (at least).  So we could have an
## argument to set the initial secnumdepth (0 by default), and work
## against this ...
message () { echo "${stars} $*"; }
stars="*"
## </NOTE>

. "${R_HOME}/share/sh/dcf.sh"	# get_dcf_field()

get_packages () {
  ## get the full path names to all packages contained in $1.
  ## NOTE: modifies pkgs!
  if grep "^Contains:" "${1}/DESCRIPTION" >/dev/null; then
    bundlepkg=`get_dcf_field Contains "${1}/DESCRIPTION"`
    for p in ${bundlepkg}; do
      pkgs="${pkgs} \"`cd "${1}/${p}" && ${GETWD}`\""
      if test -f "${1}/${p}/DESCRIPTION.in"; then
        ## Try being defensive about missing final newlines, or extra
	## empty lines.
        (cat "${1}/${p}/DESCRIPTION.in"
	  echo
          grep -v "^Contains:" "${1}/DESCRIPTION"
	  echo) | sed '/^ *$/d' > "${1}/${p}/DESCRIPTION"
      fi
    done
  else
    pkgs="${pkgs} \"`cd "${1}" && ${GETWD}`\""
  fi
}

parse_description_field () { # fieldname
  tmp=`get_dcf_field ${1} DESCRIPTION`
  if test "x$tmp" != "x" ; then
    case "$tmp" in
      yes|Yes|true|True|TRUE)
      value=true
      ;;
      no|No|false|False|FALSE)
      value=false
      ;;
      *)
      error "invalid value of ${1} field in DESCRIPTION"
      do_exit_on_error
      ;;
    esac
  else
    value="missing"
  fi
}

### 
### Setup and command line processing.

if test -n "${R_LIBS}"; then
  lib=`echo "${R_LIBS}" | cut -f1 -d:`
else
  lib="${R_HOME}/library"
fi

pkgs=
clean=false
debug=false
build_text=true
build_html=true
build_latex=true
build_example=true
build_help=true
build_help_opts=
use_configure=true
use_zip_data=
use_zip_help=
configure_args=
configure_vars=
with_package_versions=false
save="CHECK"
save_args=
fake=false
lazy="auto"
lazy_data=false
lock=true

while test -n "${1}"; do
  ##DBG echo "   in 'R INSTALL' --arg checking-- \$1 = '${1}'" # Debugging
  case ${1} in
    -h|--help)
      echo "${usage}"; exit 0 ;;
    -v|--version)
      echo "${version}"; exit 0 ;;
    -c|--clean)
      clean=true ;;
    -s|--save)
      save=true;;
    --save=*)
      save_args=`echo "${1}" | sed -e 's/[^=]*=//'` ;;
    --lazy)
      lazy=true ;;
    --no-lazy)
      lazy=false ;;
    --lazy-data)
      lazy_data=true ;;
    --no-lazy-data)
      lazy_data=false ;;
    --no-save)
      save=false ;;
    -d|--debug)
      debug=true ;;
    --with-package-versions)
      with_package_versions=true ;;
    --no-configure)
      use_configure=false ;;
    --no-docs)
      build_text=false
      build_html=false
      build_latex=false
      build_example=false ;;
    --no-text)
      build_text=false ;;
    --no-html)
      build_html=false ;;
    --no-latex)
      build_latex=false ;;
    --no-example)
      build_example=false ;;
    --use-zip)
      use_zip_data=true
      use_zip_help=true ;;
    --use-zip-data)
      use_zip_data=true ;;
    --use-zip-help)
      use_zip_help=true ;;
    -l|--library)
      lib="${2}"; shift ;;
    --library=*)
      lib=`echo "${1}" | sed -e 's/[^=]*=//'` ;;
    --configure-args=*)
      configure_args=`echo "${1}" | sed -e 's/[^=]*=//'` ;;
    --configure-vars=*)
      configure_vars=`echo "${1}" | sed -e 's/[^=]*=//'` ;;
    --fake)
      fake=true ;;
    --no-lock)
      lock=false ;;
    *)
      if test -f "${1}"; then
	##DBG echo "  in 'R INSTALL' --arg checking-- FILE '${1}'"
	pkgname=`basename "${1}"`
	## Also allow for 'package.tgz' ...
	pkgname=`basename "${pkgname}" .tgz`
	pkgname=`echo "${pkgname}" | sed 's/_.*//'`
	## Note that we use '-m' so that modification dates are *not*
	## preserved when untarring the sources.  This is necessary to
	## ensure that the preformatted help pages are always rebuilt.
	## Otherwise, the build date for an older version may be newer
	## than the modification date for the new sources as recorded in
	## the tarball ...
	gzip -dc "${1}" | (cd "${tmpdir}" && ${TAR} -mxf -)
	if test -f "${tmpdir}/${pkgname}/DESCRIPTION"; then
	  get_packages "${tmpdir}/${pkgname}"
	else
	  error "cannot extract package from '${1}'"
	  exit 1
	fi
      elif test -f "${1}/DESCRIPTION"; then
	##DBG echo " in 'R INSTALL' -f DESCRIPTION --> 'get_packages ${1}'"
        get_packages "${1}"
      else
	warning "invalid package '${1}'"
      fi
      ;;
  esac
  shift
done

if ${debug} ; then
  echo "'Rcmd INSTALL': in startdir= $startdir with tmpdir= $tmpdir"
  echo "   lib= '$lib', pkgs= '$pkgs'"
fi

if test -z "${pkgs}"; then
  error "no packages specified"
  exit 1
fi

if (test -d "${lib}" && test -w "${lib}") || \
    ${MKDIR_P} "${lib}" 2> /dev/null; then
  lib=`cd "${lib}" && ${GETWD}`
else
  error "cannot write to or create directory '${lib}'"
  exit 2
fi
if ${lock} ; then
  lockdir="${lib}/00LOCK"
  if ${debug} ; then  echo "   before checking lockdir= '${lockdir}'" ; fi
  if test -d "${lockdir}" || test -f "${lockdir}"; then
    error "failed to lock directory '${lib}' for modifying"
    echo "Try removing '${lockdir}'" >&2
    exit 3
  else
    ${MKDIR_P} "${lockdir}"
    if test ${?} -ne 0; then
      ## This should not really happen ...
      error "failed to lock directory '${lib}' for modifying"
      exit 3
    fi
  fi
  if ${debug} ; then  echo '   after checking lockdir' ; fi
fi

if ${fake}; then
  use_configure=false
  build_text=true
  build_html=false
  build_latex=true
  build_example=false
fi

if ${build_text}; then
  build_help_opts="${build_help_opts} --txt"
fi
if ${build_html}; then
  build_help_opts="${build_help_opts} --html"
fi
if ${build_latex}; then
  build_help_opts="${build_help_opts} --latex"
fi
if ${build_example}; then
  build_help_opts="${build_help_opts} --example"
fi
if test -z "${build_help_opts}"; then
  build_help=false
elif ${debug}; then
  build_help_opts="--debug ${build_help_opts}"
fi

if ${debug} ; then echo "   build_help_opts= '${build_help_opts}'" ; fi

is_first_package=yes

### 
### Install a *binary* package (not bundle) from the current directory.

do_install_binary () {
  pkg="${1}"
  R_PACKAGE_DIR="${2}"
  R_PACKAGE_NAME="${3}"

  if test "${pkg}" = "${R_PACKAGE_NAME}"; then
    message "Installing *binary* package '${pkg}' ..."
  else
    message "Installing *binary* package '${pkg}' as '${R_PACKAGE_NAME}' ..."
  fi

  if test -f "${R_PACKAGE_DIR}/DESCRIPTION"; then
    ## Back up previous version.
    if ${lock}; then mv "${R_PACKAGE_DIR}" "${lockdir}/${R_PACKAGE_NAME}" ; fi
    ${MKDIR_P} "${R_PACKAGE_DIR}"
  fi

  cp -r . "${R_PACKAGE_DIR}" || \
    (${TAR} cf - . | (cd "${R_PACKAGE_DIR}" && ${TAR} xf -))

  if test ${?} -ne 0; then
    error "installing binary package failed"
    do_exit_on_error
  fi
}

### 
### Install a *source* package from the current directory.

do_install_source () {
  pkg_name="${1}"
  pkg_dir="${4}"
  ## (The basename of ${pkg_dir} might be different from ${pkg_name},
  ## e.g., when building Debian packages from R packages.)

  ## Make the destination directories available to the developer's
  ## installation scripts (e.g. configure, etc.)
  R_PACKAGE_DIR="${2}"
  R_PACKAGE_NAME="${3}"
  R_LIBRARY_DIR="${lib}"
  export R_LIBRARY_DIR
  export R_PACKAGE_DIR
  export R_PACKAGE_NAME

  if test "${pkg_name}" = "${R_PACKAGE_NAME}"; then
    message "Installing *source* package '${pkg_name}' ..."
  else
    message "Installing *source* package '${pkg_name}' as '${R_PACKAGE_NAME}' ..."
  fi

  stars="**"

  if test -f "${R_PACKAGE_DIR}/DESCRIPTION"; then
    ## Back up previous version.
    if ${lock} ; then 
      mv "${R_PACKAGE_DIR}" "${lockdir}/${R_PACKAGE_NAME}"
    else
      ## this is only used for recommended packages installed from .tgz
      rm -rf "${R_PACKAGE_DIR}"
    fi
    ${MKDIR_P} "${R_PACKAGE_DIR}"
    ## Preserve man pages to speed up installation?  Only makes sense
    ## if we install from a non-temporary directory.
    if ${lock} && test "`cd .. && ${GETWD}`" != \
            "`cd \"${tmpdir}\" && ${GETWD}`"; then
      (cd "${lockdir}/${R_PACKAGE_NAME}" \
          && ${TAR} cf - R-ex help html latex 2>/dev/null) | \
	(cd "${R_PACKAGE_DIR}" && ${TAR} xf -)
    fi
    ## check if we are in a bundle so we can back out the whole bundle
    if grep "^Bundle:" DESCRIPTION >/dev/null; then
      bundlepkg=`get_dcf_field Contains ../DESCRIPTION`
    else
      bundlepkg=${pkg_name}
    fi
  fi

  if ${use_configure} && test -x ./configure ; then
    eval ${configure_vars} ./configure ${configure_args}
    if test ${?} -ne 0; then
      error "configuration failed for package '${pkg_name}'"
      do_exit_on_error
    fi
  fi

  for f in COPYING NAMESPACE; do
    if test -f "${f}"; then
      cp "${f}" "${R_PACKAGE_DIR}"
      chmod 644 "${R_PACKAGE_DIR}/${f}"
    fi
  done

  ## Install DESCRIPTION file with build information.
  echo "tools:::.install_package_description(\".\", \"${R_PACKAGE_DIR}\")" | \
    R_DEFAULT_PACKAGES=NULL LANG=C "${R_EXE}" --vanilla >/dev/null
  if test ${?} -ne 0; then
    error "installing package DESCRIPTION failed"
    do_exit_on_error
  fi

  if test -d src && test "${fake}" != "true"; then
    message "libs"
    has_error=false
    if ${debug}; then set -x; fi
    ${MKDIR_P} "${R_PACKAGE_DIR}/libs"
    if test -f src/Makefile; then
      cd src;
      makefiles="-f \"${R_HOME}\"/share/make/shlib.mk -f Makefile"
      if test -r Makevars; then
	makefiles="-f Makevars ${makefiles}"
      fi
      eval ${MAKE} ${makefiles} \
	&& cp *.so "${R_PACKAGE_DIR}/libs" \
        || has_error=true; \
      cd ..
    else
      cd src;
      srcs=`ls *.[cfC] *.cc *.cpp 2>/dev/null`
      if test -n "${srcs}"; then
	sh "${R_HOME}/bin/SHLIB" -o "${pkg_name}.so" ${srcs} \
          && cp *.so "${R_PACKAGE_DIR}/libs" \
	  || has_error=true; \
      else
	warning "no source files found"
      fi
      cd ..
    fi
    if ${has_error}; then
      error "compilation failed for package '${pkg_name}'"
      do_exit_on_error
    fi
    chmod 755 "${R_PACKAGE_DIR}/libs/"*
    if ${debug}; then set +x; fi
  fi

  if test -d R; then
    message "R"
      ${MKDIR_P} "${R_PACKAGE_DIR}/R"
      rm -f "${R_PACKAGE_DIR}/R/"*
      echo "tools:::.install_package_code_files(\".\", \"${R_PACKAGE_DIR}\")" | \
        R_DEFAULT_PACKAGES=NULL LANG=C "${R_EXE}" --vanilla >/dev/null
      if test ${?} -ne 0; then
        error "unable to collate files for package '${pkg_name}'"
        do_exit_on_error
      fi
    if test -f "R/sysdata.rda"; then
      echo "tools:::sysdata2LazyLoadDB(\"R/sysdata.rda\", \"${R_PACKAGE_DIR}/R\")" | \
        R_DEFAULT_PACKAGES=NULL LANG=C "${R_EXE}" --vanilla > /dev/null ; \
      if test ${?} -ne 0; then \
        error "unable to build sysdata DB for package '${pkg_name}'" ; \
        do_exit_on_error ; \
      fi ; \
    fi
    if ${fake}; then
      if test -f NAMESPACE; then
        (echo; echo ".onLoad <- function(list, pkg) NULL") >> \
	  "${R_PACKAGE_DIR}/R/${R_PACKAGE_NAME}"
	sed -e '/useDynLib/d' NAMESPACE > "${R_PACKAGE_DIR}/NAMESPACE"
      else
        (echo; echo ".First.lib <- function(lib, pkg) NULL") >> \
          "${R_PACKAGE_DIR}/R/${R_PACKAGE_NAME}"
      fi
    fi
  fi
  if test -d data; then
    message "data"
    ${MKDIR_P} "${R_PACKAGE_DIR}/data"
    rm -f "${R_PACKAGE_DIR}/data/"*
    cp data/* "${R_PACKAGE_DIR}/data" 2>/dev/null
    rm -f "${R_PACKAGE_DIR}/data/datalist"
    chmod 644 "${R_PACKAGE_DIR}/data/"*
    parse_description_field LazyData
    if test "$value" = "missing" ; then
      thislazy=${lazy_data}
    else
      thislazy=${value}
    fi
    if ${thislazy}; then
      message " moving datasets to lazyload DB"
      ## it is possible that data in a package will make use of the
      ## code in the package, so ensure the package we have just
      ## installed is on the library path.
      echo "invisible(.libPaths(c(\"${lib}\", .libPaths()))); tools:::data2LazyLoadDB(\"${R_PACKAGE_NAME}\", \"${lib}\")" | \
        R_DEFAULT_PACKAGES=NULL LANG=C "${R_EXE}" --vanilla > /dev/null
    if test ${?} -ne 0; then
      error "lazydata failed for package '${pkg_name}'"
      do_exit_on_error
    fi
    else
      if test -n "${use_zip_data}" \
	  && test -n "${R_UNZIPCMD}" \
	  && test -n "${R_ZIPCMD}"; then
	(cd "${R_PACKAGE_DIR}/data";
	  find . -type f -print > filelist
	  ${R_ZIPCMD} -q -m Rdata * -x filelist 00Index)
      fi
    fi
  fi

  if test -d demo && test "${fake}" != "true"; then
    message "demo"
    ${MKDIR_P} "${R_PACKAGE_DIR}/demo"
    rm -f "${R_PACKAGE_DIR}/demo/"*
    cp demo/* "${R_PACKAGE_DIR}/demo" 2>/dev/null
    chmod 644 "${R_PACKAGE_DIR}/demo/"*
    rm -f "${R_PACKAGE_DIR}/demo/00Index"
  fi

  if test -d exec && test "${fake}" != "true"; then
    message "exec"
    ${MKDIR_P} "${R_PACKAGE_DIR}/exec"
    rm -f "${R_PACKAGE_DIR}/exec/"*
    cp exec/* "${R_PACKAGE_DIR}/exec" 2>/dev/null
    chmod 755 "${R_PACKAGE_DIR}/exec/"*
  fi

  if test -d inst && test "${fake}" != "true"; then
    message "inst"
    cp -r inst/* "${R_PACKAGE_DIR}" || \
      (cd inst && ${TAR} cf - . | \
        (cd "${R_PACKAGE_DIR}" && ${TAR} xf -))
  fi

  parse_description_field SaveImage
  if test "$value" != "missing" ; then
    save=${value}
  fi
  case ${save} in
    CHECK)
      if test -r install.R; then
	R_SAVE_IMAGE=true;
      else
	R_SAVE_IMAGE=false;
      fi
      ;;
    *)
      R_SAVE_IMAGE=${save} ;;
  esac
  ## sanity check
  if test ! -d R ; then
    R_SAVE_IMAGE=false
  fi
  export R_SAVE_IMAGE

  if ${R_SAVE_IMAGE}; then
    message "save image"
    ## <NOTE>
    ## We want R to run as quietly as possible when creating the save
    ## image.  But this is tricky: sending options(echo=FALSE) to R via
    ## stdin (as opposed to writing to a file and reading from it)
    ## echoes what we sent before shutting up R, which is not what we
    ## want.  Option '--slave' gets around this but also turns off
    ## saving ... hence we call R with '--slave --save' (in case there
    ## is no namespace).  Argh.
    ## </NOTE>
    save_image_defaults="list(compress=TRUE, safe=FALSE)"
    code_file="${R_PACKAGE_DIR}/R/${pkg_name}"
    rda_file="${R_PACKAGE_DIR}/R/all.rda"
    if test -f NAMESPACE; then
      code_cmd="echo invisible(.libPaths(c(\"${lib}\", .libPaths()))); .getRequiredPackages(); saveNamespaceImage(\"${R_PACKAGE_NAME}\", \"${rda_file}\", \"${lib}\")"
      loader_file=nsrdaload.R
      R_save_args="--vanilla --slave"
    else
      code_cmd="eval cat \"${code_file}\""
      loader_file=firstlib.R
      R_save_args="--vanilla --slave --save"
    fi
    (echo "options(save.image.defaults=${save_image_defaults})"; \
      if test -s R_PROFILE.R; then cat R_PROFILE.R; fi; \
      echo "invisible(.libPaths(c(\"${lib}\", .libPaths()))); .getRequiredPackages()"; \
      ${code_cmd}) | \
        R_DEFAULT_PACKAGES= LANG=C "${R_EXE}" ${R_save_args} ${save_args}
    if test ${?} -ne 0; then
      error "execution of package source for '${pkg_name}' failed"
      do_exit_on_error
    fi
    test -f NAMESPACE || mv .RData "${rda_file}"
    ## we used to install the dumped code but this seems a waste of space
    rm "${code_file}"
    # mv "${code_file}" "${R_PACKAGE_DIR}/R/${pkg_name}.R"
    cat "${R_HOME}/share/R/${loader_file}" > "${code_file}"
    ## If install.R is non-empty, arrange to evaluate the R code it
    ## contains after the package loader.
    if test -s install.R; then
      cat install.R >> "${R_PACKAGE_DIR}/R/${R_PACKAGE_NAME}"
    fi
  fi

  parse_description_field LazyLoad
  if test "$value" = "missing" ; then
    thislazy=${lazy}
  else
    thislazy=${value}
  fi
  if test ! -d R ; then
    thislazy=false
  fi
  ## this test is designed to fail if a loader is used.
  ## beware: the wc format is not portable -- some systems give
  ## size in a left-space-padded first field, and some do not pad. 
  if test "x$thislazy" = "xauto" ; then
    Rcodesize=`cat ${R_PACKAGE_DIR}/R/${pkg_name} | wc -c`
    if test ${Rcodesize} -gt 25000 ; then
      thislazy=true
    else
      thislazy=false
    fi
  fi
  if ${thislazy}; then
    message "preparing package for lazy loading"
    (if test -s R_PROFILE.R; then cat R_PROFILE.R; fi; \
    echo "tools:::makeLazyLoading(\"${R_PACKAGE_NAME}\", \"${lib}\")") | \
      R_DEFAULT_PACKAGES= LC_COLLATE=C "${R_EXE}" --vanilla --slave
    if test ${?} -ne 0; then
      error "lazy loading failed for package '${pkg_name}'"
      do_exit_on_error
    fi
    rm -f ${R_PACKAGE_DIR}/R/all.rda
    if test -s install.R; then
      cat install.R >> "${R_PACKAGE_DIR}/R/${R_PACKAGE_NAME}"
    fi
  fi


  if test -d man; then
    message "help"
    ## Install man sources ...
    Rdfiles=`ls man/*.[Rr]d man/${R_OSTYPE}/*.[Rr]d 2>/dev/null`
    if test -n "${Rdfiles}"; then
      ${MKDIR_P} "${R_PACKAGE_DIR}/man"
      rm -f "${R_PACKAGE_DIR}/man/"*
      for f in ${Rdfiles}; do
	echo "% --- Source file: ${f} ---";
	cat "${f}"; echo; echo '\eof';
      done \
        > "${R_PACKAGE_DIR}/man/${pkg_name}.Rd"
      chmod 644 "${R_PACKAGE_DIR}/man/${pkg_name}.Rd"
      ${R_GZIPCMD} -9 "${R_PACKAGE_DIR}/man/${pkg_name}.Rd"
    fi
    ## Maybe build preformatted help pages ...
    if ${build_help}; then
      if ${NO_PERL5}; then
	echo "${NO_PERL5_MSG}"
      else
	if ${debug}; then
	  echo "DEBUG: build-help ${build_help_opts} ${pkg_dir} ${lib} ${R_PACKAGE_DIR} ${pkg_name}"
	fi
	"${R_CMD}" perl "${R_HOME}/share/perl/build-help.pl" \
	  ${build_help_opts} \
	  "${pkg_dir}" "${lib}" "${R_PACKAGE_DIR}" "${pkg_name}"
	if test ${?} -ne 0; then
	  error "building help failed for package '${pkg_name}'"
	  do_exit_on_error
        fi
      fi
      if test -n "${use_zip_help}" \
	  && test -n "${R_UNZIPCMD}" \
	  && test -n "${R_ZIPCMD}"; then
	(cd "${R_PACKAGE_DIR}"
	  if test -d R-ex; then
	    (cd R-ex; ${R_ZIPCMD} -q -m Rex *.R)
	  fi
          if test -d help; then
	    (cd help; ${R_ZIPCMD} -q -m Rhelp * -x AnIndex);
	  fi
	  if test -d latex; then
            (cd latex; ${R_ZIPCMD} -q -m Rhelp *.tex)
          fi)
      fi
    fi
  else
    echo "No man pages found in package '${pkg_name}'"
  fi

  echo "invisible(.libPaths(c(\"${lib}\", .libPaths()))); tools:::.install_package_indices(\".\", \"${R_PACKAGE_DIR}\")" | \
    R_DEFAULT_PACKAGES=NULL LANG=C "${R_EXE}" --vanilla >/dev/null
  if test ${?} -ne 0; then
    error "installing package indices failed"
    do_exit_on_error
  fi

  ## Install a dump of the parsed NAMESPACE file
  if test -f NAMESPACE && test "${fake}" != "true"; then
      echo "tools:::.install_package_namespace_info(\".\", \"${R_PACKAGE_DIR}\")" | \
        R_DEFAULT_PACKAGES=NULL LANG=C "${R_EXE}" --vanilla >/dev/null
    if test ${?} -ne 0; then
      error "installing namespace metadata failed"
      do_exit_on_error
    fi
  fi

  ## <NOTE>
  ## Remove stuff we should not have installed in the first place.
  ## When installing from a source directory under CVS control, we
  ## should really exclude the CVS subdirs.  The same applies for the
  ## .svn Subversion subdirs.  (In fact, we should also exclude
  ## everything in @file{.Rbuildignore}, but that's another story ...)
  find "${R_PACKAGE_DIR}" -name CVS -type d -prune \
    -exec rm -rf \{\} \; 2>/dev/null
  find "${R_PACKAGE_DIR}" -name .svn -type d -prune \
    -exec rm -rf \{\} \; 2>/dev/null
  ## </NOTE>

  if ${clean}; then
    if test -d src; then
      cd src
      if test -f Makefile; then
        ${MAKE} clean
      else
        rm -rf .libs _libs
        rm -f *.o *.so
      fi
      cd ..
    fi
    if test -x ./cleanup ; then
      ./cleanup
    fi
  fi

  stars="*"
}

### 
### Install a package.

do_install () {
  cd "${1}"
  pkg_dir="${1}"
  pkg_name=`get_dcf_field Package DESCRIPTION`
  ## (The basename of ${pkg_dir} might be different from ${pkg_name},
  ## e.g., when building Debian packages from R packages.)
  if test -z "${pkg_name}"; then
    error "no 'Package' field in 'DESCRIPTION'"
    do_exit_on_error no
  fi

  ## Set R_PACKAGE_DIR here at the top level.  If a version is being
  ## specified, tack that on.
  version=""
  if ${with_package_versions}; then
    version=`get_dcf_field Version DESCRIPTION`
    if test -z "${version}"; then
      error "no 'Version' field in 'DESCRIPTION'"
      do_exit_on_error no
    fi
    R_PACKAGE_NAME="${pkg_name}_${version}"
    R_PACKAGE_DIR="${lib}/${pkg_name}_${version}"
  else
    R_PACKAGE_NAME="${pkg_name}"
    R_PACKAGE_DIR="${lib}/${pkg_name}"
  fi
  export R_PACKAGE_DIR
  export R_PACKAGE_NAME

  depends=`get_dcf_field Depends DESCRIPTION`
  depends=`echo "${depends}" | grep 'R *('`
  if test "${depends}"; then
    depends=`echo "${depends}" | sed 's/.*R *(\([^)]*\)).*/\1/;s/=/= /'`
    dep_operator=`set - ${depends}; echo ${1}`
    dep_version=`set - ${depends}; echo ${2}`
    ## Currently, only operators '<=' and '>=' are supported.  Hence we
    ## check this, and also whether we found a version string.
    ## <FIXME> This thinks 1.9.1 > 1.10.0 </FIXME>
    if (test "${dep_operator}" = "<=" \
           || test "${dep_operator}" = ">=") \
         && test -n "${dep_version}"; then
      dep_ok=`expr ${R_VERSION} ${dep_operator} ${dep_version} `
      if test ${dep_ok} -eq 0; then
        error "This R is version ${R_VERSION}"
        echo "       package '${pkg_name}' needs R ${dep_operator} ${dep_version}" >&2
        do_exit_on_error
      fi
    else
      warning "malformed 'Depends' field in 'DESCRIPTION'"
    fi
  fi

  ${MKDIR_P} "${R_PACKAGE_DIR}" || do_exit_on_error no

  ## Make sure we do not attempt installing to srcdir.
  if test "`cd \"${R_PACKAGE_DIR}\" && ${GETWD}`" = "`${GETWD}`"; then
    error "cannot install to srcdir"
    do_exit_on_error no
  fi

  ## Figure out whether this is a source or binary package.
  if grep "^Built:" DESCRIPTION >/dev/null ; then
    ## If DESCRIPTION has a @samp{Built:} entry this is a binary
    ## package.  This is the right test, but not available for packages
    ## installed prior to 1.4.0 -- but 2.0.0 requires packages to be 
    ## reinstalled.
    is_source_package=false
  else
    is_source_package=true
  fi

  test "${is_first_package}" = no && echo

  if ${is_source_package}; then
    ## This is a source package ... hopefully.
    do_install_source \
      "${pkg_name}" "${R_PACKAGE_DIR}" "${R_PACKAGE_NAME}" "${pkg_dir}"
  else
    ## This is a binary package ... hopefully.
    do_install_binary \
      "${pkg_name}" "${R_PACKAGE_DIR}" "${R_PACKAGE_NAME}"
  fi

  find "${R_PACKAGE_DIR}" -exec chmod a+r \{\} \;

  message "DONE (${pkg_name})"
  is_first_package=no
}

### 
### Exit and cleanup.

do_exit_on_error () {
  remove_R_package_dir=${1-yes}
  for p in ${bundlepkg}; do
    if ${with_package_versions}; then
      p=${p}_${version}
    fi
    pkgdir="${lib}/$p"
    if test "${remove_R_package_dir}" = yes \
	&& test -n "${pkgdir}"; then
      message "Removing '${pkgdir}'"
      rm -rf "${pkgdir}"
    fi
    if test -n "$p" \
	&& test -d "${lockdir}/$p"; then
      message "Restoring previous '${pkgdir}'"
      mv "${lockdir}/$p" "${pkgdir}"
    fi
  done
  do_cleanup
  exit 1
}

do_cleanup () {
  ## Solaris will not remove any directory in the current path
  cd "${startdir}"
  if test -d "${tmpdir}"; then
    rm -rf "${tmpdir}"
  fi

  if test "${is_first_package}" = no; then
    ## Only need to do this in case we successfully installed  at least
    ## *one* package ... well not so sure for bundles.
    cp "${R_HOME}/doc/html/R.css" "${lib}"
    chmod 644 "${lib}/R.css"
    if test "${lib}" = "`cd \"${R_HOME}/library\" && ${GETWD}`"; then
      cat "${R_HOME}"/library/*/CONTENTS \
        > "${R_HOME}"/doc/html/search/index.txt
      if ${build_help} && test "${NO_PERL5}" = "false"; then
        "${R_CMD}" perl "${R_HOME}/share/perl/build-help.pl" --htmllists
      fi
    fi
  fi
  if ${lock}; then
    rm -rf "${lockdir}"
  fi
}

### 
### Main loop.
if ${debug} ; then
    echo "'Rcmd INSTALL': now doing 'eval ... do_install':"
fi
eval "for pkg in ${pkgs}; do do_install \"\${pkg}\"; done"
do_cleanup

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