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

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



SRCS="mew-attach.el   mew-bq.el      mew-cache.el   \
      mew-complete.el mew-decode.el  mew-demo.el    \
      mew-draft.el    mew-encode.el  mew-env.el     \
      mew-ext.el      mew-fib.el     mew-func.el    \
      mew-header.el   mew-mark.el    mew-message.el \
      mew-mime.el     mew-minibuf.el mew-pgp.el     \
      mew-pick.el     mew-refile.el  mew-scan.el    \
      mew-sort.el     mew-summary.el mew-syntax.el  \
      mew-virtual.el  mew-mule.el                   \
      mew-mule0.el    mew-mule2.el   mew-mule3.el   \
      mew-highlight.el        mew-vars.el           \
      mew-unix.el     mew-win32.el   mew-os2.el     \
      mew-lang-jp.el                                \
      mew-temacs.el   mew-xemacs.el  mew.el"

TEMPFILE=temp.el

el_dir=/usr/share/emacs/site-lisp/mew
elc_dir=/usr/share/${FLAVOR}/site-lisp/mew

byte_compile_options="-batch -q -no-site-file -l ./${TEMPFILE} -f mew-compile"

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

    install -m 755 -d ${elc_dir}
    
    # Copy the temp .el files
    for i in ${SRCS};
    do
        cp ${el_dir}/${i} ${elc_dir}/
    done

    # Byte compile them
    (cd ${elc_dir}
    echo '(setq load-path (cons "." load-path))' > ${TEMPFILE}
    echo '(defun mew-compile () (mapcar (function (lambda (x) (byte-compile-file x))) (list ' >> ${TEMPFILE}
    echo ${SRCS} | sed -e 's/\(mew[^ ]*el\)/"\1"/g' >> ${TEMPFILE}
    echo ')))' >> ${TEMPFILE}

    LOG=`tempfile`
    ${FLAVOR} ${byte_compile_options} > $LOG 2>&1
    mv $LOG ${elc_dir}/install.log
    echo "Compilation log for ${FLAVOR} saved to ${elc_dir}/install.log"
    )

    # remove the redundant .el files
    # presumes that any .el files in the <flavor> dir are trash.
    rm ${elc_dir}/*.el
fi
exit 0;
