1 |
#!/bin/sh |
2 |
# |
3 |
# addlicense -- a script to insert license comments automatically |
4 |
# |
5 |
# This script is part of OSN Commons. Copyright (C) 2024 OSN Developers. |
6 |
# |
7 |
# OSN Commons is free software: you can redistribute it and/or modify it |
8 |
# under the terms of the GNU General Public License as published by |
9 |
# the Free Software Foundation, either version 3 of the License, or |
10 |
# (at your option) any later version. |
11 |
# |
12 |
# OSN Commons is distributed in the hope that it will be useful, |
13 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 |
# GNU General Public License for more details. |
16 |
# |
17 |
# You should have received a copy of the GNU General Public License |
18 |
# along with OSN Commons. If not, see <https://www.gnu.org/licenses/>. |
19 |
# |
20 |
|
21 |
me=$0 |
22 |
canonical_name=$(echo $me | rev | cut -d'/' -f1 | rev) |
23 |
|
24 |
if [ $(echo $me | cut -c1) = "/" ]; then |
25 |
me=$canonical_name |
26 |
fi |
27 |
|
28 |
version="1.0.0" |
29 |
|
30 |
usage() { |
31 |
echo "Usage: $me [options...] [[directory...] | [-]]" |
32 |
echo "Automatically insert license comments to the project's " |
33 |
echo "source files." |
34 |
echo "" |
35 |
echo "Options:" |
36 |
echo " -e, --exclude Add an exclusion pattern" |
37 |
echo " -i, --include Add an inclusion pattern" |
38 |
echo " -E, --exclude-file Exclude a file path" |
39 |
echo " -I, --include-file Include a file path" |
40 |
echo " -h, --help Show this help and exit" |
41 |
echo " -v, --version Show version info and exit" |
42 |
echo " -f, --file Specify a file containing the license" |
43 |
echo " notice to insert" |
44 |
echo "" |
45 |
echo "Bug reports and general questions should be sent to " |
46 |
echo "<[email protected]>." |
47 |
exit 0 |
48 |
} |
49 |
|
50 |
show_version() { |
51 |
echo "$canonical_name (OSN Commons) v$version" |
52 |
echo "" |
53 |
echo "Copyright (C) 2024 OSN Developers." |
54 |
echo "License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>." |
55 |
echo "This is free software: you are free to change and redistribute it." |
56 |
echo "There is NO WARRANTY, to the extent permitted by law." |
57 |
echo "" |
58 |
echo "Written by Ar Rakin." |
59 |
exit 0 |
60 |
} |
61 |
|
62 |
trim_string() { |
63 |
echo "$1" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//' |
64 |
} |
65 |
|
66 |
targets="" |
67 |
exclpats="" |
68 |
inclpats="" |
69 |
exclfiles="" |
70 |
inclfiles="" |
71 |
license_notice_file="" |
72 |
license_notice="/* |
73 |
* This program is free software: you can redistribute it and/or modify |
74 |
* it under the terms of the GNU General Public License as published by |
75 |
* the Free Software Foundation, either version 3 of the License, or |
76 |
* (at your option) any later version. |
77 |
* |
78 |
* This program is distributed in the hope that it will be useful, |
79 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
80 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
81 |
* GNU General Public License for more details. |
82 |
* |
83 |
* You should have received a copy of the GNU General Public License |
84 |
* along with this program. If not, see <https://www.gnu.org/licenses/>. |
85 |
*/" |
86 |
stdin_read="" |
87 |
|
88 |
while [ $# -gt 0 ]; do |
89 |
case "$1" in |
90 |
-h | --help) |
91 |
usage |
92 |
;; |
93 |
|
94 |
-v | --version) |
95 |
show_version |
96 |
;; |
97 |
|
98 |
-e | --exclude) |
99 |
if [ -z "$2" ]; then |
100 |
echo "$me: option requires an argument -- '$1'" >&2 |
101 |
echo "Try '$me --help' for more detailed information." >&2 |
102 |
exit 2 |
103 |
fi |
104 |
|
105 |
if [ "$2" != "$(echo "$2" | tr -d ':')" ]; then |
106 |
echo "$me: exclusion patterns must not contain colons (':')" >&2 |
107 |
exit 2 |
108 |
fi |
109 |
|
110 |
if [ -z "$exclpats" ]; then |
111 |
exclpats="$2" |
112 |
else |
113 |
exclpats="$exclpats:$2" |
114 |
fi |
115 |
|
116 |
shift |
117 |
;; |
118 |
|
119 |
-i | --include) |
120 |
if [ -z "$2" ]; then |
121 |
echo "$me: option requires an argument -- '$1'" >&2 |
122 |
echo "Try '$me --help' for more detailed information." >&2 |
123 |
exit 2 |
124 |
fi |
125 |
|
126 |
if [ "$2" != "$(echo "$2" | tr -d ':')" ]; then |
127 |
echo "$me: inclusion patterns must not contain colons (':')" >&2 |
128 |
exit 2 |
129 |
fi |
130 |
|
131 |
if [ -z "$inclpats" ]; then |
132 |
inclpats="$2" |
133 |
else |
134 |
inclpats="$inclpats:$2" |
135 |
fi |
136 |
|
137 |
shift |
138 |
;; |
139 |
|
140 |
-E | --exclude-file) |
141 |
if [ -z "$2" ]; then |
142 |
echo "$me: option requires an argument -- '$1'" >&2 |
143 |
echo "Try '$me --help' for more detailed information." >&2 |
144 |
exit 2 |
145 |
fi |
146 |
|
147 |
if [ "$2" != "$(echo "$2" | tr -d ':')" ]; then |
148 |
echo "$me: file paths must not contain colons (':')" >&2 |
149 |
exit 2 |
150 |
fi |
151 |
|
152 |
if [ ! -e "$2" ]; then |
153 |
echo "$me: $2: No such file or directory" >&2 |
154 |
exit 2 |
155 |
fi |
156 |
|
157 |
file=$(readlink -f "$2") |
158 |
|
159 |
if [ $? -ne 0 ]; then |
160 |
echo "$me: $2: Ignoring" >&2 |
161 |
else |
162 |
if [ -z "$exclfiles" ]; then |
163 |
exclfiles="$file" |
164 |
else |
165 |
exclfiles="$exclfiles:$file" |
166 |
fi |
167 |
fi |
168 |
|
169 |
shift |
170 |
;; |
171 |
|
172 |
-I | --include-file) |
173 |
if [ -z "$2" ]; then |
174 |
echo "$me: option requires an argument -- '$1'" >&2 |
175 |
echo "Try '$me --help' for more detailed information." >&2 |
176 |
exit 2 |
177 |
fi |
178 |
|
179 |
if [ "$2" != "$(echo "$2" | tr -d ':')" ]; then |
180 |
echo "$me: file paths must not contain colons (':')" >&2 |
181 |
exit 2 |
182 |
fi |
183 |
|
184 |
if [ ! -e "$2" ]; then |
185 |
echo "$me: $2: No such file or directory" >&2 |
186 |
exit 2 |
187 |
fi |
188 |
|
189 |
file=$(readlink -f "$2") |
190 |
|
191 |
if [ $? -ne 0 ]; then |
192 |
echo "$me: $2: Ignoring" >&2 |
193 |
else |
194 |
if [ -z "$inclfiles" ]; then |
195 |
inclfiles="$file" |
196 |
else |
197 |
inclfiles="$inclfiles:$file" |
198 |
fi |
199 |
fi |
200 |
|
201 |
shift |
202 |
;; |
203 |
|
204 |
-f | --file) |
205 |
if [ -z "$2" ]; then |
206 |
echo "$me: option requires an argument -- '$1'" >&2 |
207 |
echo "Try '$me --help' for more detailed information." >&2 |
208 |
exit 2 |
209 |
fi |
210 |
|
211 |
if [ "$2" = "-" ]; then |
212 |
if [ -t 0 ]; then |
213 |
echo "$me: nothing to read from standard input" >&2 |
214 |
exit 2 |
215 |
fi |
216 |
|
217 |
if [ ! -z "$stdin_read" ] && [ "$stdin_read" != "notice" ]; then |
218 |
echo "$me: multiple options are being directed to standard input" >&2 |
219 |
exit 2 |
220 |
fi |
221 |
|
222 |
license_notice="$( |
223 |
cat |
224 |
echo E |
225 |
)" |
226 |
stdin_read="notice" |
227 |
else |
228 |
if [ ! -f "$2" ]; then |
229 |
echo "$me: $2: No such file or directory" >&2 |
230 |
exit 2 |
231 |
fi |
232 |
|
233 |
license_notice_file="$2" |
234 |
license_notice="$( |
235 |
cat "$2" |
236 |
echo E |
237 |
)" |
238 |
fi |
239 |
|
240 |
license_notice=${license_notice%E} |
241 |
shift |
242 |
;; |
243 |
|
244 |
-) |
245 |
if [ -t 0 ]; then |
246 |
echo "$me: nothing to read from standard input" >&2 |
247 |
exit 2 |
248 |
fi |
249 |
|
250 |
if [ ! -z "$stdin_read" ] && [ "$stdin_read" != "targets" ]; then |
251 |
echo "$me: multiple options are being directed to standard input" >&2 |
252 |
exit 2 |
253 |
fi |
254 |
|
255 |
while IFS='' read -r line; do |
256 |
if [ "$line" != "$(echo "$line" | tr -d ':')" ] || [ -z "$line" ]; then |
257 |
echo "$me: target paths must not contain colons (':')" >&2 |
258 |
exit 2 |
259 |
fi |
260 |
|
261 |
if [ -z "$targets" ]; then |
262 |
targets="$line" |
263 |
else |
264 |
targets="$targets:$line" |
265 |
fi |
266 |
done |
267 |
|
268 |
stdin_read="targets" |
269 |
;; |
270 |
|
271 |
-*) |
272 |
echo "$me: Invalid option -- '$1'" >&2 |
273 |
echo "Try '$me --help' for more detailed information." >&2 |
274 |
exit 2 |
275 |
;; |
276 |
|
277 |
*) |
278 |
if [ "$1" != "$(echo "$1" | tr -d ':')" ] || [ -z "$1" ]; then |
279 |
echo "$me: target paths must not contain colons (':')" >&2 |
280 |
exit 2 |
281 |
fi |
282 |
|
283 |
if [ -z "$targets" ]; then |
284 |
targets="$1" |
285 |
else |
286 |
targets="$targets:$1" |
287 |
fi |
288 |
;; |
289 |
esac |
290 |
|
291 |
shift |
292 |
done |
293 |
|
294 |
if [ -z "$targets" ]; then |
295 |
echo "$me: No target operands specified" >&2 |
296 |
echo "Try '$me --help' for more detailed information" >&2 |
297 |
exit 2 |
298 |
fi |
299 |
|
300 |
success=0 |
301 |
|
302 |
insert_license() { |
303 |
set -e |
304 |
local file="$1" |
305 |
local tmpfile="$(mktemp)" |
306 |
|
307 |
echo "$license_notice" >$tmpfile |
308 |
cat $file >>$tmpfile |
309 |
mv $tmpfile $file |
310 |
set +e |
311 |
} |
312 |
|
313 |
process_files() { |
314 |
echo "$targets" | while IFS=':' read -r target; do |
315 |
local stat_out=$(stat "$target" 2>&1) |
316 |
|
317 |
if [ $? -ne 0 ]; then |
318 |
echo "$me: $(echo $stat_out | cut -c 7-)" >&2 |
319 |
continue |
320 |
fi |
321 |
|
322 |
echo "$me: scanning: $target" |
323 |
local find_cmd="find '$target' -type f ! -path \"*/*~\" \ |
324 |
! -path \"*/.git/*\" ! -path \"*/.svn/*\" \ |
325 |
! -path \"*/.hg/*\" ! -path \"*/CVS/*\" \ |
326 |
! -path \"*/.bzr/*\" ! -path \"$me\"" |
327 |
|
328 |
if [ ! -z "$exclpats" ]; then |
329 |
local ifs="$IFS" |
330 |
|
331 |
IFS=":" |
332 |
|
333 |
for exclpat in $exclpats; do |
334 |
find_cmd="$find_cmd ! -path \"$exclpat\"" |
335 |
done |
336 |
|
337 |
IFS="$ifs" |
338 |
fi |
339 |
|
340 |
if [ ! -z "$inclpats" ]; then |
341 |
local ifs="$IFS" |
342 |
|
343 |
IFS=":" |
344 |
|
345 |
for inclpat in $inclpats; do |
346 |
find_cmd="$find_cmd -path \"$inclpat\"" |
347 |
done |
348 |
|
349 |
IFS="$ifs" |
350 |
fi |
351 |
|
352 |
local files=$(eval $find_cmd) |
353 |
|
354 |
if [ -z "$files" ]; then |
355 |
echo "$me: no files found" >&2 |
356 |
continue |
357 |
fi |
358 |
|
359 |
local r_license_notice_file |
360 |
|
361 |
if [ ! -z "$license_notice_file" ]; then |
362 |
r_license_notice_file=$(readlink -f "$license_notice_file") |
363 |
fi |
364 |
|
365 |
for file in $files; do |
366 |
local rpath=$(readlink -f "$file") |
367 |
|
368 |
if [ ! -z "$license_notice_file" ] && [ "$rpath" = "$r_license_notice_file" ]; then |
369 |
continue |
370 |
fi |
371 |
|
372 |
if [ ! -z "$exclfiles" ]; then |
373 |
local ifs="$IFS" |
374 |
|
375 |
IFS=":" |
376 |
|
377 |
for exclfile in $exclfiles; do |
378 |
if [ "$rpath" = "$exclfile" ]; then |
379 |
continue 2 |
380 |
fi |
381 |
done |
382 |
|
383 |
IFS="$ifs" |
384 |
fi |
385 |
|
386 |
if [ ! -z "$inclfiles" ]; then |
387 |
local ifs="$IFS" |
388 |
|
389 |
IFS=":" |
390 |
|
391 |
for inclfile in $inclfiles; do |
392 |
if [ "$rpath" = "$inclfile" ]; then |
393 |
break |
394 |
fi |
395 |
done |
396 |
|
397 |
if [ "$rpath" != "$inclfile" ]; then |
398 |
continue |
399 |
fi |
400 |
|
401 |
IFS="$ifs" |
402 |
fi |
403 |
|
404 |
if file "$file" -b | grep -Eq 'binary|ELF|executable|data'; then |
405 |
echo "$me: skipping binary/data file: $file" |
406 |
continue |
407 |
fi |
408 |
|
409 |
local license_notice_size=$(echo "$license_notice" | wc -c) |
410 |
local part=$(sed ':a; /^$/d; $!{N; ba}; s/^[[:space:]]*//' $file | head -c $license_notice_size) |
411 |
|
412 |
if [ "$license_notice" = "$part" ]; then |
413 |
echo "$me: license notice already present in: $file" |
414 |
success=1 |
415 |
continue |
416 |
fi |
417 |
|
418 |
echo "$me: inserting license comment to: $file" |
419 |
insert_license "$file" |
420 |
done |
421 |
|
422 |
success=1 |
423 |
done |
424 |
} |
425 |
|
426 |
process_files |
427 |
|
428 |
if [ $success -ne 1 ]; then |
429 |
exit 1 |
430 |
fi |