#! /bin/bash
#
# update-auctex-elisp - Update AUCTeX auto-loads
# [installed by 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=$(basename ${0})

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

while true; do
    case "$1" in
	(--daemon)
	    DAEMON_MODE='true'
	    shift
	    ;;
	(--no-act)
	    NO_ACT='true'
	    shift
	    ;;
	(--)
	    shift
	    break
	    ;;
	(*)
	    echo >&2 "${PROGNAME}:" \
		"Internal error: unhandled option: $1"
	    exit 1;;
    esac
done
FLAVORS=${*:-'emacs'}
LOGMASK='/var/lib/auctex/%s/CompilationLog'

if [ -n "${_UPDATE_AUCTEX_ELISP_DAEMON_MODE}" ]; then
    _FLAVORS=${FLAVORS}
else
    for FLAVOR in ${FLAVORS}; do
	if ! egrep '^emacs' <(echo ${FLAVOR}) &>/dev/null; then
	    echo "${PROGNAME}:" \
		'Ignoring what does not look like an Emacs flavor:' \
		"${FLAVOR}"
	    continue
	fi
	if [ ! -x /usr/bin/${FLAVOR} ]; then
	    echo "${PROGNAME}:" \
		"Ignoring uninstalled Emacs flavor: ${FLAVOR}"
	    continue
	fi
	_FLAVORS="${_FLAVORS}${_FLAVORS:+ }${FLAVOR}"
    done

    if [ -n "${DAEMON_MODE}" -a "${DAEMON_MODE}" = 'true' ]; then
	export _UPDATE_AUCTEX_ELISP_DAEMON_MODE=${DAEMON_MODE}
	cd / && nohup ${0} ${_FLAVORS} </dev/null &>/dev/null &
	PID=${!}
	for FLAVOR in ${_FLAVORS}; do
	    echo "${PROGNAME}[${PID}]:" \
		"Will scan macros for Emacs flavor ${FLAVOR}" \
		"(log to $(printf ${LOGMASK} ${FLAVOR}))"
	done
	exit 0
    fi
fi

PIDFILE=/var/run/${PROGNAME}.pid
unset TMPFILE
trap 'echo ${TMPFILE} | xargs --no-run-if-empty rm --force' EXIT
TMPFILE=$(mktemp /var/run/${PROGNAME}.XXXXXXXX)
chmod a+r ${TMPFILE}
echo ${$} > ${TMPFILE}
until ln ${TMPFILE} ${PIDFILE} &>/dev/null; do
    kill -0 $(cat ${PIDFILE}) &>/dev/null || rm --force ${PIDFILE}
    sleep 1
done
rm --force ${TMPFILE}
trap 'rm --force ${PIDFILE}' EXIT

for FLAVOR in ${_FLAVORS}; do
    umask 0022
    ${NO_ACT:+'echo'} rm --force --recursive /var/lib/auctex/${FLAVOR}
    ${NO_ACT:+'echo'} install --owner root --group root --mode 755 \
	--directory /var/lib/auctex/${FLAVOR}
    LOGFILE=/var/lib/auctex/${FLAVOR}/CompilationLog
    echo -n "${PROGNAME}:" \
	"Scanning macros for Emacs flavor ${FLAVOR}" \
	"(log to ${LOGFILE})..."
    if [ -z "${NO_ACT}" -o "${NO_ACT}" != 'true' ]; then
	exec {stdout}>&1 {stderr}>&2 &>${LOGFILE}
	INITFILE=$(mktemp --tmpdir XXXXXXXX-el)
	cat <<EOF >${INITFILE}
(defun font-lock-fontify-syntactic-keywords-region (start end))
EOF
    fi
    ${NO_ACT:+'echo'} ${FLAVOR} --batch --no-site-file --no-init-file \
	--load=${INITFILE} \
	--load=/usr/share/emacs/site-lisp/tex-site.el \
	--funcall=TeX-auto-generate-global \
	|| EXIT_STATUS=$?
    [ -z "${NO_ACT}" -o "${NO_ACT}" != 'true' ] && \
	exec 1>&${stdout} 2>&${stderr}
    if [ ${EXIT_STATUS} -gt 0 ]; then
	echo -e ' failed.\n<LOGFILE>'
	${NO_ACT:+'echo'} cat ${LOGFILE}
	echo '</LOGFILE>'
	exit ${EXIT_STATUS}
    else
	echo ' done.'
    fi
    ${NO_ACT:+'echo'} gzip --best --force ${LOGFILE}
done

if [ -z "${NO_ACT}" -o "${NO_ACT}" != 'true' ]; then
    find /var/lib/auctex -type f -name \*.el -print0 \
	| xargs --null --no-run-if-empty rm --force
fi

exit 0
