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

FLAVOR=$1
PACKAGE=lookup-el
VERSION=1.4

if [ -z "${FLAVOR}" ]; then exit 0; fi
if [ "${FLAVOR}" = emacs19 ]; then exit 0; fi
if [ "${FLAVOR}" = mule2 ]; then exit 0; fi

#FLAGS="-no-site-file -q -batch -l path.el -f batch-byte-compile"
FLAGS="-no-site-file -q -batch -l path.el -l lookup-compile.el"

ELCDIR=/usr/share/$FLAVOR/site-lisp/$PACKAGE
ELDIR=/usr/share/$PACKAGE
TOELDIR=../../../$PACKAGE
ELCSTAMP=$ELCDIR/compile-stamp

FLAVORTEST=`echo ${FLAVOR} | sed -e s/^s// | cut -c-6`
if [ "X${FLAVORTEST}" = "Xxemacs" ]; then
  EMACSEN_MULE=`ls /usr/bin/${FLAVOR}*-mule* 2>/dev/null | head -1`
  if [ -n "${EMACSEN_MULE}" ]; then
    EMACSEN_MULE=`basename ${EMACSEN_MULE}`
  fi
else
  EMACSEN_MULE=${FLAVOR}
fi
if [ -z "${EMACSEN_MULE}" ]; then exit 0; fi

if [ -f "$ELCSTAMP" ]; then
    if [ -z "`find "$ELDIR/lookup.el" -newer "$ELCSTAMP"`" ]; then
	echo "install/$PACKAGE: already byte-compiled for $FLAVOR, skipped"
	exit 0
    fi
    rm -f "$ELCSTAMP"
fi

echo "install/$PACKAGE: byte-compiling for $FLAVOR"

install -m 755 -d ${ELCDIR}
rm -f ${ELCDIR}/*.el

cd ${ELDIR}
# Don't byte-compile lookup-compile.el which causes an infinite loop.
# Don't byte-compile ndnmz.el which depends on flim.
FILES="`echo *.el | sed -e s/lookup-compile.el// -e s/ndnmz.el//`"
cp ${FILES} lookup-compile.el ${ELCDIR}
cp /usr/share/pixmaps/${PACKAGE}/*.xbm ${ELCDIR}
cp /usr/share/pixmaps/${PACKAGE}/*.xpm ${ELCDIR}

cd ${ELCDIR}
cat << EOF > path.el
(setq load-path (cons "/usr/share/${FLAVOR}/site-lisp/bitmap-mule" load-path))
(setq load-path (cons "." load-path))
(setq byte-compile-warnings nil)
EOF
${EMACSEN_MULE} ${FLAGS} ${FILES} > CompilationLog 2>&1
gzip -9 -f CompilationLog

rm -f *.el *xbm *xpm
for f in ${FILES}; do
  ln -sf "$TOELDIR/$f" "$ELCDIR"
done
f=ndnmz.el
if [ -f ${ELDIR}/${f} ]; then
  rm -f ${ELCDIR}/${f} ${ELCDIR}/${f}c
  ln -sf "$TOELDIR/$f" "$ELCDIR"
fi

if [ -f /usr/share/emacsen-common/debian-startup.el ] && \
   [ ! -f "/etc/$FLAVOR/site-start.d/50$PACKAGE.el" ] && \
   [ -f "/etc/emacs/site-start.d/50$PACKAGE.el" ] && \
   [ -d "/etc/$FLAVOR/site-start.d" ]; then
    ln -sf "../../emacs/site-start.d/50$PACKAGE.el" "/etc/$FLAVOR/site-start.d/50$PACKAGE.el"
fi

touch "$ELCSTAMP"
exit 0
