#! /bin/sh -e
#  /usr/lib/emacsen-common/packages/install/ess

abort_install() {
    echo "Byte compile for ${FLAVOR} is failed"
    echo "Please check /usr/share/${FLAVOR}/site-lisp/${PACKAGE}/CompilationLog"    echo
    exit 1
}

FLAVOR=$1
VERSION="5.2.9"
PACKAGE="ess"

if [ ${FLAVOR} = emacs ]; then exit 0; fi
if [ ${FLAVOR} = emacs19 ]; then exit 0; fi
if [ ${FLAVOR} = mule2 ]; then exit 0; fi

if [ "X${FLAVOR}" = "X" ]; then
    echo Need argument to determin FLAVOR of emacs;
    exit 1
fi

if [ "X${PACKAGE}" = "X" ]; then
    echo Internal error: need package name;
    exit 1;
fi

ELDIR=/usr/share/emacs/site-lisp/${PACKAGE}
ELCDIR=/usr/share/${FLAVOR}/site-lisp/${PACKAGE}

STARTDIR=/etc/${FLAVOR}/site-start.d
STARTFILE="${PACKAGE}-init.el";

SITELISP=/usr/share/${FLAVOR}/site-lisp

STAMP=${ELCDIR}/compile-stamp

echo -n "install/${PACKAGE}: Byte-compiling for ${FLAVOR} ..."

if [ -e ${STAMP} ]; then
        if [ "${VERSION}" = "`cat ${STAMP}`" ]; then
            echo " exited. (already compiled)" 
            exit
        else
            rm -f ${STAMP}
        fi
fi
echo
echo -n "This will take a while ..."

rm -rf ${ELCDIR}
install -m 755 -d ${ELCDIR}

# Byte compile them

FLAVORTEST=`echo $FLAVOR | cut -c-5`
if [ ${FLAVORTEST} = emacs ] ; then
    SITEFLAG="--no-site-file"
else
    SITEFLAG="-no-site-file"
fi
FLAGS="${SITEFLAG} -q -batch -f batch-byte-compile"

# Install-info-altdir does not actually exist. 
# Maybe somebody will write it.
if test -x /usr/sbin/install-info-altdir; then
    echo install/${PACKAGE}: install Info links for ${FLAVOR}
    install-info-altdir --quiet --section "" "" --dirname=${FLAVOR} /usr/info/${PACKAGE}.info.gz
fi

install -m 755 -d ${ELCDIR}
cd ${ELDIR}
FILES=`echo *.el`
cp ${FILES} ${ELCDIR}
mkdir -p ${ELCDIR}/../../etc/ess
(cd etc ; cp -ar * ${ELCDIR}/../../etc/ess )
cd ${ELCDIR}

cat << EOF > path.el
(setq load-path (cons "." load-path) byte-compile-warnings nil)
EOF
${FLAVOR} ${FLAGS} ${FILES} > CompilationLog 2>&1 || abort_install

echo ${VERSION} > ${STAMP}
echo " done."

exit 0;

