summaryrefslogtreecommitdiff
path: root/src/pkgbuild-check-licenses
blob: 85ca2c30014d170d444df10c043eb79e5b28b895 (plain)
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
126
#!/usr/bin/env bash
# pkgbuild-check-licenses

# Copyright 2010 Haase Hernández
# Copyright 2010 Joseph Graham
# Copyright 2010 Joshua Ismael
# Copyright 2010 Nicolás Reynolds
# Copyright 2012-2013 Luke Shumaker
#
# This file is part of Parabola.
#
# Parabola is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Parabola is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Parabola.  If not, see <http://www.gnu.org/licenses/>.

. libremessages
. $(librelib conf)

# Usage: check_deps $pkgbuild
# Check whether a PKGBUILD package depends on non-free packages
check_licenses() (
	# Note that we use () instead of {} for this function; so that variables
	# from the PKBUILD don't bubble up
	local pkgbuild=$1
	load_PKGBUILD "$pkgbuild"
	if [[ -z $pkgname ]]; then
		return $_E_ERROR # not a PKGBUILD
	fi
	if [[ -z "${license[*]}" ]]; then
		error "license array of %s %s is not set" "${pkgbase:-${pkgname[0]}}" "$(get_full_version)"
		return $_E_ERROR
	fi

	msg2 "Looking at license array of %s %s" "${pkgbase:-${pkgname[0]}}" "$(get_full_version)"

	local ret=$_E_OK
	for _license in "${license[@]}"; do
		if [[ ! -e "/usr/share/licenses/common/$_license" ]]; then
			local s=$_E_OK
			case "${_license#custom:}" in
				WTFPL)
					# accept as common, I think it should be in the licenses package
					:;;
				BSD1|BSD2|BSD3|MIT|X11)
					# accept these as common; they can't be included in the
					# 'licenses' package because some text must be customized
					:;;
				BSD4)
					warning "The 4-clause BSD license is free but has practical problems.";;
				BSD)
					warning "License 'BSD' is ambiguous, use 'BSD{1..4}' to specify the number of clauses."
					s=$_E_UNCOMMON
					;;
				JSON)
					error "License '%s' is a known non-free license." "$_license"
					s=$_E_NONFREE
					;;
				*)
					warning "License '%s' is not a common license." "$_license"
					s=$_E_UNCOMMON
					;;
			esac
			ret=$(($ret|$s))
		fi
	done
	return $ret
)

usage() {
	print "Usage: %s [OPTIONS] [PKGBUILD1 PKGBUILD2 ...]" "${0##*/}"
	echo
	prose 'If no PKGBUILD is specified, `./PKGBUILD` is implied.'
	echo
	print "Exit status (add them for combinations):"
	print "   0: Everything OK, no freedom issues"
	print "   1: Ran with error"
	print "   2: Uses uncommon licenses, check them"
	print "   4: Uses known unacceptable licenses"
	echo
	print "Options:"
	flag '-f'            'Allow running as root user'
	flag '-h'            'Show this message'
}
_E_OK=0
_E_ERROR=1
_E_UNCOMMON=2
_E_NONFREE=4

main() {
	local asroot=false
	while getopts 'fh' arg; do
		case "$arg" in
			f) asroot=true;;
			h) usage; return $_E_OK;;
			*) usage; return $_E_ERROR;;
		esac
	done
	shift $(($OPTIND - 1))
	if [[ $# -lt 1 ]]; then
		pkgbuilds=("`pwd`/PKGBUILD")
	else
		pkgbuilds=("$@")
	fi

	if [[ -w / ]] && ! $asroot; then
		error "Run as normal user, or use the -f option to run as root."
		return 1
	fi

	local ret=0
	for pkgbuild in "${pkgbuilds[@]}"; do
		check_licenses "$pkgbuild" || ret=$(($ret|$?))
	done
	return $ret
}

main "$@"