#! /bin/sh -e
# /usr/lib/emacsen-common/packages/install/w3m-el

# Written by Jim Van Zandt <jrv@vanzandt.mv.com>, borrowing heavily
# from the install scripts for gettext by Santiago Vila
# <sanvila@ctv.es> and octave by Dirk Eddelbuettel <edd@debian.org>.

FLAVOR=$1
PACKAGE=w3m

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

if [ ${FLAVOR} = emacs ]; then exit 0; fi

echo install/${PACKAGE}: Handling install for emacsen flavor ${FLAVOR}

install -m 755 -d ${ELCDIR}
install -m 755 -d ${ELCDIR}/shimbun

case ${FLAVOR} in
xemacs*)
    SITEFLAG="-no-site-file";;
*)
    SITEFLAG="--no-site-file";;
esac
LPATH="NONE"
for el in apel mule-ucs
do
  if [ -d /usr/share/emacs/site-lisp/${el} -a ! -d /usr/share/${FLAVOR}/site-lisp/${el} ]; then
    /usr/lib/emacsen-common/emacs-package-install ${el}
  fi
  if [ -d /usr/share/${FLAVOR}/site-lisp/${el} ]; then
    LPATH="${LPATH}:/usr/share/${FLAVOR}/site-lisp/${el}"
  fi
done
for el in flim mew bitmap-mule gnus t-gnus
do
 if [ -d /usr/share/${FLAVOR}/site-lisp/${el} ]; then
  LPATH="${LPATH}:/usr/share/${FLAVOR}/site-lisp/${el}"
 elif [ -d /usr/share/emacs/site-lisp/${el} ]; then
  LPATH="${LPATH}:/usr/share/emacs/site-lisp/${el}"
 fi
done
if [ ${FLAVOR} = mule2 ]; then
  LPATH="${LPATH}":/usr/share/mule2/site-lisp/custom-mule
  cp /usr/share/doc/w3m-el/attic/regexp-opt.el ${ELCDIR}/regexp-opt.el
fi

FLAGS="${SITEFLAG} -q -batch -l path.el -l w3mhack.el ${LPATH}"

# Install-info-altdir does not actually exist. 
# Maybe somebody will write it.
# if test -x /usr/sbin/install-info-altdir; then
#     echo install/${PACKAGE}: install Info links for ${FLAVOR}
#     install-info-altdir --quiet --section "" "" --dirname=${FLAVOR} /usr/info/${PACKAGE}.info.gz
# fi

(cd ${ELDIR}; FILES=`echo *.el`; cp ${FILES} ${ELCDIR})
(cd ${ELDIR}/shimbun; FILES=`echo *.el`; cp ${FILES} ${ELCDIR}/shimbun)
cd ${ELCDIR}
rm -f *.elc shimbun/*.elc
if [ ${FLAVOR} = mule2 ]; then
  ${FLAVOR} -q -no-site-file -batch -f batch-byte-compile regexp-opt.el
fi
cat << EOF > path.el
(setq load-path (cons "." load-path) byte-compile-warnings nil)
EOF
if [ "$(${FLAVOR} ${FLAGS} -f w3mhack-locate-library xml.el)" = "" ]; then
  cp /usr/share/doc/w3m-el/attic/xml.el ${ELCDIR}/xml.el
fi

${FLAVOR} ${FLAGS} -f w3mhack-compile 2>&1 | gzip -9 > CompilationLog.gz
rm -f `ls -1 *.el | grep -v 'w3m-\(kwds\|load\).el'` path.el shimbun/*.el path.elc

# force to recopmile wl for shimbun module
#  fix "Cannot open load file: elmo-shimbun"
if test -d /usr/share/${FLAVOR}/site-lisp/wl; then
 for pkg in wl wl-beta
 do
  if test -f /usr/lib/emacsen-common/packages/install/${pkg}; then
   /usr/lib/emacsen-common/packages/install/${pkg} ${FLAVOR}
  fi
 done
fi
exit 0
