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

FLAVOR=$1
PACKAGE=mew-beta
PKGSNAME=mew

case $FLAVOR in
    emacs|*xemacs*|emacs20|emacs19|mule2)
    exit 0
    ;;
esac

ELCDIR=/usr/share/$FLAVOR/site-lisp/$PKGSNAME
ELDIR=/usr/share/emacs/site-lisp/$PKGSNAME
TOELDIR=../../../emacs/site-lisp/$PKGSNAME
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"
LINKS=`echo *.el etc`
if [ -d contrib ]; then
    LINKS="$LINKS "`echo contrib/*.el`
fi
if [ ! -d "$ELCDIR" ]; then
    mkdir "$ELCDIR"
    chmod 755 "$ELCDIR"
fi
cd "$ELCDIR"
rm -f *.elc  __myinit.el
for f in $LINKS; do
    ln -sf "$TOELDIR/$f" .
done
FILES=`/bin/ls -1 *.el | grep -v ^mew-nmz`

cat > __myinit.el << EOF
(setq load-path (cons "." load-path))
(setq byte-compile-warnings nil)
(setq mew-compiling t)
(require 'cl)
EOF

FLAGS="-q -no-site-file -batch -l __myinit.el -f batch-byte-compile"
echo "$FLAVOR" $FLAGS $FILES >> "$LOG"
"$FLAVOR" $FLAGS $FILES >> "$LOG" 2>&1
chmod 644 *.elc

echo "install/$PACKAGE: deleting $LOG"
rm -f "$LOG" __myinit.el*

echo "install/$PACKAGE: byte-compiling for reverse dependency"
pkgs=
for p in mhc w3m-el:w3m w3m-el-snapshot:w3m; do
    pkg=`echo $p | cut -d : -f 1`
    sname=`echo $p | cut -d : -f 2`
    dir=/usr/share/$FLAVOR/site-lisp/$sname
    if [ -n "$sname" ] && [ -d "$dir" ] && [ -f "/usr/lib/emacsen-common/packages/install/$pkg" ]; then
	rm -f "$dir"/*.elc "$dir"/*/*.elc "$dir"/*-stamp
	pkgs="$pkg $pkgs"
    fi
done
for pkg in $pkgs; do
    "/usr/lib/emacsen-common/packages/install/$pkg" "$FLAVOR"
done

touch "$ELCSTAMP"
exit 0
