fritzkink
2023-11-28 0e6b8465e1fa76dd600d4a240e95735e71db5d7c
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
d0bc0b 68 TMPFILE=$(mktemp -q)
MT 69 [[ -z "$TMPFILE" ]] && printf "ERROR: Temporary file creation failed\n" >&2 && exit 1
70
98faf2 71 typeset -A matched
de9d5d 72 for l in "$WS_TOOLS"/licenses/* ; do
MT 73     [[ -f "$l" ]] || continue
e1d036 74     # skip filters
MT 75     [[ "$l" != "${l%.filter}" ]] && continue
98faf2 76
MT 77     # extract license identifier
78     license_id="${l##*/}"
79     license_id="${license_id#header.}"
80     license_id="${license_id#license.}"
81     # sanity check, this should never happen
82     [[ -z "$license_id" ]] && continue
83
84     # make sure we do not match one license twice
85     [[ -n "$matched[$license_id]" ]] && continue
de9d5d 86
d0bc0b 87     cat <<#EOF > "$TMPFILE"
MT 88         dos2unix -ascii \\
7b84d0 89             | tr -d '\\014' \\
54c9c7 90             | sed -E -e 's/^[[:space:]]+\$//g' \\
d0bc0b 91             | awk '/^#/{next}/^\$/{\$0="\n"}1' ORS=' ' \\
MT 92             | sed -E -e 's/[[:space:]]+/ /g' -e 's/^ //' -e 's/ \$//' -e '/^\$/d' \\
e1d036 93     EOF
9085b1 94     # Remove some reStructuredText markup
MT 95     if [[ "${F%.rst}" != "$F" ]] ; then
96         cat <<#EOF >> "$TMPFILE"
97                 | sed -e '/^\*\*\$/d' -e 's/^\*\*\([^*]\)/\1/' -e 's/\([^*]\)\*\*\$/\1/' -e 's/\([^*]\)\*\*\([^*]\)/\1\2/g' \\
98         EOF
99     fi
e1d036 100     # Apply filter if any
579445 101     [[ -x "$l.filter" ]] && printf '\t| LC_ALL=C %s \\\n' "$l.filter" >> "$TMPFILE"
e1d036 102     cat <<#EOF >> "$TMPFILE"
579445 103             | LC_ALL=C tr '[:upper:]' '[:lower:]' \\
ad959b 104             | sed -e 's|http://|https://|g' \
e1d036 105             | tr ' ' '\\n' | fmt
d0bc0b 106     EOF
de9d5d 107
MT 108     REDIRECT="/dev/null"
109
110     if ((DEBUG)) ; then
111         REDIRECT="/dev/stdout"
e1d036 112         printf "[DBG] TEMPLATE %s\n" "${l##*/}"
d0bc0b 113         . "$TMPFILE" < "$l"
de9d5d 114         printf "[DBG] FILE\n"
d0bc0b 115         . "$TMPFILE" < "$F"
de9d5d 116         printf "[DBG] DIFFS\n"
MT 117     fi
118
d0bc0b 119     diff -i <(. "$TMPFILE" < "$l") <(. "$TMPFILE" < "$F") > "$REDIRECT" || continue
de9d5d 120
98faf2 121     matched[$license_id]="$l"
MT 122
de9d5d 123     [[ -n "$L" ]] && L="$L OR "
98faf2 124     L="$L$license_id"
de9d5d 125 done
1734a6 126
d0bc0b 127 rm -f "$TMPFILE"
MT 128
1734a6 129 [[ -n "$L" ]] && printf "%s\n" "$L"