thunderbird: update to 128.3.0
[oi-userland.git] / tools / license-detector
blob1f6a1a27822c4cb40fc63953e13105e0b2ed5fb8
1 #! /usr/bin/ksh
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.
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
19 function usage
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
24 exit 0
28 DEBUG=0
29 LICENSE="*"
30 while getopts ":hdl:" OPT ; do
31 case "$OPT" in
32 "?"|"h") usage ;;
33 "d") DEBUG=1 ;;
34 "l") LICENSE="*.$OPTARG" ;;
35 esac
36 done
37 shift $((OPTIND - 1))
39 (($# == 0)) && usage
40 (($# > 1)) && usage "Too many arguments"
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"
47 WS_TOOLS=$(dirname $0)
50 F="$LICENSE_FILE"
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"
58 else
59 D="Artistic-2.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
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"
70 TMPFILE=$(mktemp -q)
71 [[ -z "$TMPFILE" ]] && printf "ERROR: Temporary file creation failed\n" >&2 && exit 1
73 typeset -A matched
74 for l in "$WS_TOOLS"/licenses/$LICENSE ; do
75 [[ -f "$l" ]] || continue
76 # skip filters
77 [[ "$l" != "${l%.filter}" ]] && continue
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
86 # make sure we do not match one license twice
87 [[ -n "$matched[$license_id]" ]] && continue
89 cat <<#EOF > "$TMPFILE"
90 dos2unix -ascii \\
91 | tr -d '\\014' \\
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' \\
95 EOF
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' \
109 | tr ' ' '\\n' | fmt
112 REDIRECT="/dev/null"
114 if ((DEBUG)) ; then
115 REDIRECT="/dev/stdout"
116 printf "[DBG] TEMPLATE %s\n" "${l##*/}"
117 . "$TMPFILE" < "$l"
118 printf "[DBG] FILE\n"
119 . "$TMPFILE" < "$F"
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 "
128 L="$L$license_id"
129 done
131 rm -f "$TMPFILE"
133 [[ -n "$L" ]] && printf "%s\n" "$L"