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

FLAVOR=$1
PACKAGE="x-face-el"

if [ -z "${FLAVOR}" ]; then
  echo Need argument to determin FLAVOR of emacsen
  exit 1
fi
if [ -z "${PACKAGE}" ]; then
  echo Internal error: need package name
  exit 1
fi
if [ "X${FLAVOR}" = Xemacs ]; then exit 0; fi

ELDIR=/usr/share/emacs/site-lisp/${PACKAGE}
ELCDIR=/usr/share/${FLAVOR}/site-lisp/${PACKAGE}
PIXMAPS_DIR=/usr/share/pixmaps/${PACKAGE}
COMPILE_OPT="-batch -q -no-site-file -f batch-byte-compile"

echo -n "install/${PACKAGE}: Byte-compiling for ${FLAVOR} ..."
rm -rf ${ELCDIR}
install -m 755 -d ${ELCDIR}

cp ${ELDIR}/* ${ELCDIR}/
cp ${PIXMAPS_DIR}/* ${ELCDIR}/

case "${FLAVOR}" in
  *xemacs*)
    COMPILE_SRC="x-face.el x-face-xmas.el"
    ;;
  emacs20|emacs1[0-9]|mule2)
    COMPILE_SRC="x-face.el"
    ;;
  *)
    COMPILE_SRC="x-face-e21.el"
    ;;
esac

( cd ${ELCDIR}
  ${FLAVOR} ${COMPILE_OPT} ${COMPILE_SRC} > ${ELCDIR}/CompilationLog 2>&1
)

rm -f ${ELCDIR}/*.el ${ELCDIR}/*.xbm ${ELCDIR}/*.xpm
( cd ${ELDIR}
  for f in *.el; do
    if [ -f ${ELCDIR}/${f}c ]; then
      ln -sf ${ELDIR}/${f} ${ELCDIR}/${f}
    fi
  done
)
gzip -9 ${ELCDIR}/CompilationLog
chmod 644 ${ELCDIR}/CompilationLog.gz
if [ -f "${ELCDIR}/x-face-e21.elc" ] || [ -f "${ELCDIR}/x-face.elc" ]; then
  chmod 644 ${ELCDIR}/*.elc
fi
echo " done."

exit 0
