-
Notifications
You must be signed in to change notification settings - Fork 1
/
makelinks
executable file
·36 lines (28 loc) · 1.31 KB
/
makelinks
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
#!/bin/bash
set -o errexit -o nounset -o pipefail
# Create symbolic links to PDF files in the repo named with prefixes of their
# SHA256 hashes that can serve as short permalinks.
#
# In the first version, these links were three characters long. After we added
# some new PDFs and saw a collision, they were extended to six characters. The
# original three-character links need to stay in the repo so old links keep
# working.
mkdir -p links
# Start with no links and recreate them all.
# Well, *almost* no links: we leave behind older, shorter links.
find links/ -name '??????' -type l -delete
find pdfjs/web/ -name '??????.*' -type l -delete
# Collect SHA256 for all PDFs in docs/
sha256sums=$(find docs -iname '*.pdf' -exec shasum -a 256 {} +)
# For each PDF, create a symlink for the file and for the viewer URL.
while read hash filename; do
# If there happens to be a collision, `ln` will fail.
ln -s "../${filename}" "links/${hash:0:6}"
ln -s "viewer2.html" "pdfjs/web/${hash:0:6}.html"
done <<< "${sha256sums}"
# Write an index file that maps link names to their targets.
# Uses `-printf` from GNU find. On macOS, `brew install findutils`.
GNUFIND=find
# https://formulae.brew.sh/formula/findutils
which gfind >/dev/null && GNUFIND=gfind
"${GNUFIND}" links -type l -printf '%f\t%l\n' | LANG=C sort | tee links/map.txt