Debian Codegraph @

firmware/copy-firmware.sh

raw symbols SCIP
1 #!/bin/shscip-shell filesystem . . `/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 num_jobs=1
13
14 usagescip-shell shell shell-project 0.0.0 usage().() {
15 echoscip-shell system . . echo. "Usage: $0 [-v] [-jN] [--xz|--zstd] <destination directory>"
16 }
17
18 errscip-shell shell shell-project 0.0.0 err().() {
19 printfscip-shell system . . printf. "ERROR: %s\n" "$*"
20 usagescip-shell shell shell-project 0.0.0 usage().
21 exit 1
22 }
23
24 warnscip-shell shell shell-project 0.0.0 warn().() {
25 printfscip-shell system . . printf. "WARNING: %s\n" "$*"
26 }
27
28 has_gnu_parallelscip-shell shell shell-project 0.0.0 has_gnu_parallel().() {
29 if command -v parallel > /dev/nullscip-shell filesystem . . `/dev/null`.; then
30 # The moreutils package comes with a simpler version of "parallel"
31 # that does not support the --version or -a options. Check for
32 # that first. In some distros, installing the "parallel" package
33 # will replace the moreutils version with the GNU version.
34 if ! parallel --version > /dev/nullscip-shell filesystem . . `/dev/null`. 2>&1; then
35 return 1
36 fi
37 if parallel --version | grepscip-shell system . . grep. -Fqi 'gnu parallel'; then
38 return 0
39 fi
40 fi
41 return 1
42 }
43
44 while testscip-shell system . . test. $# -gt 0; do
45 case $1 in
46 -v | --verbose)
47 # shellcheck disable=SC2209
48 verbose=echo
49 shift
50 ;;
51
52 -j*)
53 num_jobs=$(echoscip-shell system . . echo. "$1" | sedscip-shell system . . sed. 's/-j//')
54 num_jobs=${num_jobs:-1}
55 if [scip-shell system . . `[`. "$num_jobs" -gt 1 ] && ! has_gnu_parallelscip-shell shell shell-project 0.0.0 has_gnu_parallel().; then
56 errscip-shell shell shell-project 0.0.0 err(). "the GNU parallel command is required to use -j"
57 fi
58 parallel_args_file=$(mktempscip-shell system . . mktemp.)
59 trap 'rm -f $parallel_args_file' EXIT INT QUIT TERM
60 shift
61 ;;
62
63 --xz)
64 if testscip-shell system . . test. "$compext" = ".zst"; then
65 errscip-shell shell shell-project 0.0.0 err(). "cannot mix XZ and ZSTD compression"
66 fi
67 compress="xz --compress --quiet --stdout --check=crc32"
68 compext=".xz"
69 shift
70 ;;
71
72 --zstd)
73 if testscip-shell system . . test. "$compext" = ".xz"; then
74 errscip-shell shell shell-project 0.0.0 err(). "cannot mix XZ and ZSTD compression"
75 fi
76 # shellcheck disable=SC2209
77 compress="zstd --compress --quiet --stdout"
78 compext=".zst"
79 shift
80 ;;
81
82 -h|--help)
83 usagescip-shell shell shell-project 0.0.0 usage().
84 exit 1
85 ;;
86
87 -*)
88 # Ignore anything else that begins with - because that confuses
89 # the "test" command below
90 warnscip-shell shell shell-project 0.0.0 warn(). "ignoring option $1"
91 shift
92 ;;
93
94 *)
95 if testscip-shell system . . test. -n "$destdir"; then
96 errscip-shell shell shell-project 0.0.0 err(). "unknown command-line options: $*"
97 fi
98
99 destdir="$1"
100 shift
101 ;;
102 esac
103 done
104
105 if testscip-shell system . . test. -z "$destdir"; then
106 errscip-shell shell shell-project 0.0.0 err(). "destination directory was not specified"
107 fi
108
109 if testscip-shell system . . test. -d "$destdir"; then
110 findscip-shell system . . find. "$destdir" -type d -empty >/dev/nullscip-shell filesystem . . `/dev/null`. || warnscip-shell shell shell-project 0.0.0 warn(). "destination folder is not empty."
111 fi
112
113 if testscip-shell system . . test. -e .git/config; then
114 $verbose "Checking that WHENCE file is formatted properly"
115 ./check_whence.py || errscip-shell shell shell-project 0.0.0 err(). "check_whence.py has detected errors."
116 fi
117
118 # shellcheck disable=SC2162 # file/folder name can include escaped symbols
119 grepscip-shell system . . grep. -E '^(RawFile|File):' WHENCE | sedscip-shell system . . sed. -E -e 's/^(RawFile|File): */\1 /;s/"//g' | while read k f; do
120 installscip-shell system . . install. -d "$destdir/$(dirnamescip-shell system . . dirname. "$f")"
121 $verbose "copying/compressing file $f$compext"
122 if testscip-shell system . . test. "$compress" != "cat" && testscip-shell system . . test. "$k" = "RawFile"; then
123 $verbose "compression will be skipped for file $f"
124 if [scip-shell system . . `[`. "$num_jobs" -gt 1 ]; then
125 echoscip-shell system . . echo. "cat \"$f\" > \"$destdir/$f\"" >> "$parallel_args_file"
126 else
127 catscip-shell system . . cat. "$f" > "$destdir/$f"
128 fi
129 else
130 if [scip-shell system . . `[`. "$num_jobs" -gt 1 ]; then
131 echoscip-shell system . . echo. "$compress \"$f\" > \"$destdir/$f$compext\"" >> "$parallel_args_file"
132 else
133 $compress "$f" > "$destdir/$f$compext"
134 fi
135 fi
136 done
137 if [scip-shell system . . `[`. "$num_jobs" -gt 1 ]; then
138 parallel -j"$num_jobs" -a "$parallel_args_file"
139 echoscip-shell system . . echo. > "$parallel_args_file" # prepare for next run
140 fi
141
142 # shellcheck disable=SC2162 # file/folder name can include escaped symbols
143 grepscip-shell system . . grep. -E '^Link:' WHENCE | sedscip-shell system . . sed. -e 's/^Link: *//g;s/-> //g' | while read l t; do
144 directory="$destdir/$(dirnamescip-shell system . . dirname. "$l")"
145 installscip-shell system . . install. -d "$directory"
146 target="$(cd "$directory" && realpathscip-shell system . . realpath. -m -s "$t")"
147 if testscip-shell system . . test. -e "$target"; then
148 $verbose "creating link $l -> $t"
149 if [scip-shell system . . `[`. "$num_jobs" -gt 1 ]; then
150 echoscip-shell system . . echo. "ln -s \"$t\" \"$destdir/$l\"" >> "$parallel_args_file"
151 else
152 lnscip-shell system . . ln. -s "$t" "$destdir/$l"
153 fi
154 else
155 $verbose "creating link $l$compext -> $t$compext"
156 if [scip-shell system . . `[`. "$num_jobs" -gt 1 ]; then
157 echoscip-shell system . . echo. "ln -s \"$t$compext\" \"$destdir/$l$compext\"" >> "$parallel_args_file"
158 else
159 lnscip-shell system . . ln. -s "$t$compext" "$destdir/$l$compext"
160 fi
161 fi
162 done
163 if [scip-shell system . . `[`. "$num_jobs" -gt 1 ]; then
164 parallel -j"$num_jobs" -a "$parallel_args_file"
165 fi
166
167 # Verify no broken symlinks
168 if testscip-shell system . . test. "$(findscip-shell system . . find. "$destdir" -xtype l | wcscip-shell system . . wc. -l)" -ne 0 ; then
169 errscip-shell shell shell-project 0.0.0 err(). "Broken symlinks found:\n$(findscip-shell system . . find. "$destdir" -xtype l)"
170 fi
171
172 exit 0
173
174 # vim: et sw=4 sts=4 ts=4