#!/bin/bash

set -e

FLAVOR=$1
PACKAGE=emacs-jabber

BYTE_COMPILE_OPTIONS="-batch -f batch-byte-compile"
EL_FILES=`echo *.el`
EL_DIR=/usr/share/emacs/site-lisp/$PACKAGE
ELC_DIR=/usr/share/${FLAVOR}/site-lisp/$PACKAGE

if [ "${FLAVOR}" = emacs21 ] || [ "${FLAVOR}" = emacs22 ]  || [ "${FLAVOR}" = emacs23 ] || [ "${FLAVOR}" = emacs-snapshot ]; then
  echo "install/$PACKAGE: byte-compiling for ${FLAVOR}..."

  # Copy the temp .el files
  if [ ! -d ${ELC_DIR} ]; then
    mkdir ${ELC_DIR}
  fi
  cp ${EL_DIR}/*.el ${ELC_DIR}/

  # Byte compile them
  cd ${ELC_DIR}
  ${FLAVOR} -q --no-site-file --batch --eval "(setq load-path (cons \"${ELC_DIR}\" load-path))" -f batch-byte-compile *.el >compilation.log 2>&1 || true
  # remove the redundant .el files
  # presumes that any .el files in the <flavor> dir are trash.
  rm ${ELC_DIR}/*.el
fi

exit 0
