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

FLAVOR=$1
VERSION=1.0.0-6
PACKAGE=mell

if [ "X${FLAVOR}" = "X" ]; then
    echo "Need argument to determine FLAVOR of emacsen"
    exit 1
fi
if [ "X${PACKAGE}" = "X" ]; then
    echo "Internal error: need package name"
    exit 1
fi
if [ "X${FLAVOR}" = Xemacs ]; then exit 0; fi
if [ "X${FLAVOR}" = Xemacs19 ]; then exit 0; fi
if [ "X${FLAVOR}" = Xmule2 ]; then exit 0; fi

ELDIR=/usr/share/emacs/site-lisp/${PACKAGE}
SITELISP=/usr/share/${FLAVOR}/site-lisp
ELCDIR=${SITELISP}/${PACKAGE}
APELDIR=${SITELISP}/apel
FLAGS="${SITEFLAG} -q -batch -l ./path.el -l ${APELDIR}/poe.elc -f batch-byte-compile"
STAMP="${ELCDIR}/compile-stamp"

LOG=CompilationLog

echo -n "install/${PACKAGE}: Handling install for emacsen flavor ${FLAVOR}..."
if [ -e "${STAMP}" ]; then
    if [ "X${VERSION}" = X"$(cat ${STAMP})" ]; then
	echo " skipped. (already compiled)" 
	exit 0
    else
	rm -f ${STAMP}
    fi
fi

rm -rf ${ELCDIR}
install -m 755 -d ${ELCDIR}
cd ${ELDIR}
FILES=$(echo *.el)
cp ${FILES} ${ELCDIR}
cd ${ELCDIR}

cat << EOF > path.el
(setq load-path (cons "${APELDIR}/" (cons "." load-path)) byte-compile-warnings nil)
EOF
${FLAVOR} ${FLAGS} ${FILES} > ${LOG} 2>&1
gzip -9 ${LOG}

rm -f *.el path.el

echo "${VERSION}" > ${STAMP}
chmod 644 ${STAMP}

echo " done."
exit 0
