#! /bin/bash -e
#
# <% elisp %> Emacs install script for auctex
#
# Copyright (C) 1997, 98, 99, 2000, 01, 02, 03, 04, 05, 06, 07
# by Davide G. M. 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.,
# 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, 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}

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

do_install () {
    echo >&2 -n "install/auctex: Setting up for ${FLAVOR}" \
    "(log file: /usr/share/${FLAVOR}/site-lisp/auctex//CompilationLog)... "
    cd /usr/share/emacs/site-lisp/auctex/; umask 0022;
        touch --reference configure --date "-1 second" configure.ac
    touch --reference configure --date "-1 second" aclocal.m4
    touch --reference configure --date "-1 second" preview/configure.ac
    ${INSTDIR} /usr/share/${FLAVOR}/site-lisp/auctex/
    ./configure --disable-build-dir-test GS=gs \
	LATEX=/bin/true PDFLATEX=/bin/true TEX=/bin/true \
	--prefix=/usr/ \
	--with-emacs=${FLAVOR} \
	--with-lispdir=/usr/share/${FLAVOR}/site-lisp/ \
	--with-texmf-dir=/usr/share/texmf/ \
	--with-auto-dir=/var/lib/auctex/${FLAVOR}/ \
	>> /usr/share/${FLAVOR}/site-lisp/auctex//CompilationLog 2>&1
    make INSTALL="${INSTALL}" MKINSTALLDIRS="${INSTDIR}" \
	install-lisp install-startup \
	>> /usr/share/${FLAVOR}/site-lisp/auctex//CompilationLog 2>&1
    ln -sf /usr/share/emacs/site-lisp/auctex//images /usr/share/${FLAVOR}/site-lisp/auctex//
    pushd preview \
	>> /usr/share/${FLAVOR}/site-lisp/auctex//CompilationLog 2>&1
    make INSTALL="${INSTALL}" MKINSTALLDIRS="${INSTDIR}" \
	install-el install-nosearch install-startup \
	>> /usr/share/${FLAVOR}/site-lisp/auctex//CompilationLog 2>&1
    popd >> /usr/share/${FLAVOR}/site-lisp/auctex//CompilationLog 2>&1
    make maintainer-clean \
	>> /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 >&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##*/}: Unknown Debconf value doautofg = \"${_db_doautofg}\"." ;;
	esac ;;
	(Background)
							/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##*/}: Unknown Debconf value doauto = \"${_db_doauto}\"." ;;
    esac
    return 0
}

do_clean_old_cruft () {
        rm -f /usr/share/${FLAVOR}/site-lisp/auctex//style//../preview/.nosearch
    test -d /usr/share/${FLAVOR}/site-lisp/auctex//../preview/ && \
	rmdir --ignore-fail-on-non-empty \
	/usr/share/${FLAVOR}/site-lisp/auctex//../preview/
    return 0
}

case "${FLAVOR}" in
    emacs) :;;
    emacs2[12]|emacs-snapshot)
		do_clean_old_cruft ${FLAVOR}
	do_install ${FLAVOR}
		do_auto ${FLAVOR};;
    *) echo >&2 "install/auctex:" \
	"Ignoring emacsen flavor: \"${FLAVOR}\"."
esac

exit 0
