#!/bin/bash
#
# pdftex-quiet
# Copyright (C) 2018  Jiří Kozlovský <pdftex-quiet@jkozlovsky.cz>
#
# 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/>.

VERSION="v1.1.0"

# Do not ignore failures within the pipe
set -o pipefail

# Get the script name
BASENAME=`basename "$0"`

# Remove the script's name '-quiet' suffix to get the underlying program to wrap
WRAPPED_PROGRAM=${BASENAME/%-quiet}

# Assert the wrapped program is either pdflatex or pdftex
[[ "$WRAPPED_PROGRAM" =~ ^pdf(la)?tex$ ]] || {
	echo "ERROR: Unsupported program to be wrapped!";
	echo "       - only pdftex & pdflatex is allowed, but '$WRAPPED_PROGRAM' was provided.";
	exit 101;
}

# Assert the underlying program is installed
hash "$WRAPPED_PROGRAM" 2>/dev/null || {
	echo "ERROR: You need to install $WRAPPED_PROGRAM first!";
	exit 102;
}

# Print version if required
[[ "$@" =~ -v ]] && {
	echo "pdfTeX-quiet $VERSION";
	"$WRAPPED_PROGRAM" --version;
	exit $?;
}

# Print help if required (let's print it even if only -h is specified
# although it's ambiguous because of -halt-on-error, but the regex should take care of that)
[[ "$@" =~ -h([^a]|$) ]] && {
	echo -e "Usage: $BASENAME [OPTION]... TEXNAME[.tex]\n";

	# Print only arguments from the pdftex|pdflatex help
	"$WRAPPED_PROGRAM" --help | grep --color=never -E '^[\[-]' -A500;
	exit $?;
}

# FILENAME is always the last argument for pdftex|pdflatex
# Create FILENAME variable only if there was provided some argument
test $# -gt 0 && eval FILENAME=\${$#}

# Test that the filename was provided
test -z "$FILENAME" && {
	echo "It's mandatory to provide the filename of tex source as first argument!";
	exit 103;
}

# Test the existence of the file provided
test -f "$FILENAME" || test -f "$FILENAME.tex" || {
	echo "Last argument must be a file! Provided file '$FILENAME' doesn't exist!";
	exit 104;
}

# Run pdftex|pdflatex on the file.
# Use -halt-on-error because grep basically disables the interactive mode.
# Using $@ we also allow to pass all other arguments to the pdftex|pdflatex.

# Use grep to show only errors that occur with another 200 lines followed by the error.
# Errors start with '!' at the line start. We match whole line to mark it in red bold color.

# Finally, let's inverse the grep command result so the command is considered failed 
# only if grep have found something.

# Also make sure we provide no stdin by piping program with no output ':'
# We have to do this, because grep basically disables program <-> user dialog by not printing
# characters without finishing newline character (which is usually printed after user's input).

: | \
  "$WRAPPED_PROGRAM" --halt-on-error $@ | \
  { ! grep --color=auto '^!.*' -A200; }

EVERYTHING_SUCCEEDED=$?

# And finally, print the result accordingly
if ! test $EVERYTHING_SUCCEEDED -eq 0; then
  # But first let's print empty line to improve readability
  echo
  echo "---------------------------------------------"
  echo "- Failed to compile the provided TEX file! --"
  echo "---------------------------------------------"

  # The compilation has actually failed, so exit with failure status code
  exit $EVERYTHING_SUCCEEDED
else
  echo "The compilation was successful!"

  # Finally, we are about to print the result pdf/dvi file name, so parse the arguments provided (if any)
  [[ "$@" =~ -output-format(=|[ ]*)dvi($|[ ]*) ]] && EXTENSION=dvi || EXTENSION=pdf
  [[ "$@" =~ -output-directory(=|[ ]*)([^ $]*) ]] && OUTPUT_DIRECTORY="${BASH_REMATCH[2]}/"

  # Replace whatever extension we provided to pdftex|pdflatex with either dvi or pdf (based on arguments provided)
  OUTPUT_FILENAME="${FILENAME/%.*/.${EXTENSION}}"

  echo "Output file: '${OUTPUT_DIRECTORY}${OUTPUT_FILENAME}'"

  # The compilation has succeeded, so exit with success status code
  exit $EVERYTHING_SUCCEEDED
fi
