#!/bin/sh
# /usr/lib/emacsen-common/packages/install/mule-ucs
set -e

FLAVOR=$1
VERSION=0.85~0.20061127-3
PACKAGE="mule-ucs"

if [ -z "${FLAVOR}" ]; then
  echo Need argument to determin FLAVOR of emacsen
  exit 1
fi
if [ -z "${PACKAGE}" ]; then
  echo Internal error: need package name
  exit 1
fi
if [ -z "${VERSION}" ]; then
  echo Internal error: need package version
  exit 1
fi
if [ "X${FLAVOR}" = Xemacs ]; then exit 0; fi
if [ "X${FLAVOR}" = Xemacs19 ]; then exit 0; fi
if [ "X${FLAVOR}" = Xmule2 ]; then exit 0; fi

FLAVOR_SUFFIX=`echo ${FLAVOR} | cut -s -d - -f 2`
FLAVORTEST=`echo ${FLAVOR} | sed -e s/^s// | cut -c-6`
if [ "X${FLAVORTEST}" = "Xxemacs" ]; then
  FLAVOR_BODY=`echo ${FLAVOR} | cut -d - -f 1`
  if [ "X${FLAVOR_SUFFIX}" = "Xmule" ]; then
    EMACSEN_MULE=`ls /usr/bin/${FLAVOR_BODY}*-mule* 2>/dev/null | head -1`
    if [ -n "${EMACSEN_MULE}" ]; then
      EMACSEN_MULE=`basename ${EMACSEN_MULE}`
    fi
  elif [ "X${FLAVOR_SUFFIX}" = "Xnomule" ]; then
    exit 0
  else
    EMACSEN_MULE=`ls /usr/bin/${FLAVOR}*-mule* 2>/dev/null | head -1`
    if [ -n "${EMACSEN_MULE}" ]; then
      EMACSEN_MULE=`basename ${EMACSEN_MULE}`
    fi
  fi
else
  EMACSEN_MULE=${FLAVOR}
fi
if [ -z "${EMACSEN_MULE}" ]; then exit 0; fi
EMACSEN=${EMACSEN_MULE}

ELDIR=/usr/share/emacs/site-lisp/${PACKAGE}
ELCDIR=/usr/share/${FLAVOR}/site-lisp/${PACKAGE}
SITELISP=/usr/share/${FLAVOR}/site-lisp
STAMP=${ELCDIR}/compile-stamp

echo -n "install/${PACKAGE}: Byte-compiling for ${FLAVOR} ..."
if [ -e ${STAMP} ]; then
  if [ X"${VERSION}" = X"`cat ${STAMP}`" ]; then
    echo " skipped. (already compiled)"
    exit 0
  else
    rm -f ${STAMP}
  fi
fi
echo -n "This will take a while ..."
rm -rf ${ELCDIR}
install -m 755 -d ${ELCDIR}
cp -r ${ELDIR}/* ${ELCDIR}/

if [ X"${FLAVORTEST}" = Xxemacs ]; then
  (cd ${ELCDIR}
    ${EMACSEN} -q -no-site-file -batch -l mucs-comp.el > ${ELCDIR}/CompilationLog 2>&1
  )
else
  # for GNU Emacs
  (cd ${ELCDIR}/lisp/jisx0213
    ${EMACSEN} -q -no-site-file -batch -l x0213-comp.el > ${ELCDIR}/CompilationLog 2>&1
  )
  (cd ${ELCDIR}/lisp/big5conv
    sed -e 's/(load-file/;;(load-file/' big5-comp.el > big5-comp-without-mucs-comp.el
    ${EMACSEN} -q -no-site-file -batch -l big5-comp-without-mucs-comp.el >> ${ELCDIR}/CompilationLog 2>&1
  )
  if [ -f ${ELCDIR}/lisp/jisx0213/x0213-cdef.elc ]; then
    mv ${ELCDIR}/lisp/jisx0213/*.elc ${ELCDIR}/
    ln -s ${ELDIR}/lisp/jisx0213/jisx0213.el ${ELCDIR}/
    ln -s ${ELDIR}/lisp/jisx0213/egg-sim-jisx0213.el ${ELCDIR}/
  fi
  if [ -f ${ELCDIR}/lisp/big5conv/big5conv.elc ]; then
    mv ${ELCDIR}/lisp/big5conv/*.elc ${ELCDIR}/
  fi
fi
if [ -f ${ELCDIR}/lisp/un-define.elc ]; then
  mv ${ELCDIR}/lisp/*.elc ${ELCDIR}/
fi
rm -rf ${ELCDIR}/lisp ${ELCDIR}/mucs-comp.el
( for d in "${ELDIR}/lisp" "${ELDIR}/lisp/big5conv" "${ELDIR}/lisp/jisx0213" "${ELDIR}/lisp/reldata"; do
    cd ${d}
    for f in *.el; do
      if [ -f ${ELCDIR}/${f}c ]; then
	ln -sf ${d}/${f} ${ELCDIR}/${f}
      fi
    done
  done
)
gzip -9 ${ELCDIR}/CompilationLog
chmod 644 ${ELCDIR}/CompilationLog.gz
if [ -f ${ELCDIR}/un-define.elc ]; then
  chmod 644 ${ELCDIR}/*.elc
  echo "${VERSION}" > ${STAMP}
  chmod 644 ${STAMP}
fi
echo " done."

exit 0
