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

set -e

FLAVOR=$1
VERSION="0.84"
PACKAGE="Mule-UCS"

if [ "X${FLAVOR}" = "X" ]; then
    echo Need argument to determin FLAVOR of emacs;
    exit 1
fi

if [ "X${PACKAGE}" = "X" ]; then
    echo Internal error: need package name;
    exit 1;
fi

ELDIR=/usr/share/emacs/site-lisp/${PACKAGE}
ELCDIR=/usr/share/${FLAVOR}/site-lisp/${PACKAGE}

STARTDIR=/etc/${FLAVOR}/site-start.d
STARTFILE="${PACKAGE}-init.el";

SITELISP=/usr/share/${FLAVOR}/site-lisp

STAMP=${ELCDIR}/compile-stamp
case "${FLAVOR}" in
    emacs)
    ;;
    xemacs-21.1.14)
    ;;
    # emacs20|emacs21|xemacs21)
    *)
    echo -n "install/${PACKAGE}: Byte-compiling for ${FLAVOR} ..."

    if [ -e ${STAMP} ]; then
        if [ "${VERSION}" = "`cat ${STAMP}`" ]; then
            echo " exited. (already compiled)" 
            exit
        else
            rm -f ${STAMP}
        fi
    fi
       echo
       echo -n "This will take a while ..."

       rm -rf ${ELCDIR}
    install -m 755 -d ${ELCDIR}

    # Byte compile them
    case "${FLAVOR}" in
        xemacs-*)
               # only Mule-UCS
               (cd ${ELDIR}
                       ${FLAVOR} --vanilla -batch -l mucs-comp.el > \
                               ${ELCDIR}/CompilationLog 2>&1
               )
               mv ${ELDIR}/lisp/*.elc ${ELCDIR}/
               ;;
        emacs*)
               # Also jisx0213
               (cd ${ELDIR}/lisp/jisx0213
                       ${FLAVOR} -q --no-site-file -batch -l x0213-comp.el > ${ELCDIR}/CompilationLog 2>&1
               )
               mv ${ELDIR}/lisp/*.elc ${ELDIR}/lisp/jisx0213/*.elc ${ELCDIR}/

               # It's needed (according to Document)
               cp -a ${ELDIR}/lisp/jisx0213/*.el ${ELCDIR}/
               ;;
    esac
               rm -f ${ELDIR}/lisp/*.elc >> ${ELCDIR}/CompilationLog 2>&1
               gzip -9 ${ELCDIR}/CompilationLog
               echo ${VERSION} > ${STAMP}

               #cp ${ELDIR}/${STARTFILE} ${STARTDIR}/40${STARTFILE};
               
               echo " done."
               ;;
    *)
               ;;
esac

exit 0;

