#!/bin/sh -e
# /usr/lib/emacsen-common/packages/install/hyperlatex
# [ This particular script hasn't been tested, so be careful. ]
set -e

FLAVOR=$1
echo install/hyperlatex: Handling install of emacsen flavor ${FLAVOR}

byte_compile_options="-batch -q -l hyperlatex.el -f batch-byte-compile"
el_files="hyperlatex.el 42hyperlatex.el"
el_dir=/usr/share/emacs/site-lisp/hyperlatex/
elc_dir=/usr/share/${FLAVOR}/site-lisp/hyperlatex/
el_path_list=`echo ${el_files} | tr ' ' '\n' | perl -pe "s|^|${el_dir}|o"`
elc_path_list=`echo ${el_files} | tr ' ' '\n' | perl -pe "s|^|${elc_dir}|o"`

if [ ${FLAVOR} != emacs ]; then
  echo install/hyperlatex: byte-compiling for ${FLAVOR}

  if [ ! -d ${elc_dir} ]; then
    mkdir ${elc_dir}
  fi
  # Copy the temp .el files
  cp ${el_path_list} ${elc_dir}

  # Byte compile them
  (cd ${elc_dir} \
   && ${FLAVOR} ${byte_compile_options} ${el_files} 2> /dev/null)
  #${FLAVOR} ${byte_compile_options} ${elc_path_list}

  # remove the redundant .el files
  # presumes that any .el files in the <flavor> dir are trash.
  rm ${elc_dir}/*.el
  mv -f ${elc_dir}/42hyperlatex.elc /etc/${FLAVOR}/site-start.d/
fi
exit 0;
