]>
Commit | Line | Data |
---|---|---|
1 | #!/bin/sh | |
2 | # SPDX-License-Identifier: GPL-2.0 | |
3 | # | |
4 | # Copy firmware files based on WHENCE list | |
5 | # | |
6 | ||
7 | verbose=: | |
8 | prune=no | |
9 | # shellcheck disable=SC2209 | |
10 | compress=cat | |
11 | compext= | |
12 | ||
13 | while test $# -gt 0; do | |
14 | case $1 in | |
15 | -v | --verbose) | |
16 | # shellcheck disable=SC2209 | |
17 | verbose=echo | |
18 | shift | |
19 | ;; | |
20 | ||
21 | -P | --prune) | |
22 | prune=yes | |
23 | shift | |
24 | ;; | |
25 | ||
26 | --xz) | |
27 | if test "$compext" = ".zst"; then | |
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) | |
37 | if test "$compext" = ".xz"; then | |
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 | ||
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 | ;; | |
55 | *) | |
56 | if test "x$destdir" != "x"; then | |
57 | echo "ERROR: unknown command-line options: $*" | |
58 | exit 1 | |
59 | fi | |
60 | ||
61 | destdir="$1" | |
62 | shift | |
63 | ;; | |
64 | esac | |
65 | done | |
66 | ||
67 | if [ -z "$destdir" ]; then | |
68 | echo "ERROR: destination directory was not specified" | |
69 | exit 1 | |
70 | fi | |
71 | ||
72 | if ! which rdfind 2>/dev/null >/dev/null; then | |
73 | echo "ERROR: rdfind is not installed" | |
74 | exit 1 | |
75 | fi | |
76 | ||
77 | # shellcheck disable=SC2162 # file/folder name can include escaped symbols | |
78 | grep -E '^(RawFile|File):' WHENCE | sed -E -e 's/^(RawFile|File): */\1 /;s/"//g' | while read k f; do | |
79 | test -f "$f" || continue | |
80 | install -d "$destdir/$(dirname "$f")" | |
81 | $verbose "copying/compressing file $f$compext" | |
82 | if test "$compress" != "cat" && test "$k" = "RawFile"; then | |
83 | $verbose "compression will be skipped for file $f" | |
84 | cat "$f" > "$destdir/$f" | |
85 | else | |
86 | $compress "$f" > "$destdir/$f$compext" | |
87 | fi | |
88 | done | |
89 | ||
90 | $verbose "Finding duplicate files" | |
91 | rdfind -makesymlinks true -makeresultsfile false "$destdir" >/dev/null | |
92 | find "$destdir" -type l | while read -r l; do | |
93 | target="$(realpath "$l")" | |
94 | $verbose "Correcting path for $l" | |
95 | ln -fs "$(realpath --relative-to="$(dirname "$(realpath -s "$l")")" "$target")" "$l" | |
96 | done | |
97 | ||
98 | # shellcheck disable=SC2162 # file/folder name can include escaped symbols | |
99 | grep -E '^Link:' WHENCE | sed -e 's/^Link: *//g;s/-> //g' | while read f d; do | |
100 | if test -L "$f$compext"; then | |
101 | test -f "$destdir/$f$compext" && continue | |
102 | $verbose "copying link $f$compext" | |
103 | install -d "$destdir/$(dirname "$f")" | |
104 | cp -d "$f$compext" "$destdir/$f$compext" | |
105 | ||
106 | if test "x$d" != "x"; then | |
107 | target="$(readlink "$f")" | |
108 | ||
109 | if test "x$target" != "x$d"; then | |
110 | $verbose "WARNING: inconsistent symlink target: $target != $d" | |
111 | else | |
112 | if test "x$prune" != "xyes"; then | |
113 | $verbose "WARNING: unneeded symlink detected: $f" | |
114 | else | |
115 | $verbose "WARNING: pruning unneeded symlink $f" | |
116 | rm -f "$f$compext" | |
117 | fi | |
118 | fi | |
119 | else | |
120 | $verbose "WARNING: missing target for symlink $f" | |
121 | fi | |
122 | else | |
123 | directory="$destdir/$(dirname "$f")" | |
124 | install -d "$directory" | |
125 | target="$(cd "$directory" && realpath -m -s "$d")" | |
126 | if test -d "$target"; then | |
127 | $verbose "creating link $f -> $d" | |
128 | ln -s "$d" "$destdir/$f" | |
129 | else | |
130 | $verbose "creating link $f$compext -> $d$compext" | |
131 | ln -s "$d$compext" "$destdir/$f$compext" | |
132 | fi | |
133 | fi | |
134 | done | |
135 | ||
136 | exit 0 | |
137 | ||
138 | # vim: et sw=4 sts=4 ts=4 |