Marcel Telka
2023-06-09 c380de419920ae2c45cd8c8852a56e7f8b3c5737
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
de9d5d 22     printf "Usage: license-detector [-d] LICENSE_FILE\n" >&2
1734a6 23     [[ -n "$1" ]] && exit 1
MT 24     exit 0
25 }
26
27
de9d5d 28 DEBUG=0
MT 29 while getopts ":hd" OPT ; do
1734a6 30     case "$OPT" in
MT 31     "?"|"h")    usage ;;
de9d5d 32     "d")        DEBUG=1 ;;
1734a6 33     esac
MT 34 done
35 shift $((OPTIND - 1))
36
37 (($# == 0)) && usage
38 (($# > 1)) && usage "Too many arguments"
39
40 LICENSE_FILE="$1"
41 [[ -e "$LICENSE_FILE" ]] || usage "$LICENSE_FILE not found"
42 [[ -d "$LICENSE_FILE" ]] && usage "$LICENSE_FILE is directory"
43 [[ -r "$LICENSE_FILE" ]] || usage "Unable to read $LICENSE_FILE"
de9d5d 44
MT 45 WS_TOOLS=$(dirname $0)
1734a6 46
MT 47
48 F="$LICENSE_FILE"
49
50 if grep -q -i "Artistic License" "$F" ; then
51     if ! grep -q -i "Artistic License.*2" "$F" ; then
52         D="Artistic-1.0-TODO"
53         grep -q "7\. C subroutines" "$F" && grep -q "10\. THIS PACKAGE IS PROVIDED" "$F" && D="Artistic-1.0-Perl"
54         grep -q "7\. C or perl subroutines" "$F" && grep -q "10\. THIS PACKAGE IS PROVIDED" "$F" && D="Artistic-1.0-cl8"
55         grep -q "7\. C or perl subroutines" "$F" && grep -q "9\. THIS PACKAGE IS PROVIDED" "$F" && D="Artistic-1.0"
56     else
57         D="Artistic-2.0"
58     fi
59     [[ -n "$L" ]] && L="$L OR " ; L="$L$D"
60 fi
61
62 if grep -A 1 "GNU GENERAL PUBLIC LICENSE" "$F" | grep -q "Version 1, February 1989" ; then
63     D="GPL-1.0-only"
64     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"
65     [[ -n "$L" ]] && L="$L OR " ; L="$L$D"
66 fi
67
68 if grep -i -A 1 "GENERAL PUBLIC LICENSE" "$F" | grep -q "Version 2, June 1991" ; then
69     D="GPL-2.0-only"
70     grep -A 2 "GNU General Public License as published by the" "$F" | grep -q "or (at your option) any" && D="GPL-2.0-or-later"
71     [[ -n "$L" ]] && L="$L OR " ; L="$L$D"
72 fi
73
74 if grep -A 1 "GNU LIBRARY GENERAL PUBLIC LICENSE" "$F" | grep -q "Version 2, June 1991" ; then
75     D="LGPL-2.0-only"
76     [[ -n "$L" ]] && L="$L OR " ; L="$L$D"
77 fi
78
d0bc0b 79 TMPFILE=$(mktemp -q)
MT 80 [[ -z "$TMPFILE" ]] && printf "ERROR: Temporary file creation failed\n" >&2 && exit 1
81
98faf2 82 typeset -A matched
de9d5d 83 for l in "$WS_TOOLS"/licenses/* ; do
MT 84     [[ -f "$l" ]] || continue
e1d036 85     # skip filters
MT 86     [[ "$l" != "${l%.filter}" ]] && continue
98faf2 87
MT 88     # extract license identifier
89     license_id="${l##*/}"
90     license_id="${license_id#header.}"
91     license_id="${license_id#license.}"
92     # sanity check, this should never happen
93     [[ -z "$license_id" ]] && continue
94
95     # make sure we do not match one license twice
96     [[ -n "$matched[$license_id]" ]] && continue
de9d5d 97
d0bc0b 98     cat <<#EOF > "$TMPFILE"
MT 99         dos2unix -ascii \\
100             | awk '/^#/{next}/^\$/{\$0="\n"}1' ORS=' ' \\
101             | sed -E -e 's/[[:space:]]+/ /g' -e 's/^ //' -e 's/ \$//' -e '/^\$/d' \\
e1d036 102     EOF
MT 103     # Apply filter if any
c380de 104     [[ -x "$l.filter" ]] && printf '\t| %s \\\n' "$l.filter" >> "$TMPFILE"
e1d036 105     cat <<#EOF >> "$TMPFILE"
MT 106             | tr ' ' '\\n' | fmt
d0bc0b 107     EOF
de9d5d 108
MT 109     REDIRECT="/dev/null"
110
111     if ((DEBUG)) ; then
112         REDIRECT="/dev/stdout"
e1d036 113         printf "[DBG] TEMPLATE %s\n" "${l##*/}"
d0bc0b 114         . "$TMPFILE" < "$l"
de9d5d 115         printf "[DBG] FILE\n"
d0bc0b 116         . "$TMPFILE" < "$F"
de9d5d 117         printf "[DBG] DIFFS\n"
MT 118     fi
119
d0bc0b 120     diff -i <(. "$TMPFILE" < "$l") <(. "$TMPFILE" < "$F") > "$REDIRECT" || continue
de9d5d 121
98faf2 122     matched[$license_id]="$l"
MT 123
de9d5d 124     [[ -n "$L" ]] && L="$L OR "
98faf2 125     L="$L$license_id"
de9d5d 126 done
1734a6 127
d0bc0b 128 rm -f "$TMPFILE"
MT 129
1734a6 130 [[ -n "$L" ]] && printf "%s\n" "$L"