#!/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}" = emacs ]; then exit 0; fi
if [ "${FLAVOR}" = emacs19 ]; then exit 0; fi
if [ "${FLAVOR}" = mule2 ]; then exit 0; fi

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

#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"

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

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

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 ${ELDIR}/${f} ${ELCDIR}
done
f=ndnmz.el
if [ -f ${ELDIR}/${f} ]; then
  rm -f ${ELCDIR}/${f} ${ELCDIR}/${f}c
  ln -sf ${ELDIR}/${f} ${ELCDIR}
fi

exit 0
