summaryrefslogtreecommitdiff
path: root/support/pdftex-quiet/pdftex-quiet
blob: 7bf4599c71bf17aca833972653fe9d6050c7884d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#!/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