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

FLAVOR=$1
VERSION=2.11.30+0.20040810-2
PACKAGE=wl-beta

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 name
  exit 1
fi
if [ "X${FLAVOR}" = Xemacs ]; then exit 0; fi
if [ "X${FLAVOR}" = Xemacs19 ]; then exit 0; fi

ELDIR=/usr/share/emacs/site-lisp/wl
ELCDIR_BASE=/usr/share/${FLAVOR}/site-lisp/wl
SITELISP=/usr/share/${FLAVOR}/site-lisp
APEL_DIR_BASE=${SITELISP}/apel
FLIM_DIR_BASE=${SITELISP}/flim
SEMI_DIR_BASE=${SITELISP}/semi
W3M_DIR_BASE=${SITELISP}/w3m
BBDB_DIR_BASE=${SITELISP}/bbdb

EMACSEN_MULE=${FLAVOR}
EMACSEN_NOMULE=""

FLAVOR_SUFFIX=`echo ${FLAVOR} | cut -s -d - -f 2`
FLAVORTEST=`echo ${FLAVOR} | 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
    EMACSEN_NOMULE=""
  elif [ "X${FLAVOR_SUFFIX}" = "Xnomule" ]; then
    EMACSEN_MULE=""
    EMACSEN_NOMULE=`ls /usr/bin/${FLAVOR_BODY}*-nomule* 2>/dev/null | head -1`
    if [ -n "${EMACSEN_NOMULE}" ]; then
      EMACSEN_NOMULE=`basename ${EMACSEN_NOMULE}`
    fi
  else
    FLAVOR_SUFFIX=""
    EMACSEN_MULE=`ls /usr/bin/${FLAVOR}*-mule* 2>/dev/null | head -1`
    if [ -n "${EMACSEN_MULE}" ]; then
      EMACSEN_MULE=`basename ${EMACSEN_MULE}`
    fi
    EMACSEN_NOMULE=`ls /usr/bin/${FLAVOR}*-nomule* 2>/dev/null | head -1`
    if [ -n "${EMACSEN_NOMULE}" ]; then
      EMACSEN_NOMULE=`basename ${EMACSEN_NOMULE}`
    fi
  fi
fi

COMPILED=no
for EMACSEN in "${EMACSEN_MULE}" "${EMACSEN_NOMULE}"; do
  if [ -n "${EMACSEN}" ]; then
    ELCDIR=${ELCDIR_BASE}
    APEL_DIR=${APEL_DIR_BASE}
    FLIM_DIR=${FLIM_DIR_BASE}
    SEMI_DIR=${SEMI_DIR_BASE}
    W3M_DIR=${W3M_DIR_BASE}
    BBDB_DIR=${BBDB_DIR_BASE}
    if [ "X${FLAVORTEST}" = "Xxemacs" -a -z "${FLAVOR_SUFFIX}" ]; then
      if [ "X${EMACSEN}" = "X${EMACSEN_MULE}" ]; then
	echo -n "install/${PACKAGE}: Byte-compiling for ${FLAVOR} (mule) ..."
        ELCDIR=${ELCDIR}/mule
	if [ -d ${APEL_DIR_BASE}/mule ]; then
	  APEL_DIR=${APEL_DIR_BASE}/mule
	fi
	if [ -d ${FLIM_DIR_BASE}/mule ]; then
	  FLIM_DIR=${FLIM_DIR_BASE}/mule
	fi
	if [ -d ${SEMI_DIR_BASE}/mule ]; then
	  SEMI_DIR=${SEMI_DIR_BASE}/mule
	fi
	if [ -d ${W3M_DIR_BASE}/mule ]; then
	  W3M_DIR=${W3M_DIR_BASE}/mule
	fi
	if [ -d ${BBDB_DIR_BASE}/mule ]; then
	  BBDB_DIR=${BBDB_DIR_BASE}/mule
	fi
      else
	echo -n "install/${PACKAGE}: Byte-compiling for ${FLAVOR} (nomule) ..."
        ELCDIR=${ELCDIR}/nomule
	if [ -d ${APEL_DIR_BASE}/nomule ]; then
	  APEL_DIR=${APEL_DIR_BASE}/nomule
	fi
	if [ -d ${FLIM_DIR_BASE}/nomule ]; then
	  FLIM_DIR=${FLIM_DIR_BASE}/nomule
	fi
	if [ -d ${SEMI_DIR_BASE}/nomule ]; then
	  SEMI_DIR=${SEMI_DIR_BASE}/nomule
	fi
	if [ -f ${ELCDIR_BASE}/mule/elmo-shimbun.elc ]; then
	  W3M_DIR=""
	fi
	if [ -d ${W3M_DIR_BASE}/nomule ]; then
	  W3M_DIR=${W3M_DIR_BASE}/nomule
	fi
	if [ -d ${BBDB_DIR_BASE}/nomule ]; then
	  BBDB_DIR=${BBDB_DIR_BASE}/nomule
	fi
      fi
    else
      echo -n "install/${PACKAGE}: Byte-compiling for ${FLAVOR} ..."
    fi
    STAMP="${ELCDIR}/compile-stamp"
    if [ -f ${W3M_DIR}/shimbun/shimbun.elc -a ! -f ${ELCDIR}/elmo-shimbun.elc ]; then
      rm -f ${STAMP}
    elif [ ! -f ${W3M_DIR}/shimbun/shimbun.elc -a -f ${ELCDIR}/elmo-shimbun.elc ]; then
      rm -f ${STAMP}
    elif [ -f ${W3M_DIR}/shimbun/shimbun.elc -a -f ${ELCDIR}/elmo-shimbun.elc ]; then
      if [ ${W3M_DIR}/shimbun/shimbun.elc -nt ${ELCDIR}/elmo-shimbun.elc ]; then
	rm -f ${STAMP}
      fi
    fi
    if [ -f ${BBDB_DIR}/bbdb.elc -a ! -f ${ELCDIR}/bbdb-wl.elc ]; then
      rm -f ${STAMP}
    elif [ ! -f ${BBDB_DIR}/bbdb.elc -a -f ${ELCDIR}/bbdb-wl.elc ]; then
      rm -f ${STAMP}
    elif [ -f ${BBDB_DIR}/bbdb.elc -a -f ${ELCDIR}/bbdb-wl.elc ]; then
      if [ ${BBDB_DIR}/bbdb.elc -nt ${ELCDIR}/bbdb-wl.elc ]; then
	rm -f ${STAMP}
      fi
    fi
    if [ -f ${SEMI_DIR}/compile-stamp -a ! -f ${ELCDIR}/compile-stamp ]; then
      rm -f ${STAMP}
    elif [ ! -f ${SEMI_DIR}/compile-stamp -a -f ${ELCDIR}/compile-stamp ]; then
      rm -f ${STAMP}
    elif [ -f ${SEMI_DIR}/compile-stamp -a -f ${ELCDIR}/compile-stamp ]; then
      if [ ${SEMI_DIR}/compile-stamp -nt ${ELCDIR}/compile-stamp ]; then
	rm -f ${STAMP}
      fi
    fi
    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
      if [ "X${FLAVORTEST}" = "Xxemacs" -a -z "${FLAVOR_SUFFIX}" ]; then
	rm -f ${ELCDIR_BASE}/*.el ${ELCDIR_BASE}/*.elc
      fi
      rm -rf ${ELCDIR}
      install -m 755 -d ${ELCDIR}/tmp
      cp -r ${ELDIR}/* ${ELCDIR}/tmp/
      rm -rf ${ELCDIR}/tmp/etc/icons
      ln -sf /usr/share/pixmaps/wl ${ELCDIR}/tmp/etc/icons
      ( cd ${ELCDIR}/tmp
	cat << EOF > WL-CFG.el
(setq wl-install-utils t)
(setq load-path (cons "${APEL_DIR}" load-path))
(setq load-path (cons "${FLIM_DIR}" load-path))
(setq load-path (cons "${SEMI_DIR}" load-path))
(setq load-path (cons "${BBDB_DIR}" load-path))
EOF
	if [ -n "${W3M_DIR}" ]; then
	  cat << EOF >> WL-CFG.el
(setq load-path (cons "${W3M_DIR}/shimbun" load-path))
(setq load-path (cons "${W3M_DIR}" load-path))
EOF
	fi
	make EMACS=${EMACSEN} LISPDIR=${ELCDIR} PIXMAPDIR=${ELCDIR}/icons \
	  > ${ELCDIR}/CompilationLog 2>&1
	make EMACS=${EMACSEN} LISPDIR=${ELCDIR} PIXMAPDIR=${ELCDIR}/icons \
	  install >> ${ELCDIR}/CompilationLog 2>&1
	make EMACS=${EMACSEN} LISPDIR=${ELCDIR} PIXMAPDIR=${ELCDIR}/icons \
	  clean >> ${ELCDIR}/CompilationLog 2>&1
	cd ${ELCDIR}
	echo "rm -rf ${ELCDIR}/wl/*.el ${ELCDIR}/icons ${ELCDIR}/tmp" >> ${ELCDIR}/CompilationLog 2>&1
	rm -rf ${ELCDIR}/wl/*.el ${ELCDIR}/icons ${ELCDIR}/tmp
	echo "mv ${ELCDIR}/wl/* ${ELCDIR}/" >> ${ELCDIR}/CompilationLog 2>&1
	mv ${ELCDIR}/wl/* ${ELCDIR}/
	echo "rm -rf ${ELCDIR}/wl" >> ${ELCDIR}/CompilationLog 2>&1
	rm -rf ${ELCDIR}/wl
	for d in "${ELDIR}/elmo" "${ELDIR}/utils" "${ELDIR}/wl"; 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}/elmo.elc ]; then
	touch ${ELCDIR}/*.elc
	chmod 644 ${ELCDIR}/*.elc
	echo "${VERSION}" > ${STAMP}
	chmod 644 ${STAMP}
	COMPILED=yes
      fi
      if [ "X${FLAVORTEST}" = "Xxemacs" -a -z "${FLAVOR_SUFFIX}" ]; then
	if [ -e ${ELCDIR_BASE}/mule/compile-stamp ]; then
	  ln -sf ${ELCDIR_BASE}/mule/*.elc ${ELCDIR_BASE}/mule/*.el ${ELCDIR_BASE}/
	elif [ -e ${ELCDIR_BASE}/nomule/compile-stamp ]; then
	  ln -sf ${ELCDIR_BASE}/nomule/*.elc ${ELCDIR_BASE}/nomule/*.el ${ELCDIR_BASE}/
	fi
      fi
      echo " done."
    fi
  fi
done

if [ "X${COMPILED}" = Xyes ]; then
  # recompile mhc for mhc-wl
  pkg=mhc
  if [ -d /usr/share/${FLAVOR}/site-lisp/${pkg} ]; then
    if [ -f /usr/lib/emacsen-common/packages/install/${pkg} ]; then
      /usr/lib/emacsen-common/packages/remove/${pkg} ${FLAVOR}
      /usr/lib/emacsen-common/packages/install/${pkg} ${FLAVOR}
    fi
  fi
fi

exit 0
