-
Notifications
You must be signed in to change notification settings - Fork 4
/
detect-license.sh
executable file
·102 lines (92 loc) · 2.81 KB
/
detect-license.sh
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
#!/bin/sh
# This tool attempts to detect the license from the full license text
# and returns an SPDX identifier. It only supports a very limited set of common open source licenses
for file in README.md README.MD README.rst README README.txt README.TXT; do
[ -e "$file" ] || continue;
echo "Trying $file ...">&2
SPDX=$(grep "SPDX-License-Identifier:" "$file" | sed -e 's/^.*SPDX-License-Identifier://')
if [ -n "$SPDX" ]; then
echo "$SPDX"
exit 0
fi
done
for file in LICENSE LICENSE.md LICENSE.txt COPYING COPYRIGHT LICENCE LICENCE.md LICENCE.txt; do
[ -e "$file" ] || continue;
echo "Trying $file ...">&2
TEXT=$(head -n 10 "$file" | tr "[:lower:]" "[:upper:]")
case "$TEXT" in
*"GNU LESSER GENERAL PUBLIC LICENSE"*|*"GNU LESSER GENERAL PUBLIC LICENCE"*)
LICENSE="LGPL"
;;
*"GNU AFFERO GENERAL PUBLIC LICENSE"*|*"GNU AFFERO GENERAL PUBLIC LICENCE"*)
LICENSE="AGPL"
;;
*"GNU GENERAL PUBLIC LICENSE"*|*"GNU GENERAL PUBLIC LICENCE"*)
LICENSE="GPL"
;;
*"APACHE LICENSE"*|*"APACHE LICENCE"*)
LICENSE="Apache"
;;
*"MIT LICENSE"*|*"MIT LICENCE"*)
LICENSE="MIT"
;;
*"ECLIPSE PUBLIC LICENSE"*|*"ECLIPSE PUBLIC LICENCE"*)
LICENSE="EPL"
;;
*"MOZILLA PUBLIC LICENSE"*|*"MOZILLA PUBLIC LICENCE"*)
LICENSE="MPL"
;;
*"EUROPEAN UNION PUBLIC LICENSE"*|*"EUROPEAN UNION PUBLIC LICENCE"*)
LICENSE="EUPL"
;;
*)
LICENSE=""
;;
esac
case "$TEXT" in
*"V3.0"*|*"VERSION 3.0"*|*"VERSION 3"*)
VERSION="3.0"
;;
*"V2.1"*|*"VERSION 2.1"*)
VERSION="2.1"
;;
*"V2.0"*|*"VERSION 2.0"*|*"VERSION 2"*)
VERSION="2.0"
;;
*"V1.1"*|*"VERSION 1.1"*)
VERSION="1.1"
;;
*"V1.0"*|*"VERSION 1.0"*|*"VERSION 1"*)
VERSION="1.0"
;;
*)
VERSION=""
;;
esac
#shellch
case "$TEXT" in
*"OR ANY LATER VERSION"*)
QUALIFIER="or-later"
;;
*)
QUALIFIER="only"
;;
esac
if [ -n "$LICENSE" ]; then
if [ "$LICENSE" = "MIT" ]; then
#no version
echo "$LICENSE"
exit 0
elif [ "$LICENSE" != "MIT" ] && [ -n "$VERSION" ]; then
#version mandatory
if [ "$LICENSE" = "GPL" ] || [ "$LICENSE" = "AGPL" ] || [ "$LICENSE" = "LGPL" ]; then
echo "$LICENSE-$VERSION-$QUALIFIER"
else
echo "$LICENSE-$VERSION"
fi
exit 0
fi
fi
done
echo "No license detected" >&2
exit 1