Marcel Telka
2023-10-17 7b84d0d65c2a9b461d9af0b7ca531efebcc645d7
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
0dc9b0 52         D=
1734a6 53         grep -q "7\. C subroutines" "$F" && grep -q "10\. THIS PACKAGE IS PROVIDED" "$F" && D="Artistic-1.0-Perl"
MT 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
0dc9b0 59     [[ -n "$L" && -n "$D" ]] && L="$L OR " ; L="$L$D"
1734a6 60 fi
MT 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 \\
7b84d0 100             | tr -d '\\014' \\
d0bc0b 101             | awk '/^#/{next}/^\$/{\$0="\n"}1' ORS=' ' \\
MT 102             | sed -E -e 's/[[:space:]]+/ /g' -e 's/^ //' -e 's/ \$//' -e '/^\$/d' \\
e1d036 103     EOF
MT 104     # Apply filter if any
c380de 105     [[ -x "$l.filter" ]] && printf '\t| %s \\\n' "$l.filter" >> "$TMPFILE"
e1d036 106     cat <<#EOF >> "$TMPFILE"
MT 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"