#! /bin/bash
#
# elisp Emacs install script for the Debian auctex package
#
# Copyright (C) 1997-2023 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 -e

PROGNAME='auctex/install'

GETOPT=$(getopt -o 'd' --long 'debug' -- "${@}")
if [ ${?} != 0 ]; then
    echo >&2 "${PROGNAME}:" 'getopt error'
    exit 1
fi
eval set -- "${GETOPT}"
unset GETOPT

prefix=""

while true; do
    case "$1" in
	(--debug|-d)
	    prefix=$(pwd)
	    shift
	    ;;
	(--)
	    shift
	    break
	    ;;
	(*)
	    echo >&2 "${PROGNAME}:" \
		"Internal error: unhandled option: $1"
	    exit 1;;
    esac
done

FLAVOR=${1}

do_install () {
    local FLAVOR=${1}
    local srcdir=${prefix}/usr/share/auctex
    local objdir=${prefix}/usr/share/${FLAVOR}/site-lisp/auctex
    local ELCC="/usr/bin/${FLAVOR} --batch --no-site-file --no-init-file"
    local LOGFILE=${objdir}/CompilationLog
    echo -n "${PROGNAME}:" \
	"Byte-compiling for Emacs flavor ${FLAVOR}" \
	"(log to ${LOGFILE})..."
    rm --recursive --force ${objdir}
    install --owner root --group root --mode 755 --directory ${objdir}
    install --owner root --group root --mode 755 --directory ${objdir}/style
    exec {stdout}>&1 {stderr}>&2 &>${LOGFILE}
    pushd ${srcdir}
    umask 0022
    touch ${objdir}/.nosearch
    for target in *.el images; do
	ln --symbolic --verbose ../../../auctex/${target} \
	    ${objdir}/${target}
    done
    touch ${objdir}/style/.nosearch
    for target in style/*.el; do
	ln --symbolic --verbose ../../../../auctex/${target} \
	    ${objdir}/${target}
    done
    popd
    pushd ${objdir}
    INITFILE=$(mktemp --tmpdir XXXXXXXX-el)
    cat <<EOF >${INITFILE}
(push "." load-path)
(setq byte-compile-warnings nil
      byte-compile-verbose t
      TeX-lisp-directory "<none>"
      TeX-auto-global "<none>")
EOF
    EXIT_STATUS=0
    ${ELCC} --load ${INITFILE} --funcall batch-byte-compile *.el style/*.el \
	|| EXIT_STATUS=$?
    rm --force ${INITFILE}
    rm --force ${objdir}/style/*.el
    exec 1>&${stdout} 2>&${stderr}
    if [ ${EXIT_STATUS} -gt 0 ]; then
	echo -e ' failed.\n<LOGFILE>'
	cat ${LOGFILE}
	echo '</LOGFILE>'
	exit ${EXIT_STATUS}
    else
	echo ' done.'
    fi
    gzip --best ${LOGFILE}
    return 0
}

function do_update_auctex_elisp () {
    dpkg-trigger --by-package=auctex auctex-install-${FLAVOR}
    return 0
}

case "${FLAVOR}" in
    (emacs)
	do_install ${FLAVOR}
	do_update_auctex_elisp ${FLAVOR}
	;;
    (*)
	echo "${PROGNAME}:" \
	    "Ignoring unsupported Emacs flavor: ${FLAVOR}"
	;;
esac

exit 0
