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

FLAVOR=$1
PACKAGE=mhc

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

echo install/${PACKAGE}: Handling install for emacsen flavor ${FLAVOR}

EMACS=$FLAVOR
FLAVORTEST=`echo $FLAVOR | cut -c-6`
if [ ${FLAVORTEST} = xemacs ] ; then
    SITEFLAG="-no-site-file"
    set +e
    EMACS=`/bin/ls -1 /usr/bin/${FLAVOR}*-mule* 2>/dev/null| head -1 `
    if test -x "$EMACS"; then
	:
    else
	echo Warning: Mule enabled ${FLAVOR} not found, skipped
	exit 0
    fi
    set -e
else
    SITEFLAG="--no-site-file"
fi
# FLAGS="${SITEFLAG} -q -batch -f batch-byte-compile"
FLAGS="${SITEFLAG} -q -batch -l MHC-MK"

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

install -m 755 -d ${ELCDIR}
cd ${ELDIR}
FILES=`echo *.el`
rm -f ${ELCDIR}/*.el
cp ${FILES} Makefile MHC-MK ${ELCDIR}
cd ${ELCDIR}

INST_OPTS=--with-addpath=.
MEW_OPTS=
WL_OPTS=
GNUS_OPTS=
if test -d /usr/share/${FLAVOR}/site-lisp/mew; then
    INST_OPTS=${INST_OPTS}:/usr/share/${FLAVOR}/site-lisp/mew
    MEW_OPTS=--with-mew
fi
if test -d /usr/share/${FLAVOR}/site-lisp/wl; then
    INST_OPTS=${INST_OPTS}:/usr/share/${FLAVOR}/site-lisp/wl:/usr/share/${FLAVOR}/site-lisp/apel:/usr/share/${FLAVOR}/site-lisp/flim:/usr/share/${FLAVOR}/site-lisp/semi
    WL_OPTS=--with-wl
fi
if test -d /usr/share/${FLAVOR}/site-lisp/t-gnus; then
    INST_OPTS=${INST_OPTS}:/usr/share/${FLAVOR}/site-lisp/t-gnus:/usr/share/${FLAVOR}/site-lisp/apel:/usr/share/${FLAVOR}/site-lisp/flim:/usr/share/${FLAVOR}/site-lisp/semi
    GNUS_OPTS=--with-gnus
elif test -d /usr/share/${FLAVOR}/site-lisp/gnus; then
    INST_OPTS=${INST_OPTS}:/usr/share/${FLAVOR}/site-lisp/gnus
    GNUS_OPTS=--with-gnus
fi
test -f mhc.elc && rm -f mhc.elc
make -f Makefile EMACS=${EMACS} FLAGS="${FLAGS}" OPTS="${INST_OPTS} ${MEW_OPTS} ${WL_OPTS} ${GNUS_OPTS}" elc > CompilationLog 2>&1
rm -f *.el Makefile MHC-MK CompilationLog.gz
rm -f mhc-bm.elc mhc-cmail.elc mhc-gnus.elc mhc-mime.elc nnmhc.elc
for f in ${FILES}; do
    ln -fs ${ELDIR}/$f
done
gzip -9 CompilationLog

exit 0
