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

if [ ${FLAVOR} = xemacs21 ]; then
   XEMACS=`ls /usr/bin/xemacs*21*-mule* 2> /dev/null | head -1`
   if [ -z $XEMACS ]; then
	   echo "SKIP: mule-ucs needs Mule support."
	   exit 0
   fi
   EMACS=`basename $XEMACS`
else
   EMACS=${FLAVOR}
fi

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

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

STAMP=${ELCDIR}/compile-stamp

case "${FLAVOR}" in
    emacs)
    ;;
    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
    if [ "${FLAVOR}" = "xemacs21" ];then
	# only Mule-UCS
	(cd ${ELDIR}
	    ${EMACS} -q --no-site-file -batch -l mucs-comp.el > \
		${ELCDIR}/CompilationLog 2>&1
	)
	mv ${ELDIR}/lisp/*.elc ${ELCDIR}/
    else
	# Also jisx0213

        # It's needed (according to Document)
	cp ${ELDIR}/lisp/jisx0213/*.el ${ELCDIR}/

	(cd ${ELDIR}/lisp/jisx0213
	    ${EMACS} -q --no-site-file -batch -l x0213-comp.el > ${ELCDIR}/CompilationLog 2>&1
	)
	mv ${ELDIR}/lisp/*.elc ${ELDIR}/lisp/jisx0213/*.elc ${ELCDIR}/
    fi
    
    gzip -9 ${ELCDIR}/CompilationLog
    echo ${VERSION} > ${STAMP}
    
    echo " done."
    ;;
    *)
    ;;
esac
