#! /bin/sh

# ${RHOME}/etc/INSTALL for installing add-on packages
# Usage:
#	R INSTALL [(+|-)debug] [-help/+help] [-html/+html] [-latex/+latex]
#                 [+docs/-docs] pkg [lib]

IHELP=y
IHTML=y
ILATEX=y
if [ "$1" = "+debug" ]; then shift; DEBUG=y; fi
if [ "$1" = "-debug" ]; then shift; DEBUG=y; fi
if [ "$1" = "+help" ]; then shift; IHELP=y; fi
if [ "$1" = "-help" ]; then shift; IHELP=;  fi
if [ "$1" = "+html" ]; then shift; IHTML=y; fi
if [ "$1" = "-html" ]; then shift; IHTML=;  fi
if [ "$1" = "+latex" ]; then shift; ILATEX=y; fi
if [ "$1" = "-latex" ]; then shift; ILATEX=;  fi

if [ "$1" = "-docs" ]
then
    shift
    IHELP=""
    IHTML=""
    ILATEX=""
fi

if [ "$1" = "+docs" ]
then
    shift
    IHELP="y"
    IHTML="y"
    ILATEX="y"
fi

if [ "$1" ]
then
    pkg=$1
    if [ ! -d ${pkg} ]
    then
	echo "Package \`${pkg}' does not exist"
	exit 1
    fi
else
    echo "usage:  R INSTALL <options> pkg [lib]"
    exit 1
fi

lib=${2:-${RHOME}/library}

test -d ${lib} || mkdir ${lib} || exit 1;
lib=`cd ${lib}; pwd`

test -d ${lib}/${pkg} || mkdir ${lib}/${pkg} || exit 1;

src=src-c

cd ${pkg}
pkg=`basename ${pkg}`

echo "Installing package \`${pkg}' ..."
##DEBUG me: set -x -v

if test -d ${src}
then
    echo "libs"
    (cd ${src}; make CC="gcc" CFLAGS="-g -fpic -I${RHOME}/include" SHLIBLDFLAGS="-shared" FC="" FFLAGS=" -fpic")
    test -d ${lib}/${pkg}/libs || mkdir ${lib}/${pkg}/libs
    cp ${src}/*.so ${lib}/${pkg}/libs
fi

if test -d R ; then dir=R ;elif test -d funs; then dir=funs; fi
if test "$dir"
then
 echo "${dir}"
 test -d ${lib}/${pkg}/R || mkdir ${lib}/${pkg}/R
 cat `ls ${dir}/[a-z]* | sed '/CVS/d; /~$/d; /\.orig$/d; /\.bak$/d;
		/^core$/d; /system.*/d'` > ${lib}/${pkg}/R/${pkg}
fi

if test -d data
then
    echo "data"
    test -d ${lib}/${pkg}/data || mkdir ${lib}/${pkg}/data
    cp data/* ${lib}/${pkg}/data
fi

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

if [ -f INDEX ]
then
    cp INDEX ${lib}/${pkg}
fi
if [ -f TITLE ]
then
    cp TITLE ${lib}/${pkg}
fi
(cd ${lib}; cat */TITLE > LibIndex 2> /dev/null)

echo "DONE"


# ----------- Installing help pages ------

NO_PERL5=""


if [ "$IHELP" -o "$IHTML" -o "$ILATEX" ]
then
	echo
else
	exit 0
fi

if test -d man
then
    echo "Installing documentation of package \`${pkg}' ..."
else
    echo "No man pages found in package \`${pkg}'"
    exit 0
fi

if [ "$NO_PERL5" ]
then
    echo "*** You requested to format and install the help pages of ${pkg}."
    echo "*** Formatting R help pages needs perl version 5, which seems not to be"
    echo "*** installed on your system or is not in your path. Please install"
    echo "*** either perl 5 on your system and re-configure R or get"
    echo "*** pre-formatted help pages from the nearest CRAN server."
    echo "*** The CRAN master site can be found at"
    echo "***    ftp://ftp.ci.tuwien.ac.at/pub/R"
    echo "***    http://www.ci.tuwien.ac.at/R"
    echo

    exit 0
fi

if [ "$DEBUG" ]
then BUILDHELPOPTS="--debug"
else BUILDHELPOPTS=""
fi

if [ "$IHELP" ]
then
    BUILDHELPOPTS="${BUILDHELPOPTS} --nroff"
fi

if [ "$IHTML" ]
then
    BUILDHELPOPTS="${BUILDHELPOPTS} --html"
fi

if [ "$ILATEX" ]
then
    BUILDHELPOPTS="${BUILDHELPOPTS} --latex"
fi

if [ "$DEBUG" ]
then echo "now '${RHOME}/etc/build-help ${BUILDHELPOPTS} ../${pkg} ${lib}'"
fi
${RHOME}/etc/build-help ${BUILDHELPOPTS} ../${pkg} ${lib}

echo "DONE (INSTALL)"


