# Copyright (C) 2012 The Meme Factory, Inc.  http://www.karlpinc.com/
# Copyright (C) 2010, 2011, Karl O. Pinc  <kop@karlpinc.com>
#
#  This program is free software: you can redistribute it and/or modify
#  it under the terms of the GNU Affero 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 Affero General Public License for more details.
#
#  You should have received a copy of the GNU Affero General Public License
#  along with this program.  If not, see <https://www.gnu.org/licenses/>.
#
# Wrapper around dblatex to deliver an exit code on error.
#
# Karl O. Pinc <kop@karlpinc.com>
#
# Remarks:
#  Run with "sh <./dblatex-wrapper dblatex-wrapper" so that it
# does not have to be executable and does not have to rely on
# a (non-Posix) source (.) which takes arguments.
#
# Bugs:
# This is a kludge to deal with oddity in dblatex.
#
# Normally dblatex does not exit with an error when, at least,
# an olink does not resolve, but it does send text to stderr.
# Go figure.

export tfile=/tmp/dblatex-wrapper.$$

cleanup() {
  rm -f $tfile
}
trap cleanup ALRM HUP INT PIPE PROF TERM USR1 USR2 VTALRM EXIT

dblatex "$@" >$tfile 2>&1

status=$(echo $?)

if [ -s $tfile ] && cat $tfile && [ $status = "0" ] ; then
  ! grep ^Error: $tfile >/dev/null 2>/dev/null
  exit
fi

exit $status
