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

FLAVOR=$1
PACKAGE=mhc

case $FLAVOR in
    emacs|emacs19|mule2)
    exit 0
    ;;
    *xemacs*)
    EMACS=`/bin/ls -1 /usr/bin/$FLAVOR*-mule* 2>/dev/null | head -1`
    if [ -z "$EMACS" ]; then
	echo "install/$PACKAGE: $FLAVOR MULE not found, skipped"
	exit 0
    fi
    ;;
    *)
    EMACS=$FLAVOR
    ;;
esac

ELCDIR=/usr/share/$FLAVOR/site-lisp/$PACKAGE
ELDIR=/usr/share/emacs/site-lisp/$PACKAGE
TOELDIR=../../../emacs/site-lisp/$PACKAGE
ELCSTAMP=$ELCDIR/compile-stamp
if [ -f "$ELCSTAMP" ]; then
    echo "install/$PACKAGE: already byte-compiled for $FLAVOR, skipped"
    exit 0
fi

LOG=`mktemp -t elc.XXXXXXXXXXXX`
chmod 644 "$LOG"
echo "install/$PACKAGE: byte-compiling for $FLAVOR, logged in $LOG"

cd "$ELDIR"
FILES=`echo *.el`
if [ ! -d "$ELCDIR" ]; then
    mkdir "$ELCDIR"
    chmod 755 "$ELCDIR"
fi
cp Makefile MHC-MK "$ELCDIR"
cd "$ELCDIR"
rm -f *.elc __myinit.el
for f in $FILES; do
    ln -sf "$TOELDIR/$f" .
done

cat > __myinit.el << EOF
(setq load-path (cons "." load-path))
(setq byte-compile-warnings nil)
EOF

INST_OPTS=--with-addpath=.
MEW_OPTS=
WL_OPTS=
GNUS_OPTS=
if [ -d "/usr/share/$FLAVOR/site-lisp/mew" ]; then
    INST_OPTS=$INST_OPTS:/usr/share/$FLAVOR/site-lisp/mew
    MEW_OPTS=--with-mew
fi
if [ -d "/usr/share/$FLAVOR/site-lisp/wl" ]; then
    INST_OPTS=$INST_OPTS:/usr/share/$FLAVOR/site-lisp/wl:/usr/share/$FLAVOR/site-lisp/semi:/usr/share/$FLAVOR/site-lisp/flim:/usr/share/$FLAVOR/site-lisp/apel
    WL_OPTS=--with-wl
fi
FLAGS="-q -no-site-file -batch -l __myinit.el -l MHC-MK"
echo make EMACS="$EMACS" FLAGS="$FLAGS" OPTS="$INST_OPTS $MEW_OPTS $WL_OPTS $GNUS_OPTS" elc >> "$LOG" 2>&1
make EMACS="$EMACS" FLAGS="$FLAGS" OPTS="$INST_OPTS $MEW_OPTS $WL_OPTS $GNUS_OPTS" elc >> "$LOG" 2>&1

echo "install/$PACKAGE: deleting $LOG"
rm -f "$LOG" __myinit.el* Makefile MHC-MK mhc-cmail.elc mhc-gnus.elc nnmhc.elc

touch "$ELCSTAMP"
exit 0
