]> git.proxmox.com Git - mirror_linux-firmware.git/blob - copy-firmware.sh
Add support for sending emails while processing a PR/patch
[mirror_linux-firmware.git] / copy-firmware.sh
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 quiet=">/dev/null"
13 rdfind_results=/dev/null
14
15 while test $# -gt 0; do
16 case $1 in
17 -v | --verbose)
18 # shellcheck disable=SC2209
19 verbose=echo
20 quiet=
21 rdfind_results=results.txt
22 shift
23 ;;
24
25 -P | --prune)
26 prune=yes
27 shift
28 ;;
29
30 --xz)
31 if test "$compext" = ".zst"; then
32 echo "ERROR: cannot mix XZ and ZSTD compression"
33 exit 1
34 fi
35 compress="xz --compress --quiet --stdout --check=crc32"
36 compext=".xz"
37 shift
38 ;;
39
40 --zstd)
41 if test "$compext" = ".xz"; then
42 echo "ERROR: cannot mix XZ and ZSTD compression"
43 exit 1
44 fi
45 # shellcheck disable=SC2209
46 compress="zstd --compress --quiet --stdout"
47 compext=".zst"
48 shift
49 ;;
50
51 -*)
52 if test "$compress" = "cat"; then
53 echo "ERROR: unknown command-line option: $1"
54 exit 1
55 fi
56 compress="$compress $1"
57 shift
58 ;;
59 *)
60 if test "x$destdir" != "x"; then
61 echo "ERROR: unknown command-line options: $*"
62 exit 1
63 fi
64
65 destdir="$1"
66 shift
67 ;;
68 esac
69 done
70
71 # shellcheck disable=SC2162 # file/folder name can include escaped symbols
72 grep -E '^(RawFile|File):' WHENCE | sed -E -e 's/^(RawFile|File): */\1 /;s/"//g' | while read k f; do
73 test -f "$f" || continue
74 install -d "$destdir/$(dirname "$f")"
75 $verbose "copying/compressing file $f$compext"
76 if test "$compress" != "cat" && test "$k" = "RawFile"; then
77 $verbose "compression will be skipped for file $f"
78 cat "$f" > "$destdir/$f"
79 else
80 $compress "$f" > "$destdir/$f$compext"
81 fi
82 done
83
84 # shellcheck disable=SC2162 # file/folder name can include escaped symbols
85 grep -E '^Link:' WHENCE | sed -e 's/^Link: *//g;s/-> //g' | while read f d; do
86 if test -L "$f$compext"; then
87 test -f "$destdir/$f$compext" && continue
88 $verbose "copying link $f$compext"
89 install -d "$destdir/$(dirname "$f")"
90 cp -d "$f$compext" "$destdir/$f$compext"
91
92 if test "x$d" != "x"; then
93 target="$(readlink "$f")"
94
95 if test "x$target" != "x$d"; then
96 $verbose "WARNING: inconsistent symlink target: $target != $d"
97 else
98 if test "x$prune" != "xyes"; then
99 $verbose "WARNING: unneeded symlink detected: $f"
100 else
101 $verbose "WARNING: pruning unneeded symlink $f"
102 rm -f "$f$compext"
103 fi
104 fi
105 else
106 $verbose "WARNING: missing target for symlink $f"
107 fi
108 else
109 directory="$destdir/$(dirname "$f")"
110 install -d "$directory"
111 target="$(cd "$directory" && realpath -m -s "$d")"
112 if test -d "$target"; then
113 $verbose "creating link $f -> $d"
114 ln -s "$d" "$destdir/$f"
115 else
116 $verbose "creating link $f$compext -> $d$compext"
117 ln -s "$d$compext" "$destdir/$f$compext"
118 fi
119 fi
120 done
121
122 $verbose rdfind -makesymlinks true "$destdir" -outputname $rdfind_results "$quiet"
123 find "$destdir" -type l | while read -r l; do
124 target="$(realpath "$l")"
125 ln -fs "$(realpath --relative-to="$(dirname "$(realpath -s "$l")")" "$target")" "$l"
126 done
127
128 exit 0
129
130 # vim: et sw=4 sts=4 ts=4