#! /bin/sh

# ${RHOME}/bin/INSTALL for installing add-on packages
# Usage:
#	R INSTALL [options] [-l lib] pkg_1 ... pkg_n

# Generated automatically from INSTALL.in by configure.

VERSION="0.1-4"
USAGE_MSG="Usage:  R INSTALL [options] [-l lib] pkg_1 ... pkg_n"

CLEAN=false
DEBUG=false
BUILD_TEXT=true
BUILD_HTML=true
BUILD_LATEX=true
BUILD_HELP=true
BUILD_HELP_OPTS=
BUILD_EXAMPLE=true
SHLIB_OPTS=

USE_ZIP=

INSTALL='/usr/bin/install -c -o root -g wheel'

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 pre-formatted help pages from the nearest CRAN server.
*** The CRAN master site can be found at
***    http://www.ci.tuwien.ac.at/R
***    ftp://ftp.ci.tuwien.ac.at/pub/R"

PKGS=
lib=${RHOME}/library

MAKE=${MAKE-make}

umask 022

while test -n "${1}"; do
  case ${1} in
    -h|--help|-\?)
      echo "${USAGE_MSG}"; exit 0 ;;
    -V|--version)
      echo "${VERSION}"; exit 0 ;;
    -c|--clean)
      CLEAN=true ;;
    --debug)
      DEBUG=true; SHLIB_OPTS="${SHLIB_OPTS} --debug" ;;
    --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=true ;;
    -l|--library)
      lib=${2}; shift ;;
    *)
      if test -d ${1}; then
	PKGS="${PKGS} `cd ${1}; pwd`"
      else
	echo "WARNING: package \`${1}' does not exist"
      fi
      ;;
  esac
  shift
done

if test -z "${PKGS}"; then
  echo "${USAGE_MSG}"
  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} --nroff"
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

for pkg in ${PKGS}; do
  cd ${pkg}
  pkg=`basename ${pkg}`

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

  if test -x ./configure ; then
    ./configure
  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;
	${MAKE} -f ${RHOME}/etc/Makeconf -f Makefile \
	  && cp *.so ${lib}/${pkg}/libs; \
	if ${CLEAN}; then ${MAKE} clean; fi)
    else
      (cd src;
	sh ${RHOME}/bin/SHLIB ${SHLIB_OPTS} \
	   -o ${lib}/${pkg}/libs/${pkg}.so *.[cf]; \
	if ${CLEAN}; then rm -f *.o; fi)
    fi || {
      echo "ERROR: Compilation failed for package \`${pkg}'"
      exit 4
    }
    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]` > ${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/*
  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

  if test -f INDEX; then
    cp INDEX ${lib}/${pkg}
  fi
  if test -f TITLE; then
    cp TITLE ${lib}/${pkg}
  fi
  if test -f DESCRIPTION; then
    cp DESCRIPTION ${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 "  ${RHOME}/bin/build-help ${BUILD_HELP_OPTS} ../${pkg} ${lib}"
	fi
	${RHOME}/bin/build-help ${BUILD_HELP_OPTS} ../${pkg} ${lib}
	${RHOME}/bin/Rd2contents ../${pkg} > ${lib}/${pkg}/CONTENTS
	if test ${lib} = ${RHOME}/library; then 
	  ${RHOME}/bin/build-help --htmllists
	  cat ${RHOME}/library/*/CONTENTS \
	    > ${RHOME}/doc/html/search/index.txt
	fi	
      fi
      if test -n "${USE_ZIP}" -a -n "${R_UNZIPCMD}" -a -n "${R_ZIPCMD}"; then
	cd ${lib}/${pkg}
	if test -d R-ex; then
	  (cd R-ex; ${R_ZIPCMD} -m Rhelp *.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

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

echo "DONE (INSTALL)"

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