summaryrefslogtreecommitdiff
path: root/macros/latex/contrib/dvisirule/dvisirule.sh
blob: 20969abf8f5ad65982b760a6597802030ccc5e89 (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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#!/bin/sh
#
# Copyright (C) 2012-2022 Jiro Senju
#
# This package 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 2 of the License, or
# any later version.
#
# This package 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 package.  If not, see <http://www.gnu.org/licenses/>.
#

# Superimpose the hidden or covered `\hline` and `\vline` in a LaTeX
# tabular / colortbl environment.

set -eu

usage()
{
	cat <<- EOF
	$(basename $0) [option] in.dvi [out.dvi]
	option:
		-j jobs
		   number of jobs in parallel.
		   a job means handling the extracted page.
		   default: the number of processors online
		-t dvitype-cmd
		   'dvitype' command.
		   default: dvitype or \$DIVTYPE
		-c dviconcat-cmd
		   'dviconcat' command.
		   default: dviconcat or \$DIVCONCAT
		-s dviselect-cmd
		   'dviselect' command.
		   default: dviselect or \$DIVSELECT
		-l library-dir
		   library path where dvisirule-bin, \*.awk and \*.mk
		   are located.
		   default: a dir kpsewhich(1) returned.
			if you set "library-dir" by this option, then
			"kpsewhich -path \"library-dir\"" is used.
	Also this command creates a temporary directory under \$TMPDIR
	or /tmp. For details, refer to dvisirule.pdf.
	EOF
}

jobs=0
dvitype=${DVITYPE:-dvitype}
dviconcat=${DVICONCAT:-dviconcat}
dviselect=${DVISELECT:-dviselect}
libdir=""
# posix compliant builtin
while getopts j:t:c:s:l:h i
do
	case $i in
	j)
		{
		test "$2" -eq $(($2 + 0))
		test "$2" -ge 1
		} || {
		echo Bad arg "$2" 1>&2
		exit 1
		}
		jobs="$2"
		shift
		;;
	t)	dvitype="$2"
		shift
		;;
	c)	dviconcat="$2"
		shift
		;;
	l)	libdir="-path \"$2\""
		shift
		;;
	h)	usage
		exit 0
		;;
	*)	usage 1>&2
		exit 1
		;;
	esac
	shift
done
shift $(($OPTIND - 1))

find_lib() # file
{
	eval kpsewhich $libdir $1 ||
	kpsewhich $1
}

pgnum="$(find_lib dvisirule-pgnum.awk)"
expg="$(find_lib dvisirule-expg.mk)"
marker="$(find_lib dvisirule-marker.awk)"
sirule="$(find_lib dvisirule-bin)"

# find the input file
f=$(realpath "$1")
d=$(dirname "$f")
b=$(basename "$f" .dvi)
tables="$d/$b.sirule"
test -e "$tables" ||
{
	echo No "$tables" 1>&2
	exit 1
}
# no need to handle it
test -s "$tables" || exit 0

# decide the output file
out="$d/$b-si.dvi"
test $# -eq 2 &&
out=$(realpath -m "$2")
test $# -ge 3 &&
{
	echo Bad arg 1>&2
	exit 1
}

d="${TMPDIR:-/tmp}"
test -d "$d" ||
{
	echo "$d" is not a directory 1>&2
	exit 1
}
tmp="$d/$(basename $0).$$"
mkdir "$tmp"
cd "$tmp"

# parallel processing per page
test $jobs -eq 0 &&
jobs=$(getconf _NPROCESSORS_ONLN)
# set +eu
# if [ "$MAKEFLAGS" ]
# then
#	echo "$MAKEFLAGS" | fgrep -q -- -j
#	if [ $? -eq 1 ]
#	then
#		MAKEFLAGS="$MAKEFLAGS -j$jobs"
#	fi
# else
#	MAKEFLAGS="-j$jobs"
# fi
# export MAKEFLAGS
# set -eu

# which pages to handle?
npages=$("$dvitype" -max-pages=1 "$f" |
	fgrep totalpages= |
	sed -e 's/^.*totalpages=//')
uniq "$tables" |
awk -v npages=$npages -f "$pgnum" |
tee tables2 |
xargs -r make -s -j$jobs -f "$expg" \
	DVITYPE="$dvitype" \
	Input="$f" \
	Marker="$marker" \
	SIRULE="$sirule" \
	DVISELECT="$dviselect"

# concatenate all pages
err=0
"$dviconcat" -o "$out" $(cat tables2) 2> err ||
err=$?
test $err -ne 0 &&
grep -vx 'Wrote [0-9]* pages, [0-9]* bytes' err
#test $err -eq 0 # debugging

cd $OLDPWD
rm -fr "$tmp"

exit $err