#!/bin/sh
# /usr/lib/emacsen-common/packages/install/mew-beta
set -e

abort_install() {
  echo "Byte compile for ${FLAVOR} is failed" >&2
  echo "Please check ${ELCDIR}/CompilationLog" >&2
  exit 1
}

FLAVOR=$1
VERSION=6.0.51-1
PACKAGE=mew-beta

if [ -z "${FLAVOR}" ]; then exit 0; fi
if [ "${FLAVOR}" = emacs ]; then exit 0; fi
if [ "${FLAVOR}" = emacs19 ]; then exit 0; fi
if [ "${FLAVOR}" = mule2 ]; then exit 0; fi

FLAVORTEST=`echo ${FLAVOR} | sed -e s/^s// | cut -c-6`
if [ "${FLAVORTEST}" = "xemacs" ]; then exit 0; fi

ELDIR=/usr/share/emacs/site-lisp/mew
ELCDIR_BASE=/usr/share/${FLAVOR}/site-lisp/mew
FLAGS="-no-site-file -q -batch -l __path.el -f batch-byte-compile"

COMPILED=no

ELCDIR=${ELCDIR_BASE}
echo -n "install/${PACKAGE}: Byte-compiling for ${FLAVOR} ..."
STAMP="${ELCDIR}/compile-stamp"
if [ -e "${STAMP}" ]; then
  if [ "X${VERSION}" = X"`cat ${STAMP}`" ]; then
    echo " skipped. (already compiled)" 
  else
    rm -f ${STAMP}
  fi
fi
if [ ! -e "${STAMP}" ]; then
  rm -rf ${ELCDIR}
  install -m 755 -d ${ELCDIR}
  cd ${ELDIR}
  cp *.el contrib/*.el ${ELCDIR}/
  cd ${ELCDIR}
  rm -f auxiliary.el
  FILES=`ls -1 *.el | grep -v '^mew-nmz'`
  cat << EOF > __path.el
(setq load-path (cons "." load-path))
(setq byte-compile-warnings nil)
(setq mew-compiling t)
(require 'cl)
EOF
  ${FLAVOR} ${FLAGS} ${FILES} >CompilationLog 2>&1 || abort_install

  rm -f mew-nmz*.el
  ln -sf ${ELDIR}/mew-nmz*.el .
  ln -sf ${ELDIR}/contrib/mew-nmz*.el .
  rm -f 'mew-nmz*.el'

  rm -f etc
  ln -sf ${ELDIR}/etc .
  rm -f ${FILES} __path.el
  ( for d in "${ELDIR}" "${ELDIR}/contrib"; 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 CompilationLog
  chmod 644 CompilationLog.gz
  if [ -f mew.elc ]; then
    chmod 644 *.elc
    echo "${VERSION}" > ${STAMP}
    chmod 644 ${STAMP}
    COMPILED=yes
  fi
  echo " done."
fi

if [ "${COMPILED}" = yes ]; then
  RECOMPILE_MHC=no
  RECOMPILE_W3MEL=no
  if [ -d /usr/share/${FLAVOR}/site-lisp/mhc ]; then
    RECOMPILE_MHC=yes
  fi
  if [ -d /usr/share/${FLAVOR}/site-lisp/w3m ]; then
    RECOMPILE_W3MEL=yes
  fi
  if [ "X${RECOMPILE_MHC}" = Xyes ]; then
    # remove mhc before removing w3m-el to prevent redundant byte-compiling
    pkg=mhc
    f=/usr/lib/emacsen-common/packages/remove/${pkg}
    if [ -f ${f} ]; then
      ${f} ${FLAVOR}
    fi
  fi
  if [ "X${RECOMPILE_W3MEL}" = Xyes ]; then
    pkg=w3m-el
    f=/usr/lib/emacsen-common/packages/remove/${pkg}
    if [ -f ${f} ]; then
      ${f} ${FLAVOR}
    fi
    # install w3m-el before installing mhc to prevent redundant byte-compiling
    pkg=w3m-el
    f=/usr/lib/emacsen-common/packages/install/${pkg}
    if [ -f ${f} ]; then
      ${f} ${FLAVOR}
    fi
  fi
  if [ "X${RECOMPILE_MHC}" = Xyes ]; then
    pkg=mhc
    if [ ! -d /usr/share/${FLAVOR}/site-lisp/${pkg} ]; then
      f=/usr/lib/emacsen-common/packages/install/${pkg}
      if [ -f ${f} ]; then
	${f} ${FLAVOR}
      fi
    fi
  fi
fi

exit 0
