#!/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

ELDIR=/usr/share/${PACKAGE}
ELCDIR=/usr/share/${FLAVOR}/site-lisp/${PACKAGE}
TOELDIR=../../../$PACKAGE
ELCSTAMP=$ELCDIR/compile-stamp
PIXMAPS_DIR=/usr/share/pixmaps/${PACKAGE}
COMPILE_OPT="-batch -q -no-site-file -l __myinit.el -f batch-byte-compile"

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

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 "$ELDIR"
FILES=`echo *.el`
cd "$ELCDIR"
cat > __myinit.el << EOF
(setq inhibit-automatic-native-compilation t)
(setq native-comp-deferred-compilation nil)
(setq comp-enable-subr-trampolines nil)
EOF
set +e
${FLAVOR} ${COMPILE_OPT} ${COMPILE_SRC} > CompilationLog 2>&1
if [ $? -ne 0 ]; then cat CompilationLog; exit 1; fi
set -e
rm -f ${ELCDIR}/*.el ${ELCDIR}/*.xbm ${ELCDIR}/*.xpm
for f in $FILES; do
    if [ -f "${f}c" ]; then
        ln -sf "$TOELDIR/$f" .
    fi
done
gzip -9f CompilationLog
if [ -f "${ELCDIR}/x-face-e21.elc" ] || [ -f "${ELCDIR}/x-face.elc" ]; then
  chmod 644 ${ELCDIR}/*.elc
fi
if [ -f /usr/share/emacsen-common/debian-startup.el ] && \
   [ ! -f "/etc/$FLAVOR/site-start.d/70$PACKAGE.el" ] && \
   [ -f "/etc/emacs/site-start.d/70$PACKAGE.el" ] && \
   [ -d "/etc/$FLAVOR/site-start.d" ]; then
    ln -sf "../../emacs/site-start.d/70$PACKAGE.el" "/etc/$FLAVOR/site-start.d/70$PACKAGE.el"
fi
echo " done."

touch "$ELCSTAMP"
exit 0
