#!/bin/bash -e
#
# elisp install file for Debian auctex package.
# $Id: install.in,v 1.14 2002/01/18 16:04:09 salve Exp $
#
# Copyright (C) 1997, 98, 99, 2000, 01, 02 by Davide Giovanni Maria Salvetti.
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 2 of the License, or (at your
# option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to: The Free Software Foundation, Inc.,
# 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
#
# On Debian GNU/Linux System you can find a copy of the GNU General Public
# License in "/usr/share/common-licenses/GPL".

set -o posix

source /usr/share/debconf/confmodule
db_version 2.0
db_get auctex/logfile || true; _db_logfile=${RET}
db_get auctex/doauto || true; _db_doauto=${RET}
db_get auctex/doautofg || true; _db_doautofg=${RET}

FLAVOR=${1}

do_byte_compile () {
    EFLAGS="--no-site-file --no-init-file --batch --load=lpath.el \
	--funcall=batch-byte-compile"
    EMULEFLAGS="--no-site-file --no-init-file --batch --load=lpath-jp.el \
	--funcall=batch-byte-compile"

    # hilit-LaTeX.el:
    # this shouldn't compile without X, and we have font-latex.el anyway.

    echo >&2 -n "install/auctex: Byte-compiling for ${1}... "
    install -m 755 -d /usr/share/${1}/site-lisp/auctex/ \
	/usr/share/${1}/site-lisp/auctex//style/
    # This one prevents ${style} from being added to load-path by
    # normal-top-level-add-subdirs-to-load-path (see subdirs.el)
    (umask 022 && touch /usr/share/${1}/site-lisp/auctex//style//.nosearch)
    for i in auc-old.el tex.el tex-buf.el latex.el tex-info.el texmathp.el multi-prompt.el tex-mik.el font-latex.el tex-font.el bib-cite.el tex-fptex.el style/slides.el    style/foils.el    style/amstex.el style/article.el   style/book.el     style/letter.el style/report.el    style/amsart.el   style/amsbook.el style/epsf.el      style/psfig.el    style/latexinfo.el style/dutch.el     style/german.el   style/dk.el style/j-article.el style/j-book.el   style/j-report.el style/jarticle.el  style/jbook.el    style/jreport.el style/dinbrief.el  style/virtex.el   style/plfonts.el style/plhb.el      style/harvard.el	style/swedish.el style/danish.el    style/slovak.el   style/czech.el style/amsmath.el   style/amstext.el  style/amsbsy.el style/amsopn.el    style/amsthm.el	style/natbib.el style/index.el     style/makeidx.el  style/multind.el style/varioref.el  style/fancyref.el	style/mdwlist.el style/ngerman.el   style/graphicx.el	style/graphics.el style/verbatim.el
      do cp /usr/share/emacs/site-lisp/auctex//${i} /usr/share/${1}/site-lisp/auctex//${i}
    done
    cat <<-EOF >/usr/share/${1}/site-lisp/auctex//lpath.el
		(setq load-path (cons "." load-path)
		    TeX-lisp-directory "<none>")
	EOF
    (cd /usr/share/${1}/site-lisp/auctex/ \
	&& ${1} ${EFLAGS} auc-old.el tex.el tex-buf.el latex.el tex-info.el texmathp.el multi-prompt.el tex-mik.el font-latex.el tex-font.el bib-cite.el tex-fptex.el style/slides.el    style/foils.el    style/amstex.el style/article.el   style/book.el     style/letter.el style/report.el    style/amsart.el   style/amsbook.el style/epsf.el      style/psfig.el    style/latexinfo.el style/dutch.el     style/german.el   style/dk.el style/j-article.el style/j-book.el   style/j-report.el style/jarticle.el  style/jbook.el    style/jreport.el style/dinbrief.el  style/virtex.el   style/plfonts.el style/plhb.el      style/harvard.el	style/swedish.el style/danish.el    style/slovak.el   style/czech.el style/amsmath.el   style/amstext.el  style/amsbsy.el style/amsopn.el    style/amsthm.el	style/natbib.el style/index.el     style/makeidx.el  style/multind.el style/varioref.el  style/fancyref.el	style/mdwlist.el style/ngerman.el   style/graphicx.el	style/graphics.el style/verbatim.el 2>&1 \
	| gzip -9qf > CompilationLog.gz)
	# We always have MULE, as we don't support emacs19 anymore.
    for i in tex-jp.el
      do cp /usr/share/emacs/site-lisp/auctex//${i} \
	  /usr/share/${1}/site-lisp/auctex//${i}
    done
	# This contains an hack to allow byte-compiling
	# with japanese multibyte chars
    cat <<-EOF >/usr/share/${1}/site-lisp/auctex//lpath-jp.el
		(setq load-path (cons "." load-path)
			TeX-lisp-directory "<none>")
		(set-language-environment 'Japanese);')
	EOF
    (cd /usr/share/${1}/site-lisp/auctex/ \
	&& ${1} ${EMULEFLAGS} tex-jp.el 2>&1 \
	| gzip -9qf >> CompilationLog.gz)
    rm -f /usr/share/${1}/site-lisp/auctex//*.el \
	/usr/share/${1}/site-lisp/auctex//style//*.el
    echo >&2 "done."
    return 0
}

do_auto () {
    case "${_db_doauto}" in
	(Foreground)
	case "${_db_doautofg}" in
	    (Console)
	    rm -f ${_db_logfile}
	    /usr/sbin//update-auctex-elisp ${1} ;;
	    (File)
	    echo >&2 -n "update-auctex-elisp: "
	    echo >&2 "Further output will appear in: ${_db_logfile}."
	    echo >&2 -n "auctex: "
	    echo >&2 -n "Waiting for update-auctex-elisp to terminate... "
	    /usr/sbin//update-auctex-elisp ${1} &> ${_db_logfile}
	    echo >&2 "done." ;;
	    (*) echo >&2 \
		"${0##*/}: Debconf passed unknown value \`${_db_doautofg}'." ;;
	esac ;;
	(Background)
	# Closing fd 3 is debconf important.
						/usr/sbin//update-auctex-elisp ${1} &> ${_db_logfile} 3>&- &
	echo >&2 -n "update-auctex-elisp[${!}]: "
	echo >&2 "Further output will appear in: ${_db_logfile}." ;;
	(None) rm -f ${_db_logfile} ;;
	(*) echo >&2 \
	    "${0##*/}: Debconf passed unknown value \`${_db_doauto}'." ;;
    esac
    return 0
}

case "${FLAVOR}" in
    (emacs) : ;;
    (emacs20|emacs21)
        do_byte_compile ${FLAVOR}
        do_auto ${FLAVOR} ;;
    (*) echo >&2 "install/auctex: Ignoring emacsen flavor ${FLAVOR}." ;;
esac

exit 0
