#!/bin/bash -e
#
# elisp install file for Debian auctex package.
# $Id: install.in,v 1.24 2005/01/15 12:46:10 salve Exp $
#
# Copyright (C) 1997, 98, 99, 2000, 01, 02, 03, 04 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/default || true; _db_default=${RET}
db_get auctex/doauto || true; _db_doauto=${RET}
db_get auctex/doautofg || true; _db_doautofg=${RET}

FLAVOR=${1}

INSTALL="install -o root -g root"
INSTDIR="${INSTALL} -m 755 -d"

do_install () {
    echo >&2 -n "install/auctex: Setting up for ${FLAVOR}... "
    cd /usr/share/emacs/site-lisp/auctex/; umask 0022;
    ${INSTDIR} /usr/share/${FLAVOR}/site-lisp/auctex/
    ./configure --disable-build-dir-test \
	--prefix=/usr/ \
	--with-emacs=${FLAVOR} \
	--with-lispdir=/usr/share/${FLAVOR}/site-lisp/ \
	--with-auto-dir=/var/lib/auctex/${FLAVOR} \
	>> /usr/share/${FLAVOR}/site-lisp/auctex//CompilationLog 2>&1
    make INSTALL="${INSTALL}" MKINSTALLDIRS="${INSTDIR}" \
	install-lisp install-contrib \
	>> /usr/share/${FLAVOR}/site-lisp/auctex//CompilationLog 2>&1
    make distclean \
	>> /usr/share/${FLAVOR}/site-lisp/auctex//CompilationLog 2>&1
    find /usr/share/${FLAVOR}/site-lisp/auctex/ -type f -name \*.el \
	-print0 | xargs --null rm -f dummy \
	>> /usr/share/${FLAVOR}/site-lisp/auctex//CompilationLog 2>&1
    gzip -9fq /usr/share/${FLAVOR}/site-lisp/auctex//CompilationLog
    echo -e >/etc/${FLAVOR}/site-start.d//50auctex.el \
	";;; This file is automatically generated.\n"
    echo -e >>/etc/${FLAVOR}/site-start.d//50auctex.el \
	"(if (fboundp 'debian-pkg-add-load-path-item)\n" \
	"   (progn (debian-pkg-add-load-path-item \"/usr/share/${FLAVOR}/site-lisp/auctex/\")\n" \
	"          (debian-pkg-add-load-path-item \"/usr/share/emacs/site-lisp/auctex/\"))\n" \
	" (progn (add-to-list 'load-path \"/usr/share/${FLAVOR}/site-lisp/auctex/\" 'append)\n" \
	"        (add-to-list 'load-path \"/usr/share/emacs/site-lisp/auctex/\" 'append)))"
    if [ "${_db_default}" = "true" ]; then
	echo -e >>/etc/${FLAVOR}/site-start.d//50auctex.el \
	"\n(require 'tex-site)"
    fi
    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 ${FLAVOR} ;;
	    (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 ${FLAVOR} >> ${_db_logfile} 2>&1
	    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 ${FLAVOR} >> ${_db_logfile} 2>&1 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) : ;;
    (emacs21|emacs-snapshot)
        do_install ${FLAVOR}
        do_auto ${FLAVOR} ;;
    (*) echo >&2 "install/auctex: Ignoring emacsen flavor ${FLAVOR}." ;;
esac

exit 0
