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
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.
15 # Copyright 2022 Marcel Telka
21 [[ -n "$1" ]] && printf "ERROR: %s\n\n" "$1" >&2
22 printf "Usage: license-detector [-d] [-l LICENSE] LICENSE_FILE\n" >&2
23 [[ -n "$1" ]] && exit 1
30 while getopts ":hdl:" OPT
; do
34 "l") LICENSE
="*.$OPTARG" ;;
40 (($# > 1)) && usage
"Too many arguments"
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"
47 WS_TOOLS
=$
(dirname $0)
52 if grep -q -i "Artistic License" "$F" ; then
53 if ! grep -q -i "Artistic License.*2" "$F" ; then
55 grep -q "7\. C subroutines" "$F" && grep -q "10\. THIS PACKAGE IS PROVIDED" "$F" && D
="Artistic-1.0-Perl"
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"
61 [[ -n "$L" && -n "$D" ]] && L
="$L OR " ; L
="$L$D"
64 if grep -A 1 "GNU GENERAL PUBLIC LICENSE" "$F" |
grep -q "Version 1, February 1989" ; then
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"
71 [[ -z "$TMPFILE" ]] && printf "ERROR: Temporary file creation failed\n" >&2 && exit 1
74 for l
in "$WS_TOOLS"/licenses
/$LICENSE ; do
75 [[ -f "$l" ]] ||
continue
77 [[ "$l" != "${l%.filter}" ]] && continue
79 # extract license identifier
81 license_id
="${license_id#header.}"
82 license_id
="${license_id#license.}"
83 # sanity check, this should never happen
84 [[ -z "$license_id" ]] && continue
86 # make sure we do not match one license twice
87 [[ -n "$matched[$license_id]" ]] && continue
89 cat <<#EOF > "$TMPFILE"
92 | LC_ALL
=C
sed -E -e 's/^[[:space:]]+\$//g' \\
93 |
awk '/^#/{next}/^\$/{\$0="\n"}1' ORS
=' ' \\
94 | LC_ALL
=C
sed -E -e 's/[[:space:]]+/ /g' -e 's/^ //' -e 's/ \$//' -e '/^\$/d' \\
96 # Remove some reStructuredText markup
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' \\
102 # Punctuation - Quotes
103 printf '\t| sed -e %cs/“/"/g%c -e %cs/”/"/g%c \\\n' "'" "'" "'" "'" >> "$TMPFILE"
104 # Apply filter if any
105 [[ -x "$l.filter" ]] && printf '\t| LC_ALL=C %s \\\n' "$l.filter" >> "$TMPFILE"
106 cat <<#EOF >> "$TMPFILE"
107 | LC_ALL
=C
tr '[:upper:]' '[:lower:]' \\
108 |
sed -e 's|http://|https://|g' \
115 REDIRECT
="/dev/stdout"
116 printf "[DBG] TEMPLATE %s\n" "${l##*/}"
118 printf "[DBG] FILE\n"
120 printf "[DBG] DIFFS\n"
123 diff -i <(.
"$TMPFILE" < "$l") <(.
"$TMPFILE" < "$F") > "$REDIRECT" ||
continue
125 matched
[$license_id]="$l"
127 [[ -n "$L" ]] && L
="$L OR "
133 [[ -n "$L" ]] && printf "%s\n" "$L"