summaryrefslogtreecommitdiff
path: root/mkdeps.sh
blob: 496c13e8498b78d5ef49dd513e183e34d5002bc8 (plain) (blame)
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
#!/bin/sh
set -eu

export LANG=POSIX.UTF-8


## -m so a path need not exist: po4a's outputs are named here
## before po4a has written them --- naming them is what the rules
## being generated are for --- and realpath without it fails on
## the first one, taking the whole script with it under set -e.
## For a path that does exist the answer is the same either way.
normalize() {
	## realpath(1) did this, but it is a page of string work and
	## this runs it dozens of times: doing it here saves a
	## process and most of the time.
	awk '
		{
			n = split($0, c, "/")
			top = 0
			for (i = 1; i <= n; i++) {
				if (c[i] == "" || c[i] == ".") {
					continue
				}
				if (c[i] == ".." && top > 0 &&
				    out[top] != "..") {
					top--
					continue
				}
				out[++top] = c[i]
			}
			p = ""
			for (i = 1; i <= top; i++) {
				p = p (i > 1 ? "/" : "") out[i]
			}
			if (p != "") {
				print p
			}
		}
	' | sort
}

## Every page there is: what the repository carries, plus what
## po4a writes from it.  A translation need not exist yet -- on a
## fresh clone none of them do -- so it is named, not found.
universe() {
	{
		find src/content -type f -name '*.adoc'
		page_translations
	} | normalize | uniq
}

## The classifying rules read the masters, which the repository
## always carries; a translation is whatever its master was.
homes() {
	find src/content/*/index.adoc -type f | normalize |
		only_masters | with_translations | normalize
}

collections() {
	grep -l '^:type: collection$' src/content/*/*/index.adoc |
		normalize | only_masters | xargs -n1 dirname
}

indexes() {
	all_collections | sed 's|$|/index.adoc|' | normalize
}

## Every collection there is, translations included: the rules
## below emit a listing, a feed and a sortdata list for each, and
## a translated collection needs all three as much as its master.
all_collections() {
	collections | sed 's|$|/index.adoc|' | with_translations |
		sed 's|/index.adoc$||' | normalize
}

categories() {
	grep -l '^:type: categories$' src/content/*/*/*.adoc |
		normalize | only_masters | with_translations | normalize
}

articles() {
	collections | xargs -I% find % -mindepth 3 -name '*.adoc' |
		normalize | only_masters | with_translations | normalize
}

slides() {
	find src/content -type f -name '*.eslaides'              | normalize
}

## Whatever no other rule claimed, at any depth.
pages() {
	claimed="$(mktemp)"
	{ homes; indexes; categories; articles; } | sort -u > "$claimed"
	universe | grep -vxF -f "$claimed"
	rm -f "$claimed"
}

media() {
	# src/content/music is excluded: those .ogg files are score
	# renderings, not published media, and the old site never
	# offered them over BitTorrent.
	find src/content/ -path 'src/content/music' -prune -or \
		\( \( -type f -and \! -type l \) -and \( \
			-name '*.flac' -or \
			-name '*.ogg'  -or \
			-name '*.webm'     \
		\) -print \) | normalize
}

## A glob that matches nothing makes find exit non-zero, which
## under set -e would end the script.  There is no music yet.
music() {
	find src/content/music/*.ly 2>/dev/null | normalize || true
}

tarballs() {
	find src/content/ \( -type f -and \! -type l \) -and \( \
			-name '*.tar.gz'   \
		\) | normalize
}

## Short paths that stand in for a page's own, kept as symlinks
## beside it.  Guarded like music() above: a site need not have any,
## and find exits non-zero on a directory that is not there.
shortener() {
	find src/content/l -type l 2>/dev/null | normalize || true
}

extras() {
	media
	tarballs
	shortener
}

## Which pages are one another's translations.  po4a already says
## so, set by set, so read it there rather than keep a second list
## that can quietly disagree with the first.
## The languages po4a translates into: whichever .po files exist.
langs() {
	if [ -z "${LANGS_CACHE:-}" ]; then
		LANGS_CACHE="$(
			find po -maxdepth 1 -name '*.po' |
				sed 's|.*/||; s|\.po$||' | sort
		)"
		export LANGS_CACHE
	fi
	echo "$LANGS_CACHE"
}

## One stanza per line, master first, then the files po4a writes
## from it.  "$lang:" stands for every language, and "add_$lang:"
## names an addendum rather than an output.
po4a_sets() {
	langs | tr '\n' ' ' | {
		## read hits EOF on the last field and reports it, which
		## under set -e would end the block before awk ran.
		read -r all || true
		awk -v langs="$all" '
			/^\[type:/ { set = "" }
			/^\[type:/ || cont {
				line = $0
				sub(/^\[type:[^]]*\][ \t]*/, "", line)
				cont = (line ~ /\\$/)
				sub(/[ \t]*\\$/, "", line)
				n = split(line, f, /[ \t]+/)
				nl = split(langs, L, " ")
				for (i = 1; i <= n; i++) {
					if (f[i] == "" || f[i] ~ /^add_/) continue
					if (f[i] ~ /^\$lang:/) {
						sub(/^\$lang:/, "", f[i])
						for (j = 1; j <= nl; j++) {
							t = f[i]
							gsub(/\$lang/, L[j], t)
							set = set " " t
						}
						continue
					}
					sub(/^[a-z]+:/, "", f[i])
					set = set (set == "" ? "" : " ") f[i]
				}
				if (!cont && set != "") { print set; set = "" }
			}
		' po/po4a.cfg
	}
}

## Everything po4a writes: every field of a stanza but the first.
translations() {
	cut -d' ' -f2- "$(po4a_cache)" |
		tr ' ' '\n' | grep . | normalize
}

## Just the pages among them.  The switcher offers a reader the
## same page in another language; the data files under src/i18n
## are not pages.
page_translations() {
	translations | grep '\.adoc$'
}

## Drops the translations from a list of paths, so a rule reads
## the masters whether or not po4a has run.
## po4a.cfg does not change while this runs, so parse it once:
## every selector below asks for the same answer.
po4a_cache() {
	if [ -z "${PO4A_CACHE:-}" ]; then
		PO4A_CACHE="$(mktemp)"
		po4a_sets > "$PO4A_CACHE"
		export PO4A_CACHE
	fi
	echo "$PO4A_CACHE"
}

translations_cache() {
	if [ -z "${TRANSLATIONS_CACHE:-}" ]; then
		TRANSLATIONS_CACHE="$(mktemp)"
		translations > "$TRANSLATIONS_CACHE"
		export TRANSLATIONS_CACHE
	fi
	echo "$TRANSLATIONS_CACHE"
}

only_masters() {
	awk 'NR == FNR { t[$0]; next } !($0 in t)' \
		"$(translations_cache)" -
}

## Reads masters on stdin, writes them and their translations.
with_translations() {
	awk -v sets="$(po4a_cache)" '
		BEGIN {
			while ((getline line < sets) > 0) {
				n = split(line, f, /[ \t]+/)
				for (i = 2; i <= n; i++) {
					t[f[1]] = t[f[1]] " " f[i]
				}
			}
		}
		{
			print
			if ($0 in t) {
				n = split(t[$0], g, " ")
				for (i = 1; i <= n; i++) {
					print g[i]
				}
			}
		}
	'
}

## Prime the caches here, in this shell.  Every pipeline stage
## below runs in a subshell, so a cache filled inside one is gone
## by the next: filling them once, up here, is what makes them
## caches rather than seven identical computations.
PO4A_CACHE="$(mktemp)"
po4a_sets > "$PO4A_CACHE"
TRANSLATIONS_CACHE="$(mktemp)"
translations > "$TRANSLATIONS_CACHE"
LANGS_CACHE="$(find po -maxdepth 1 -name '*.po' |
	sed 's|.*/||; s|\.po$||' | sort)"
export PO4A_CACHE TRANSLATIONS_CACHE LANGS_CACHE
trap 'rm -f "$PO4A_CACHE" "$TRANSLATIONS_CACHE"' EXIT

pages                                      | varlist      'pages.adoc'
homes                                      | varlist      'homes.adoc'
articles                                   | varlist   'articles.adoc'
slides                                     | varlist 'slides.eslaides'
categories                                 | varlist 'categories.adoc'
indexes                                    | varlist    'indexes.adoc'
indexes | sed 's|/index\.adoc$|/feed.xml|' | varlist 'feeds.xml'
find src/content/img/ -name '*.svg' | sort | varlist 'images.svg'
media                                      | varlist 'sources.media'
tarballs                                   | varlist 'sources.tarballs'
extras                                     | varlist 'sources.extras'
find po/*.po po/*.pot                      | varlist 'sources.po'
music                                      | varlist 'music.ly'
find src/content/music/*.ogg 2>/dev/null   | varlist 'music.ogg'
find src/content/music/*.ly.include 2>/dev/null | \
	varlist 'music.include'

# lilypond emits the score alongside the MIDI, from one run.
music | sed 's/^\(.*\)\.ly$/\1.pdf:\t\1.midi/'

## A short link points into the built tree, so on a fresh clone it
## dangles, and make --- which stats through a symlink --- reports no
## rule for a file that is right there.  Name the page each one points
## at as what the link is made from.
shortener | while read -r link; do
	target="$(dirname "$link")/$(readlink "$link")"
	printf '%s:\t%s\n' "$link" \
		"$(printf '%s\n' "$target" | normalize)"
done


## What this site has, for mkwb to say what each artifact is
## built from.  Finding them is the site's half: only it knows
## where its pages live, or that it keeps decks and no music.
{
	pages      | sed 's/^/page\t/'
	homes      | sed 's/^/home\t/'
	articles   | sed 's/^/article\t/'
	indexes    | sed 's/^/index\t/'
	categories | sed 's/^/category\t/'
	slides     | sed 's/^/slide\t/'
	media      | sed 's/^/media\t/'
} | mkwb deps src/config.json

## What po4a writes.  Naming them here is what lets them be
## derived: deps.mk is written before make runs, so a translation
## that is not named cannot be built, and would have to be
## committed instead.
translations | varlist 'translations'

## One run writes all of them, so they hang off a stamp rather
## than each invoking po4a for itself.
printf 'src/i18n.stamp: po/po4a.cfg %s\n' "$(find po -maxdepth 1 -name '*.po' | sort | tr '\n' ' ')"
printf '\tpo4a po/po4a.cfg\n'
printf '\ttouch $@\n\n'
printf '$(translations): src/i18n.stamp\n\n'


## The map mkwb conf reads to link a page to its translations,
## written out here so it cannot drift from po4a.cfg.
printf '\ntranslations.txt = src/translations.txt\n\n'
printf 'src/translations.txt: deps.mk\n'
printf '\tprintf %s \\\n' "'%s\\n'"
po4a_sets | grep '\.adoc' | sed "s/^/\t\t'/; s/\$/' \\\\/"
printf '\t\t> $@\n\n'


## The canonical rules, inlined so no include is needed
## at make time.
echo
mkwb rules


## Everything mkwb renders reads the site's wording table, which
## po4a writes.  The rules above define the variables, so this
## comes after them.  It has to name what renders rather than
## what a page is made of: an article never writes a .htmlheader
## of its own, so naming that missed every one of them.
printf '\n$(sources.html) $(sources.json): src/i18n.stamp\n'
printf '$(listings.htmllisting) $(categories.xml): src/i18n.stamp\n'
printf '$(articles.feedentry): src/i18n.stamp\n'