]>
Commit | Line | Data |
---|---|---|
07b925b4 TI |
1 | #!/bin/sh |
2 | # SPDX-License-Identifier: GPL-2.0 | |
3 | # | |
4 | # Copy firmware files based on WHENCE list | |
5 | # | |
6 | ||
7 | verbose=: | |
2de7abd4 | 8 | prune=no |
ee91452d EV |
9 | # shellcheck disable=SC2209 |
10 | compress=cat | |
11 | compext= | |
07b925b4 | 12 | |
2de7abd4 TR |
13 | while test $# -gt 0; do |
14 | case $1 in | |
15 | -v | --verbose) | |
ad2ce8be | 16 | # shellcheck disable=SC2209 |
2de7abd4 TR |
17 | verbose=echo |
18 | shift | |
19 | ;; | |
20 | ||
21 | -P | --prune) | |
22 | prune=yes | |
23 | shift | |
24 | ;; | |
25 | ||
ee91452d | 26 | --xz) |
0a51959c | 27 | if test "$compext" = ".zst"; then |
ee91452d EV |
28 | echo "ERROR: cannot mix XZ and ZSTD compression" |
29 | exit 1 | |
30 | fi | |
31 | compress="xz --compress --quiet --stdout --check=crc32" | |
32 | compext=".xz" | |
33 | shift | |
34 | ;; | |
35 | ||
36 | --zstd) | |
0a51959c | 37 | if test "$compext" = ".xz"; then |
ee91452d EV |
38 | echo "ERROR: cannot mix XZ and ZSTD compression" |
39 | exit 1 | |
40 | fi | |
41 | # shellcheck disable=SC2209 | |
42 | compress="zstd --compress --quiet --stdout" | |
43 | compext=".zst" | |
44 | shift | |
45 | ;; | |
46 | ||
2bad80e7 JH |
47 | -*) |
48 | if test "$compress" = "cat"; then | |
49 | echo "ERROR: unknown command-line option: $1" | |
50 | exit 1 | |
51 | fi | |
52 | compress="$compress $1" | |
53 | shift | |
54 | ;; | |
2de7abd4 TR |
55 | *) |
56 | if test "x$destdir" != "x"; then | |
ad2ce8be | 57 | echo "ERROR: unknown command-line options: $*" |
2de7abd4 TR |
58 | exit 1 |
59 | fi | |
60 | ||
61 | destdir="$1" | |
62 | shift | |
63 | ;; | |
64 | esac | |
65 | done | |
07b925b4 | 66 | |
195eae59 ML |
67 | if [ -z "$destdir" ]; then |
68 | echo "ERROR: destination directory was not specified" | |
69 | exit 1 | |
70 | fi | |
71 | ||
ad2ce8be | 72 | # shellcheck disable=SC2162 # file/folder name can include escaped symbols |
db99828b | 73 | grep -E '^(RawFile|File):' WHENCE | sed -E -e 's/^(RawFile|File): */\1 /;s/"//g' | while read k f; do |
07b925b4 | 74 | test -f "$f" || continue |
40fa2b20 | 75 | install -d "$destdir/$(dirname "$f")" |
ee91452d | 76 | $verbose "copying/compressing file $f$compext" |
db99828b | 77 | if test "$compress" != "cat" && test "$k" = "RawFile"; then |
ee91452d EV |
78 | $verbose "compression will be skipped for file $f" |
79 | cat "$f" > "$destdir/$f" | |
80 | else | |
81 | $compress "$f" > "$destdir/$f$compext" | |
82 | fi | |
07b925b4 TI |
83 | done |
84 | ||
4c55675d ML |
85 | $verbose "Finding duplicate files" |
86 | rdfind -makesymlinks true -makeresultsfile false "$destdir" >/dev/null | |
87 | find "$destdir" -type l | while read -r l; do | |
88 | target="$(realpath "$l")" | |
89 | $verbose "Correcting path for $l" | |
90 | ln -fs "$(realpath --relative-to="$(dirname "$(realpath -s "$l")")" "$target")" "$l" | |
91 | done | |
92 | ||
ad2ce8be | 93 | # shellcheck disable=SC2162 # file/folder name can include escaped symbols |
77f31a80 | 94 | grep -E '^Link:' WHENCE | sed -e 's/^Link: *//g;s/-> //g' | while read f d; do |
ee91452d EV |
95 | if test -L "$f$compext"; then |
96 | test -f "$destdir/$f$compext" && continue | |
97 | $verbose "copying link $f$compext" | |
40fa2b20 | 98 | install -d "$destdir/$(dirname "$f")" |
ee91452d | 99 | cp -d "$f$compext" "$destdir/$f$compext" |
2de7abd4 TR |
100 | |
101 | if test "x$d" != "x"; then | |
67bf50e7 | 102 | target="$(readlink "$f")" |
2de7abd4 TR |
103 | |
104 | if test "x$target" != "x$d"; then | |
105 | $verbose "WARNING: inconsistent symlink target: $target != $d" | |
106 | else | |
107 | if test "x$prune" != "xyes"; then | |
108 | $verbose "WARNING: unneeded symlink detected: $f" | |
109 | else | |
110 | $verbose "WARNING: pruning unneeded symlink $f" | |
ee91452d | 111 | rm -f "$f$compext" |
2de7abd4 TR |
112 | fi |
113 | fi | |
114 | else | |
115 | $verbose "WARNING: missing target for symlink $f" | |
116 | fi | |
117 | else | |
b6ea35ff BD |
118 | directory="$destdir/$(dirname "$f")" |
119 | install -d "$directory" | |
120 | target="$(cd "$directory" && realpath -m -s "$d")" | |
121 | if test -d "$target"; then | |
122 | $verbose "creating link $f -> $d" | |
123 | ln -s "$d" "$destdir/$f" | |
124 | else | |
125 | $verbose "creating link $f$compext -> $d$compext" | |
126 | ln -s "$d$compext" "$destdir/$f$compext" | |
127 | fi | |
2de7abd4 | 128 | fi |
07b925b4 TI |
129 | done |
130 | ||
131 | exit 0 | |
2de7abd4 TR |
132 | |
133 | # vim: et sw=4 sts=4 ts=4 |