| 1 |
#!/bin/sh |
| 2 |
# SPDX-License-Identifier: GPL-2.0 |
| 3 |
# |
| 4 |
# Copy firmware files based on WHENCE list |
| 5 |
# |
| 6 |
|
| 7 |
verbose=: |
| 8 |
# shellcheck disable=SC2209 |
| 9 |
compress=cat |
| 10 |
compext= |
| 11 |
destdir= |
| 12 |
|
| 13 |
err() { |
| 14 |
printf "ERROR: %s\n" "$*" |
| 15 |
exit 1 |
| 16 |
} |
| 17 |
|
| 18 |
warn() { |
| 19 |
printf "WARNING: %s\n" "$*" |
| 20 |
} |
| 21 |
|
| 22 |
while test $# -gt 0; do |
| 23 |
case $1 in |
| 24 |
-v | --verbose) |
| 25 |
# shellcheck disable=SC2209 |
| 26 |
verbose=echo |
| 27 |
shift |
| 28 |
;; |
| 29 |
|
| 30 |
--xz) |
| 31 |
if test "$compext" = ".zst"; then |
| 32 |
err "cannot mix XZ and ZSTD compression" |
| 33 |
fi |
| 34 |
compress="xz --compress --quiet --stdout --check=crc32" |
| 35 |
compext=".xz" |
| 36 |
shift |
| 37 |
;; |
| 38 |
|
| 39 |
--zstd) |
| 40 |
if test "$compext" = ".xz"; then |
| 41 |
err "cannot mix XZ and ZSTD compression" |
| 42 |
fi |
| 43 |
# shellcheck disable=SC2209 |
| 44 |
compress="zstd --compress --quiet --stdout" |
| 45 |
compext=".zst" |
| 46 |
shift |
| 47 |
;; |
| 48 |
|
| 49 |
*) |
| 50 |
if test -n "$destdir"; then |
| 51 |
err "unknown command-line options: $*" |
| 52 |
fi |
| 53 |
|
| 54 |
destdir="$1" |
| 55 |
shift |
| 56 |
;; |
| 57 |
esac |
| 58 |
done |
| 59 |
|
| 60 |
if test -z "$destdir"; then |
| 61 |
err "destination directory was not specified" |
| 62 |
fi |
| 63 |
|
| 64 |
if test -d "$destdir"; then |
| 65 |
find "$destdir" -type d -empty >/dev/null || warn "destination folder is not empty." |
| 66 |
fi |
| 67 |
|
| 68 |
if test -e .git/config; then |
| 69 |
$verbose "Checking that WHENCE file is formatted properly" |
| 70 |
./check_whence.py || err "check_whence.py has detected errors." |
| 71 |
fi |
| 72 |
|
| 73 |
# shellcheck disable=SC2162 # file/folder name can include escaped symbols |
| 74 |
grep -E '^(RawFile|File):' WHENCE | sed -E -e 's/^(RawFile|File): */\1 /;s/"//g' | while read k f; do |
| 75 |
install -d "$destdir/$(dirname "$f")" |
| 76 |
$verbose "copying/compressing file $f$compext" |
| 77 |
if test "$compress" != "cat" && test "$k" = "RawFile"; then |
| 78 |
$verbose "compression will be skipped for file $f" |
| 79 |
cat "$f" > "$destdir/$f" |
| 80 |
else |
| 81 |
$compress "$f" > "$destdir/$f$compext" |
| 82 |
fi |
| 83 |
done |
| 84 |
|
| 85 |
# shellcheck disable=SC2162 # file/folder name can include escaped symbols |
| 86 |
grep -E '^Link:' WHENCE | sed -e 's/^Link: *//g;s/-> //g' | while read l t; do |
| 87 |
directory="$destdir/$(dirname "$l")" |
| 88 |
install -d "$directory" |
| 89 |
target="$(cd "$directory" && realpath -m -s "$t")" |
| 90 |
if test -e "$target"; then |
| 91 |
$verbose "creating link $l -> $t" |
| 92 |
ln -s "$t" "$destdir/$l" |
| 93 |
else |
| 94 |
$verbose "creating link $l$compext -> $t$compext" |
| 95 |
ln -s "$t$compext" "$destdir/$l$compext" |
| 96 |
fi |
| 97 |
done |
| 98 |
|
| 99 |
# Verify no broken symlinks |
| 100 |
if test "$(find "$destdir" -xtype l | wc -l)" -ne 0 ; then |
| 101 |
err "Broken symlinks found:\\n$(find "$destdir" -xtype l)" |
| 102 |
fi |
| 103 |
|
| 104 |
exit 0 |
| 105 |
|
| 106 |
# vim: et sw=4 sts=4 ts=4 |