commit | author | age
|
1734a6
|
1 |
#! /usr/bin/ksh |
MT |
2 |
# |
|
3 |
# |
|
4 |
# This file and its contents are supplied under the terms of the |
|
5 |
# Common Development and Distribution License ("CDDL"), version 1.0. |
|
6 |
# You may only use this file in accordance with the terms of version |
|
7 |
# 1.0 of the CDDL. |
|
8 |
# |
|
9 |
# A full copy of the text of the CDDL should have accompanied this |
|
10 |
# source. A copy of the CDDL is also available via the Internet at |
|
11 |
# http://www.illumos.org/license/CDDL. |
|
12 |
# |
|
13 |
|
|
14 |
# |
|
15 |
# Copyright 2022 Marcel Telka |
|
16 |
# |
|
17 |
|
|
18 |
|
|
19 |
function usage |
|
20 |
{ |
|
21 |
[[ -n "$1" ]] && printf "ERROR: %s\n\n" "$1" >&2 |
08ba8d
|
22 |
printf "Usage: license-detector [-d] [-l LICENSE] LICENSE_FILE\n" >&2 |
1734a6
|
23 |
[[ -n "$1" ]] && exit 1 |
MT |
24 |
exit 0 |
|
25 |
} |
|
26 |
|
|
27 |
|
de9d5d
|
28 |
DEBUG=0 |
08ba8d
|
29 |
LICENSE="*" |
MT |
30 |
while getopts ":hdl:" OPT ; do |
1734a6
|
31 |
case "$OPT" in |
MT |
32 |
"?"|"h") usage ;; |
de9d5d
|
33 |
"d") DEBUG=1 ;; |
08ba8d
|
34 |
"l") LICENSE="*.$OPTARG" ;; |
1734a6
|
35 |
esac |
MT |
36 |
done |
|
37 |
shift $((OPTIND - 1)) |
|
38 |
|
|
39 |
(($# == 0)) && usage |
|
40 |
(($# > 1)) && usage "Too many arguments" |
|
41 |
|
|
42 |
LICENSE_FILE="$1" |
|
43 |
[[ -e "$LICENSE_FILE" ]] || usage "$LICENSE_FILE not found" |
|
44 |
[[ -d "$LICENSE_FILE" ]] && usage "$LICENSE_FILE is directory" |
|
45 |
[[ -r "$LICENSE_FILE" ]] || usage "Unable to read $LICENSE_FILE" |
de9d5d
|
46 |
|
MT |
47 |
WS_TOOLS=$(dirname $0) |
1734a6
|
48 |
|
MT |
49 |
|
|
50 |
F="$LICENSE_FILE" |
|
51 |
|
|
52 |
if grep -q -i "Artistic License" "$F" ; then |
|
53 |
if ! grep -q -i "Artistic License.*2" "$F" ; then |
0dc9b0
|
54 |
D= |
1734a6
|
55 |
grep -q "7\. C subroutines" "$F" && grep -q "10\. THIS PACKAGE IS PROVIDED" "$F" && D="Artistic-1.0-Perl" |
MT |
56 |
grep -q "7\. C or perl subroutines" "$F" && grep -q "10\. THIS PACKAGE IS PROVIDED" "$F" && D="Artistic-1.0-cl8" |
|
57 |
grep -q "7\. C or perl subroutines" "$F" && grep -q "9\. THIS PACKAGE IS PROVIDED" "$F" && D="Artistic-1.0" |
|
58 |
else |
|
59 |
D="Artistic-2.0" |
|
60 |
fi |
0dc9b0
|
61 |
[[ -n "$L" && -n "$D" ]] && L="$L OR " ; L="$L$D" |
1734a6
|
62 |
fi |
MT |
63 |
|
|
64 |
if grep -A 1 "GNU GENERAL PUBLIC LICENSE" "$F" | grep -q "Version 1, February 1989" ; then |
|
65 |
D="GPL-1.0-only" |
|
66 |
grep -A 2 "GNU General Public License as published by the" "$F" | grep -q "or (at your option) any" && D="GPL-1.0-or-later" |
|
67 |
[[ -n "$L" ]] && L="$L OR " ; L="$L$D" |
|
68 |
fi |
|
69 |
|
d0bc0b
|
70 |
TMPFILE=$(mktemp -q) |
MT |
71 |
[[ -z "$TMPFILE" ]] && printf "ERROR: Temporary file creation failed\n" >&2 && exit 1 |
|
72 |
|
98faf2
|
73 |
typeset -A matched |
08ba8d
|
74 |
for l in "$WS_TOOLS"/licenses/$LICENSE ; do |
de9d5d
|
75 |
[[ -f "$l" ]] || continue |
e1d036
|
76 |
# skip filters |
MT |
77 |
[[ "$l" != "${l%.filter}" ]] && continue |
98faf2
|
78 |
|
MT |
79 |
# extract license identifier |
|
80 |
license_id="${l##*/}" |
|
81 |
license_id="${license_id#header.}" |
|
82 |
license_id="${license_id#license.}" |
|
83 |
# sanity check, this should never happen |
|
84 |
[[ -z "$license_id" ]] && continue |
|
85 |
|
|
86 |
# make sure we do not match one license twice |
|
87 |
[[ -n "$matched[$license_id]" ]] && continue |
de9d5d
|
88 |
|
d0bc0b
|
89 |
cat <<#EOF > "$TMPFILE" |
MT |
90 |
dos2unix -ascii \\ |
7b84d0
|
91 |
| tr -d '\\014' \\ |
76f359
|
92 |
| LC_ALL=C sed -E -e 's/^[[:space:]]+\$//g' \\ |
d0bc0b
|
93 |
| awk '/^#/{next}/^\$/{\$0="\n"}1' ORS=' ' \\ |
76f359
|
94 |
| LC_ALL=C sed -E -e 's/[[:space:]]+/ /g' -e 's/^ //' -e 's/ \$//' -e '/^\$/d' \\ |
e1d036
|
95 |
EOF |
9085b1
|
96 |
# Remove some reStructuredText markup |
MT |
97 |
if [[ "${F%.rst}" != "$F" ]] ; then |
|
98 |
cat <<#EOF >> "$TMPFILE" |
|
99 |
| sed -e '/^\*\*\$/d' -e 's/^\*\*\([^*]\)/\1/' -e 's/\([^*]\)\*\*\$/\1/' -e 's/\([^*]\)\*\*\([^*]\)/\1\2/g' \\ |
|
100 |
EOF |
|
101 |
fi |
e1d036
|
102 |
# Apply filter if any |
579445
|
103 |
[[ -x "$l.filter" ]] && printf '\t| LC_ALL=C %s \\\n' "$l.filter" >> "$TMPFILE" |
e1d036
|
104 |
cat <<#EOF >> "$TMPFILE" |
579445
|
105 |
| LC_ALL=C tr '[:upper:]' '[:lower:]' \\ |
ad959b
|
106 |
| sed -e 's|http://|https://|g' \ |
e1d036
|
107 |
| tr ' ' '\\n' | fmt |
d0bc0b
|
108 |
EOF |
de9d5d
|
109 |
|
MT |
110 |
REDIRECT="/dev/null" |
|
111 |
|
|
112 |
if ((DEBUG)) ; then |
|
113 |
REDIRECT="/dev/stdout" |
e1d036
|
114 |
printf "[DBG] TEMPLATE %s\n" "${l##*/}" |
d0bc0b
|
115 |
. "$TMPFILE" < "$l" |
de9d5d
|
116 |
printf "[DBG] FILE\n" |
d0bc0b
|
117 |
. "$TMPFILE" < "$F" |
de9d5d
|
118 |
printf "[DBG] DIFFS\n" |
MT |
119 |
fi |
|
120 |
|
d0bc0b
|
121 |
diff -i <(. "$TMPFILE" < "$l") <(. "$TMPFILE" < "$F") > "$REDIRECT" || continue |
de9d5d
|
122 |
|
98faf2
|
123 |
matched[$license_id]="$l" |
MT |
124 |
|
de9d5d
|
125 |
[[ -n "$L" ]] && L="$L OR " |
98faf2
|
126 |
L="$L$license_id" |
de9d5d
|
127 |
done |
1734a6
|
128 |
|
d0bc0b
|
129 |
rm -f "$TMPFILE" |
MT |
130 |
|
1734a6
|
131 |
[[ -n "$L" ]] && printf "%s\n" "$L" |