#! /bin/sh
set -e

FLAVOR=$1
PACKAGE="iiimecf"

if [ "X${FLAVOR}" = "X" ]; then
  echo Need argument to determin FLAVOR of emacsen
  exit 1
fi
if [ "X${PACKAGE}" = "X" ]; then
  echo Internal error: need package nameen
  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

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

case "${FLAVOR}" in
  xemacs*)
    ;;
  *)
    echo -n "install/${PACKAGE}: Byte-compiling for ${FLAVOR} ..."
    rm -rf ${ELCDIR}
    install -m 755 -d ${ELCDIR}/tmp
    ( cd ${ELCDIR}/tmp
      cp -r ${ELDIR}/* ${ELCDIR}/tmp
      cat << EOF > path.el
(setq load-path (cons "/usr/share/${FLAVOR}/site-lisp/mule-ucs" load-path))
(setq load-path (cons "." load-path) byte-compile-warnings nil)
EOF
      ${FLAVOR} -q -no-site-file -batch -l path.el -l iiimcf-comp.el > ${ELCDIR}/CompilationLog 2>&1
      mv ${ELCDIR}/tmp/lisp/*.elc ${ELCDIR}/
      cd ${ELCDIR}
      rm -fr ${ELCDIR}/tmp
      for d in "${ELDIR}" "${ELDIR}/lisp"; do
	if [ -d ${d} ]; then
	  cd ${d}
	  for f in *.el; do
	    if [ -f ${ELCDIR}/${f}c ]; then
	      ln -sf ${d}/${f} ${ELCDIR}/${f}
	    fi
	  done
	fi
      done
    )
    gzip -9 ${ELCDIR}/CompilationLog
    chmod 644 ${ELCDIR}/CompilationLog.gz
    if [ -f ${ELCDIR}/iiimcf-sc.elc ]; then
      touch ${ELCDIR}/*.elc
      chmod 644 ${ELCDIR}/*.elc
    fi
    echo " done."
    ;;
esac

exit 0
