Marcel Telka
2022-11-24 e1d0369c2511dbd104cb24adaaa40b0ed9294853
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#! /usr/bin/ksh
#
#
# This file and its contents are supplied under the terms of the
# Common Development and Distribution License ("CDDL"), version 1.0.
# You may only use this file in accordance with the terms of version
# 1.0 of the CDDL.
#
# A full copy of the text of the CDDL should have accompanied this
# source.  A copy of the CDDL is also available via the Internet at
# http://www.illumos.org/license/CDDL.
#
 
#
# Copyright 2022 Marcel Telka
#
 
 
function usage
{
    [[ -n "$1" ]] && printf "ERROR: %s\n\n" "$1" >&2
    printf "Usage: license-detector [-d] LICENSE_FILE\n" >&2
    [[ -n "$1" ]] && exit 1
    exit 0
}
 
 
DEBUG=0
while getopts ":hd" OPT ; do
    case "$OPT" in
    "?"|"h")    usage ;;
    "d")        DEBUG=1 ;;
    esac
done
shift $((OPTIND - 1))
 
(($# == 0)) && usage
(($# > 1)) && usage "Too many arguments"
 
LICENSE_FILE="$1"
[[ -e "$LICENSE_FILE" ]] || usage "$LICENSE_FILE not found"
[[ -d "$LICENSE_FILE" ]] && usage "$LICENSE_FILE is directory"
[[ -r "$LICENSE_FILE" ]] || usage "Unable to read $LICENSE_FILE"
 
WS_TOOLS=$(dirname $0)
 
 
F="$LICENSE_FILE"
 
if grep -A 1 "Apache License" "$F" | grep -q "Version 2\.0, January 2004" ; then
    D="Apache-2.0"
    [[ -n "$L" ]] && L="$L OR " ; L="$L$D"
fi
 
if grep -q -i "Artistic License" "$F" ; then
    if ! grep -q -i "Artistic License.*2" "$F" ; then
        D="Artistic-1.0-TODO"
        grep -q "7\. C subroutines" "$F" && grep -q "10\. THIS PACKAGE IS PROVIDED" "$F" && D="Artistic-1.0-Perl"
        grep -q "7\. C or perl subroutines" "$F" && grep -q "10\. THIS PACKAGE IS PROVIDED" "$F" && D="Artistic-1.0-cl8"
        grep -q "7\. C or perl subroutines" "$F" && grep -q "9\. THIS PACKAGE IS PROVIDED" "$F" && D="Artistic-1.0"
    else
        D="Artistic-2.0"
    fi
    [[ -n "$L" ]] && L="$L OR " ; L="$L$D"
fi
 
if grep -A 1 "GNU GENERAL PUBLIC LICENSE" "$F" | grep -q "Version 1, February 1989" ; then
    D="GPL-1.0-only"
    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"
    [[ -n "$L" ]] && L="$L OR " ; L="$L$D"
fi
 
if grep -i -A 1 "GENERAL PUBLIC LICENSE" "$F" | grep -q "Version 2, June 1991" ; then
    D="GPL-2.0-only"
    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"
    [[ -n "$L" ]] && L="$L OR " ; L="$L$D"
fi
 
if grep -A 1 "GNU LIBRARY GENERAL PUBLIC LICENSE" "$F" | grep -q "Version 2, June 1991" ; then
    D="LGPL-2.0-only"
    [[ -n "$L" ]] && L="$L OR " ; L="$L$D"
fi
 
TMPFILE=$(mktemp -q)
[[ -z "$TMPFILE" ]] && printf "ERROR: Temporary file creation failed\n" >&2 && exit 1
 
for l in "$WS_TOOLS"/licenses/* ; do
    [[ -f "$l" ]] || continue
    # skip filters
    [[ "$l" != "${l%.filter}" ]] && continue
 
    cat <<#EOF > "$TMPFILE"
        dos2unix -ascii \\
            | awk '/^#/{next}/^\$/{\$0="\n"}1' ORS=' ' \\
            | sed -E -e 's/[[:space:]]+/ /g' -e 's/^ //' -e 's/ \$//' -e '/^\$/d' \\
    EOF
    # Apply filter if any
    [[ -x "$l.filter" ]] && printf "\t| %s \\\n" "$l.filter" >> "$TMPFILE"
    cat <<#EOF >> "$TMPFILE"
            | tr ' ' '\\n' | fmt
    EOF
 
    REDIRECT="/dev/null"
 
    if ((DEBUG)) ; then
        REDIRECT="/dev/stdout"
        printf "[DBG] TEMPLATE %s\n" "${l##*/}"
        . "$TMPFILE" < "$l"
        printf "[DBG] FILE\n"
        . "$TMPFILE" < "$F"
        printf "[DBG] DIFFS\n"
    fi
 
    diff -i <(. "$TMPFILE" < "$l") <(. "$TMPFILE" < "$F") > "$REDIRECT" || continue
 
    [[ -n "$L" ]] && L="$L OR "
    l="${l##*/}"
    l="${l#header.}"
    l="${l#license.}"
    L="$L$l"
done
 
rm -f "$TMPFILE"
 
[[ -n "$L" ]] && printf "%s\n" "$L"