summaryrefslogtreecommitdiffstats
path: root/lib/package.sh
blob: 6aa7b94b8eb9db6fb9d1cff3c48d74e854d3af6f (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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
# Interface for retrieving source and binary package metadata
#
# Copyright (C) 2012, 2019 Patrick McDermott
#
# This file is part of opkbuild.
#
# opkbuild 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.
#
# opkbuild 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 opkbuild.  If not, see <http://www.gnu.org/licenses/>.

_OB_PACKAGE_DIR=
_OB_PACKAGE_FORMAT=
_OB_BINARY_PACKAGES=
_OB_SOURCE_PARAMETERS=
_OB_BINARY_PARAMETERS=

ob_init_package()
{
	local dir="${1}"
	shift 1 || _ob_abort

	_OB_PACKAGE_DIR="$(cd -- "${dir}" && pwd)"

	if [ -r "${_OB_PACKAGE_DIR}/format" ]; then
		case "$(cat -- "${_OB_PACKAGE_DIR}/format")" in
			2.0)
				_OB_PACKAGE_FORMAT='2'
				;;
		esac
	fi

	if [ -z "${_OB_PACKAGE_FORMAT}" ]; then
		_ob_error_msg 'unable_to_detect_package_format'
		return 1
	fi

	_OB_BINARY_PACKAGES=

	return 0
}

_ob_package_do()
{
	local func="${1}"
	shift 1 || _ob_abort

	"_ob_${func}_${_OB_PACKAGE_FORMAT}" "${@}" || return ${?}

	return 0
}

ob_parse_package_metadata()
{
	_ob_package_do 'parse_package_metadata' || return ${?}

	return 0
}

ob_get_binary_packages()
{
	local opt=
	local host_arch=
	local host_plat=
	local pkg=
	local pkgs_clean=
	local pkg_clean=
	local pkgs=

	OPTIND=1
	while getopts 'a:p:' opt; do
		case "${opt}" in
			a)
				host_arch="${OPTARG}"
				;;
			p)
				host_plat="${OPTARG}"
				;;
			?)
				_ob_abort
				;;
		esac
	done
	shift $(($OPTIND - 1))

	# NB: If a source package lists no binary packages, this will be true each
	# call.
	if [ -z "${_OB_BINARY_PACKAGES}" ]; then

		_ob_package_do 'get_binary_packages'

		pkgs_clean=' '

		for pkg in ${_OB_BINARY_PACKAGES}; do

			# Validate the name.
			if ! ob_validate_binary_name "${pkg}"; then
				_ob_warn_msg 'bad_binary_name' "${pkg}"
				continue
			fi

			# Make sure the "clean" name is unique.
			pkg_clean="$({ tr 'a-z' 'A-Z' | tr -dC 'A-Z0-9';} <<-EOF
				"${pkg}"
				EOF
				)"
			case "${pkgs_clean}" in *" ${pkg_clean} "*)
				_ob_warn_msg 'duplicate_clean_binary_name' \
					"${pkg_clean}"
				continue
			esac
			pkgs_clean="${pkgs_clean}${pkg_clean} "

			_OB_BINARY_PACKAGES="${_OB_BINARY_PACKAGES} ${pkgs}"

		done

	fi

	pkgs=''

	for pkg in ${_OB_BINARY_PACKAGES}; do
		if [ -n "${host_arch}" ] && ! ob_arch_is_concerned \
				"${host_arch}" "$(ob_get_binary_parameter \
				"${pkg}" 'Architecture')"; then
			continue
		fi
		if [ -n "${host_plat}" ] && ! ob_plat_is_concerned \
				"${host_plat}" "$(ob_get_binary_parameter \
				"${pkg}" 'Platform')"; then
			continue
		fi
		pkgs="${pkgs} ${pkg}"
	done

	printf '%s ' ${pkgs}

	return 0
}

ob_get_source_parameter()
{
	local name="${1}"
	shift 1 || _ob_abort

	# Convert field name to uppercase and validate.
	case "${name}" in *[!A-Za-z0-9-]* | '')
		return 1
	esac
	name="$(tr 'a-z-' 'A-Z_' <<-EOF
		${name}
		EOF
		)"

	eval "printf '%s' \"\${_OB_SRCFIELD_${name}}\""

	return 0
}

ob_get_binary_parameter()
{
	local package="${1}"
	local name="${2}"
	shift 2 || _ob_abort

	if ! ob_validate_binary_name "${package}"; then
		return 1
	fi

	# Convert package name to its uppercase "clean" form.
	package="$({ tr 'a-z' 'A-Z' | tr -dC 'A-Z0-9'; } <<-EOF
		"${pkg}"
		EOF
		)"

	# Convert field name to uppercase and validate.
	case "${name}" in *[!A-Za-z0-9-]* | '')
		return 1
	esac
	name="$(tr 'a-z-' 'A-Z_' <<-EOF
		${name}
		EOF
		)"

	eval "printf '%s' \"\${_OB_BINFIELD_${package}_${name}}\""

	return 0
}

ob_get_doc_package()
{
	_ob_package_do 'get_doc_package' || return ${?}

	return 0
}

ob_get_doc_files()
{
	local arch="${1}"
	local plat="${2}"
	shift 2 || _ob_abort

	_ob_package_do 'get_doc_files' "${arch}" "${plat}" || return ${?}

	return 0
}

ob_summarize_package_arch()
{
	# XXX: Stub.

	return 0
}

ob_summarize_package_plat()
{
	# XXX: Stub.

	return 0
}

ob_set_package_substvars()
{
	local pkg="${1}"
	shift 1 || _ob_abort

	_ob_package_do 'set_package_substvars' "${pkg}" || return ${?}

	return 0
}

_ob_set_binary_packages()
{
	local packages="${1}"
	shift 1 || _ob_abort

	_OB_BINARY_PACKAGES="${packages}"

	return 0
}

_ob_set_source_parameter()
{
	local name="${1}"
	local value="${2}"
	shift 2 || _ob_abort

	# Convert field name to uppercase and validate.
	case "${name}" in *[!A-Za-z0-9-]* | '')
		return 1
	esac
	name="$(tr 'a-z-' 'A-Z_' <<-EOF
		${name}
		EOF
		)"

	_OB_SOURCE_PARAMETERS="${_OB_SOURCE_PARAMETERS} ${name}"

	eval "_OB_SRCFIELD_${name}=\"\${value}\""

	return 0
}

_ob_set_binary_parameter()
{
	local package="${1}"
	local name="${2}"
	local value="${3}"
	shift 3 || _ob_abort

	if ! ob_validate_binary_name "${package}"; then
		return 1
	fi

	# Convert package name to its uppercase "clean" form.
	package="$({ tr 'a-z' 'A-Z' | tr -dC 'A-Z0-9'; } <<-EOF
		"${pkg}"
		EOF
		)"

	# Convert field name to uppercase and validate.
	case "${name}" in *[!A-Za-z0-9-]* | '')
		return 1
	esac
	name="$(tr 'a-z-' 'A-Z_' <<-EOF
		${name}
		EOF
		)"

	case " ${_OB_BINARY_PARAMETERS} " in
		*" ${name} "*)
			;;
		*)
			_OB_BINARY_PARAMETERS="${_OB_BINARY_PARAMETERS} ${name}"
			;;
	esac

	eval "_OB_BINFIELD_${package}_${name}=\"\${value}\""

	return 0
}