| 1 |
#!/bin/sh -eu |
| 2 |
# SPDX-License-Identifier: GPL-2.0-or-later |
| 3 |
|
| 4 |
COMPRESS="${COMPRESS?A command to compress files is required.}" |
| 5 |
tempfile="$(mktemp "${TMPDIR-/tmp}/kbd.compress.XXXXXX")" |
| 6 |
|
| 7 |
fatal() |
| 8 |
{ |
| 9 |
[ -z "$filename" ] || [ ! -f "$tempfile" ] || |
| 10 |
rm -f -- "$tempfile" |
| 11 |
printf >&2 'compress.sh: ERROR: %s\n' "$*" |
| 12 |
exit 1 |
| 13 |
} |
| 14 |
|
| 15 |
for filename in "$@"; do |
| 16 |
if [ -f "$filename" ] && [ -L "$filename" ]; then |
| 17 |
printf '%s\n' "$filename" |
| 18 |
readlink -f "$filename" |
| 19 |
fi |
| 20 |
done > "$tempfile" |
| 21 |
|
| 22 |
for filename in "$@"; do |
| 23 |
[ -f "$filename" ] && [ ! -L "$filename" ] || |
| 24 |
continue |
| 25 |
|
| 26 |
case "$filename" in |
| 27 |
*/ERRORS*|*/README*) |
| 28 |
continue |
| 29 |
;; |
| 30 |
esac |
| 31 |
|
| 32 |
$COMPRESS "$filename" || |
| 33 |
fatal "$filename: Compression failed." |
| 34 |
|
| 35 |
rm -f -- "$filename" ||: |
| 36 |
done |
| 37 |
|
| 38 |
eof= |
| 39 |
symlink= |
| 40 |
|
| 41 |
while [ -z "$eof" ]; do |
| 42 |
filename= |
| 43 |
read -r filename || eof=1 |
| 44 |
|
| 45 |
if [ -z "$symlink" ]; then |
| 46 |
symlink="$filename" |
| 47 |
continue |
| 48 |
fi |
| 49 |
|
| 50 |
i=0 |
| 51 |
for compressed in "$filename".*; do |
| 52 |
[ -f "$compressed" ] || |
| 53 |
continue |
| 54 |
|
| 55 |
[ "$i" -eq 0 ] || |
| 56 |
fatal "$symlink: Too many candidates were found to fix symlink: $(echo "$filename".*)" |
| 57 |
|
| 58 |
i=$(( $i + 1 )) |
| 59 |
|
| 60 |
suffix="${compressed##$filename.}" |
| 61 |
|
| 62 |
ln -s -r -- "$compressed" "$symlink.$suffix" || |
| 63 |
fatal "Unable to create symlink: $symlink.$suffix" |
| 64 |
|
| 65 |
rm -f -- "$symlink" ||: |
| 66 |
done |
| 67 |
|
| 68 |
symlink= |
| 69 |
done < "$tempfile" |
| 70 |
|
| 71 |
rm -f -- "$tempfile" |
| 72 |
tempfile= |