#! /bin/bash -e
#
# <% elisp %> Emacs install script for auctex
#
# Copyright (C) 1997, 98, 99, 2000, 01, 02, 03, 04, 05, 06, 07, 08, 09, 10
# 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 3 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, see <http://www.gnu.org/licenses/>.
#
# 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/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}" \
	lisp 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
    for s in /usr/share/${FLAVOR}/site-lisp/auctex//*.elc; do
	find /usr/share/emacs/site-lisp/auctex/ -type f -name $(basename ${s%c}) -print0 \
	    | xargs --null --no-run-if-empty \
	    ln --symbolic --verbose \
	    --target-directory=/usr/share/${FLAVOR}/site-lisp/auctex/ \
	    >> /usr/share/${FLAVOR}/site-lisp/auctex//CompilationLog 2>&1
    done
    gzip -9fq /usr/share/${FLAVOR}/site-lisp/auctex//CompilationLog
    echo >&2 "done."
    return 0
}

do_auto () {
    LOGFILE=/var/log/auctex-${FLAVOR}.log
    case "${_db_doauto}" in
	(Foreground)
	case "${_db_doautofg}" in
	    (Console)
	    /usr/sbin//update-auctex-elisp ${FLAVOR} ;;
	    (File)
	    echo >&2 -n "update-auctex-elisp: "
	    echo >&2 "Further output will appear in: ${LOGFILE}."
	    echo >&2 -n "auctex: "
	    echo >&2 -n "Waiting for update-auctex-elisp to terminate... "
	    /usr/sbin//update-auctex-elisp ${FLAVOR} >> ${LOGFILE} 2>&1
	    echo >&2 "done." ;;
	    (*) echo >&2 \
		"${0##*/}: Unknown Debconf value doautofg = \"${_db_doautofg}\"." ;;
	esac ;;
	(Background)
							/usr/sbin//update-auctex-elisp ${FLAVOR} >> ${LOGFILE} 2>&1 3>&- &
	echo >&2 -n "update-auctex-elisp[${!}]: "
	echo >&2 "Further output will appear in: ${LOGFILE}." ;;
	(None) :;;
	(*) 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[123]|emacs-snapshot)
		do_clean_old_cruft ${FLAVOR}
	do_install ${FLAVOR}
		do_auto ${FLAVOR};;
    *) echo >&2 "install/auctex:" \
	"Ignoring emacsen flavor: \"${FLAVOR}\"."
esac

exit 0
