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

# @configure_input@

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

usage="Usage: R INSTALL [options] pkgs

Install the add-on packages specified by pkgs into the default R library
tree (${R_HOME}/library) or the tree specified via \`--library'.  The
elements of pkgs can be relative or absolute paths to directories with
the package (bundle) sources, or to gzipped package \`tar' archives.

Options:
  -c, --clean		remove all files created during installation
  -d, --debug		turn on shell and build-help debugging
  -h, --help		print short help message and exit
  -l, --library=LIB	install packages to library tree LIB
      --no-docs		do not build and install documentation
      --use-zip-date	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'
  -v, --version		print version info and exit

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

R_VERSION='@VERSION@'
INSTALL='@INSTALL@'
MAKE=${MAKE-@MAKE@}
NO_PERL5=@NO_PERL5@
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/"
tmpdir="${TMPDIR:-/tmp}/R.INSTALL.$$"

umask 022

# Get one field including all continuation lines from a DCF file.
get_dcf_field () {
  ws="[ 	]"		# space and tab
  sed -n "/^${1}:/,/^[^ ]/{p;}" ${2} | \
    sed -n "/^${1}:/{s/^${1}:${ws}*//;p;}
            /^${ws}/{s/^${ws}*//;p;}"
}
  

# get the full path names to all packages contained in $1; modzifies pkgs
get_packages () {
  if grep -s "^Contains:" "${1}/DESCRIPTION"; then
    bundlepkg=`get_dcf_field Contains "${1}/DESCRIPTION"`
    for p in ${bundlepkg}; do
      pkgs="${pkgs} \"`cd "${1}/${p}"; pwd`\""
      if test -f "${1}/${p}/DESCRIPTION.in"; then
        cat "${1}/${p}/DESCRIPTION.in"  > "${1}/${p}/DESCRIPTION"
        grep -v "^Contains:" "${1}/DESCRIPTION" >> "${1}/${p}/DESCRIPTION"
      fi
    done
  else 
    pkgs="${pkgs} \"`cd "${1}"; pwd`\""
  fi
}

lib=${R_HOME}/library
pkgs=
clean=false
debug=false
error=false
build_text=true
build_html=true
build_latex=true
build_example=true
build_help=true
build_help_opts=
use_zip_data=
use_zip_help=

while test -n "${1}"; do
  case ${1} in
    -h|--help)
      echo "${usage}"; exit 0 ;;
    -v|--version)
      echo "${version}"; exit 0 ;;
    -c|--clean)
      clean=true ;;
    -d|--debug)
      debug=true ;;
    --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/[^=]*=//'` ;;
    *)
      if test -f "${1}"; then
        mkdir -p ${tmpdir}
	pkgname=`basename "${1}"`
	pkgname=`echo "${pkgname}" | sed 's/_.*//'`
	gzip -dc "${1}" | (cd ${tmpdir} && tar -xf -)
	if test -d "${tmpdir}/${pkgname}"; then
	  get_packages "${tmpdir}/${pkgname}"
	else
	  echo "ERROR: cannot extract package from ${1}"
	  exit 1
	fi    
      elif test -d "${1}"; then
        get_packages "${1}"
      else
	echo "WARNING: package \`${1}' does not exist"
      fi
      ;;
  esac
  shift
done

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

if test -d ${lib} -a -w ${lib} || mkdir ${lib} 2> /dev/null; then
  lib=`cd ${lib}; pwd`
else
  echo "ERROR: cannot write to or create directory \`${lib}'"
  exit 2
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

do_install () {
  cd "${1}"
  pkg=`basename "${1}"`

  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}`
    dep_ok=`expr ${R_VERSION} ${dep_operator} ${dep_version} `
    if test ${dep_ok} -eq 0; then
      echo "ERROR: This R is version ${R_VERSION}"
      echo "       package \`${pkg}' depends on R ${dep_version}"
      exit 1;
    fi
  fi

  mkdir -p ${lib}/${pkg} || exit 3
  echo "Installing package \`${pkg}' ..."

  if test -x ./configure ; then
    ./configure ${configure_args}
    if test ${?} -ne 0; then
      echo "ERROR: Configuration failed for package \`${pkg}'"
      exit 4
    fi
  fi

  if test -d src; then
    echo " libs"
    if ${debug}; then set -x; fi
    mkdir -p ${lib}/${pkg}/libs
    if test -f src/Makefile; then
      (cd src;
        makefiles="-f ${R_HOME}/etc/Makeconf -f Makefile"
	if test -r Makevars; then
	  makefiles="-f Makevars ${makefiles}"
	fi
	${MAKE} ${makefiles} \
	  && cp *.@SHLIBEXT@ ${lib}/${pkg}/libs \
	  || error=true; \
	if ${clean}; then ${MAKE} clean; fi)
    else
      (cd src;
        srcs=`ls *.[cf] *.cc 2>/dev/null`
	sh ${R_HOME}/bin/SHLIB \
	    -o ${lib}/${pkg}/libs/${pkg}.@SHLIBEXT@ ${srcs} \
	  || error=true; \
	if ${clean}; then rm -f *.o; fi)
    fi
    if ${error}; then
      echo "ERROR: Compilation failed for package \`${pkg}'"
      exit 5
    fi
    if ${debug}; then set +x; fi
  fi

  if test -d R; then dir=R; elif test -d funs; then dir=funs; fi
  if test "${dir}"; then
    echo " ${dir}"
    mkdir -p ${lib}/${pkg}/R
    cat `ls ${dir}/*.[RSqrs] ${dir}/unix/*.[RSqrs] 2>/dev/null` \
      > ${lib}/${pkg}/R/${pkg}
  fi
  dir=

  if test -d data; then
    echo " data"
    mkdir -p ${lib}/${pkg}/data
    cp data/* ${lib}/${pkg}/data
    chmod 644 ${lib}/${pkg}/data/*
    if test -n "${use_zip_data}" \
        -a -n "${R_UNZIPCMD}" \
        -a -n "${R_ZIPCMD}"; then
      (cd ${lib}/${pkg}/data;
        find . -type f -print > filelist
        ${R_ZIPCMD} -m Rdata * -x filelist 00Index)
    fi
  fi

  if test -d exec; then
    echo " exec"
    mkdir -p ${lib}/${pkg}/exec
    cp exec/* ${lib}/${pkg}/exec
    chmod 755 ${lib}/${pkg}/exec/*
  fi

  if test -d inst; then
    echo " inst"
    cp -r inst/* ${lib}/${pkg}
  fi

  for f in COPYING DESCRIPTION INDEX; do
    if test -f ${f}; then cp ${f} ${lib}/${pkg}; fi
  done
  title=`grep -s "^Title:" DESCRIPTION | sed "s/^Title:[ \t]*//"`
  if test "${title}"; then
      ${R_HOME}/bin/maketitle > ${lib}/${pkg}/TITLE
  elif test -f TITLE; then
    cp TITLE ${lib}/${pkg}
  fi
  (cd ${lib}; cat */TITLE > LibIndex 2> /dev/null)

  if test -d man; then
    echo " help"
    if ${build_help}; then
      if ${NO_PERL5}; then
	echo "${NO_PERL5_MSG}"
      else
	if ${debug}; then
	  echo "  ${R_HOME}/bin/build-help ${BUILD_HELP_OPTS} ../${pkg} ${lib}"
	fi
	${R_HOME}/bin/build-help ${build_help_opts} ../${pkg} ${lib}
	${R_HOME}/bin/Rd2contents ../${pkg} > ${lib}/${pkg}/CONTENTS
	if test ${lib} = ${R_HOME}/library; then 
	  ${R_HOME}/bin/build-help --htmllists
	  cat ${R_HOME}/library/*/CONTENTS \
	    > ${R_HOME}/doc/html/search/index.txt
	fi	
      fi
      if test -n "${use_zip_help}" \
	  -a -n "${R_UNZIPCMD}" \
	  -a -n "${R_ZIPCMD}"; then
	cd ${lib}/${pkg}
	if test -d R-ex; then
	  (cd R-ex; ${R_ZIPCMD} -m Rex *.R)
	fi
	## NOT YET:
	## if test -d latex; then
	##   (cd latex; ${R_ZIPCMD} -m Rhelp *.tex)
	## fi
	if test -d help; then
	  (cd help; ${R_ZIPCMD} -m Rhelp * -x 00Titles AnIndex);
	fi
      fi
    fi
  else
    echo "  No man pages found in package \`${pkg}'"
  fi

  if ${clean}; then
    if test -x ./cleanup ; then
      ./cleanup
    fi
  fi

  chmod -R a+r ${lib}/${pkg}

  echo " DONE (${pkg})"
  echo
}

eval "for pkg in ${pkgs}; do do_install \"\${pkg}\"; done"

if test -d "${tmpdir}"; then
  cd /
  rm -rf "${tmpdir}"
fi

echo "DONE (INSTALL)"

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