summaryrefslogtreecommitdiffstats
path: root/src/ob-gencontrol.sh
blob: 172f50f05e62da455fda93efe32e3a105e672f73 (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
# Generate a control directory with a control file and scripts
#
# 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/>.

set -eu

calc_inst_size()
{
	local binary="${1}"
	shift 1
	local inst_size=
	local sizes=
	local size=

	# Calculate installed size.
	# Don't use du, since that considers the allocated size of files,
	# symbolic links, and directories (i.e. actual file sizes plus the
	# filesystem overhead on the build system).
	# See the following for more information:
	#   <http://pubs.opengroup.org/onlinepubs/9699919799/utilities/du.html>
	#   <http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=630533>
	#   <https://www.gnu.org/software/coreutils/manual/html_node/du-invocation.html>
	inst_size=0
	if [ -d "${binary}.data" ]; then
		sizes="$(find "${binary}.data" -type f -exec wc -c '{}' ';' | \
			cut -d ' ' -f 1)"
		for size in ${sizes}; do
			inst_size=$((${inst_size} + ${size}))
		done
	fi
	# Convert bytes to kibibytes and round up.
	# Note: There is an inconsistency between the Debian Policy Manual and
	# opkg in the units of this field.  Debian Policy defines this field in
	# units of kibibytes:
	#   The disk space is given as the integer value of the estimated
	#   installed size in bytes, divided by 1024 and rounded up.
	# However, opkg apparently attempts to convert this value from bytes to
	# kibibytes in its determination of whether the package's data will fit
	# on the system:
	#   pkg_size_kbs = (pkg->installed_size + 1023)/1024;
	#   (from libopkg/opkg_install.c line 245)
	#   (still in upstream as of 2019-02-25, libopkg/opkg_install.c:118)
	# In other words, if a package declares an installed size of 10 KiB,
	# opkg checks for 10 MiB of available space.
	# TODO: Further investigate opkg's disk space calculation and, if
	# necessary, patch opkg and submit a bug report.
	printf '%d' $(((${inst_size} + 1023) / 1024))
}

gen_control_src()
{
	local binary=
	local name=
	local value=
	local homepage=

	binary="src-${OPK_SOURCE}"

	mkdir -p -- "${binary}.control"

	cat >"${binary}.control/control" <<-EOF
		Package: src-${OPK_SOURCE}
		Source: ${OPK_SOURCE}
		Binary:$(printf '%s\n' $(ob_get_binary_packages) | \
			LC_ALL='C' sort | xargs printf ' %s')
		Version: ${OPK_SOURCE_VERSION}
		Architecture: src
		Platform: all
		Maintainer: $(ob_get_source_parameter 'Maintainer' | \
			tr '\n' ' ')
		EOF

	for name in Build-Depends; do
		value="$(ob_get_source_parameter "${name}")"
		if [ -z "${value}" ]; then
			continue
		fi
		value="$(ob_substvars "${value}")"
		printf '%s: %s\n' "${name}" "${value}" | \
			sed 's/[, ]*$//' \
			>>"${binary}.control/control"
	done

	cat >>"${binary}.control/control" <<-EOF
		Installed-Size: $(calc_inst_size "src-${OPK_SOURCE}")
		Description: ${OPK_SOURCE} source package
		EOF

	homepage="$(ob_get_source_parameter 'Homepage')"
	if [ -n "${homepage}" ]; then
		printf '%s: %s\n' 'Homepage' "${homepage}" \
			>>"${binary}.control/control"
	fi

	return 0
}

gen_control_bin()
{
	local binary="${1}"
	local version="${2}"
	local arch="${3}"
	local plat="${4}"
	local desc="${5}"
	shift 5
	local name=
	local value=
	local homepage=

	mkdir -p -- "${binary}.control"

	cat >"${binary}.control/control" <<-EOF
		Package: ${binary}
		Source: ${OPK_SOURCE}
		Version: ${version}
		Architecture: ${arch}
		Platform: ${plat}
		Maintainer: $(ob_get_source_parameter 'Maintainer' | \
			tr '\n' ' ')
		EOF

	if :; then
		for name in Essential Depends Recommends Suggests Pre-Depends \
				Conflicts Provides Replaces; do
			value="$(ob_get_binary_parameter "${binary}" "${name}")"
			if [ -z "${value}" ]; then
				continue
			fi
			case "${name}" in
				'Essential')
					;;
				'Depends' | 'Recommends' | \
					'Suggests' | 'Pre-Depends')
					value="$(ob_reduce_deps \
						-a "${OPK_HOST_ARCH}" \
						-p "${OPK_HOST_PLAT}" \
						-- "${value}")"
					value="$(ob_substvars "${value}" \
						"${binary}")"
					;;
				'Conflicts' | 'Provides' | 'Replaces')
					value="$(ob_reduce_deps \
						-a "${OPK_HOST_ARCH}" \
						-p "${OPK_HOST_PLAT}" \
						-u -- "${value}")"
					value="$(ob_substvars "${value}" \
						"${binary}")"
					;;
			esac
			printf '%s: %s\n' "${name}" "${value}" | \
				sed 's/[, ]*$//' \
				>>"${binary}.control/control"
		done
	fi

	cat >>"${binary}.control/control" <<-EOF
		Installed-Size: $(calc_inst_size "${binary}")
		Description:$(printf '%s' "${desc}" | sed 's/^/ /')
		EOF

	homepage="$(ob_get_source_parameter 'Homepage')"
	if [ -n "${homepage}" ]; then
		printf '%s: %s\n' 'Homepage' "${homepage}" \
			>>"${binary}.control/control"
	fi

	return 0
}

install_maintainer_scripts()
{
	local binary="${1}"
	shift 1
	local script=
	local target=

	for script in preinst postinst prerm postrm; do
		if [ -L "../${binary}.pkg/${script}" ]; then
			target="$(ls -l "../${binary}.pkg/${script}")"
			target="${target#* -> }"
			ln -s -- "${target}" "${binary}.control/${script}"
		elif [ -r "../${binary}.pkg/${script}" ]; then
			cp "../${binary}.pkg/${script}" \
				"${binary}.control/${script}"
			chmod 755 "${binary}.control/${script}"
		fi
	done

	return 0
}

gen_conffiles()
{
	local binary="${1}"
	shift 1

	if [ -d "${binary}.data/etc" ]; then
		find "${binary}.data/etc" -type f | sed "s@^${binary}.data@@" \
			>"${binary}.control/conffiles"
		if [ -z "$(head -n 1 "${binary}.control/conffiles")" ]; then
			rm -f -- "${binary}.control/conffiles"
		else
			chmod 644 "${binary}.control/conffiles"
		fi
	fi

	return 0
}

gen_md5sums()
{
	local binary="${1}"
	shift 1
	local has_data=
	local sum=
	local file=

	has_data=false
	exec 3>"${binary}.control/md5sums"
	while read -r sum file; do
		case "${file}" in '-') continue;; esac
		file="${file#${binary}.data}"
		printf '%s  %s\n' "${sum}" "${file}" >&3
		has_data=true
	done <<-EOF
		$(find "${binary}.data" -type f 2>/dev/null | sort | \
			xargs md5sum)
		EOF
	exec 3>&-

	if ! ${has_data}; then
		rm -f -- "${binary}.control/md5sums"
	else
		chmod 644 "${binary}.control/md5sums"
	fi

	return 0
}

main()
{
	local pkg=
	local arch=
	local plat=
	local desc=

	if ! ob_set_text_domain 'opkbuild'; then
		printf '%s: Error: Failed to load locale messages\n' \
			"${0##*/}" >&2
		return 1
	fi

	ob_init_package '..' || return 1

	if [ x"${OB_DO_SOURCE:+set}" = x'set' ]; then
		ob_info "$(ob_get_msg 'gen_control')" "src-${OPK_SOURCE}"
		gen_control_src
		gen_md5sums "src-${OPK_SOURCE}"
	else
		for pkg in ${OPK_PACKAGES_REDUCED}; do
			ob_info "$(ob_get_msg 'gen_control')" "${pkg}"
			ob_set_package_substvars "${pkg}"
			arch="$(ob_get_binary_parameter "${pkg}" \
				'Architecture')"
			[ x"${arch}" != x'all' ] && arch="${OPK_HOST_ARCH}"
			plat="$(ob_get_binary_parameter "${pkg}" 'Platform')"
			[ x"${plat}" != x'all' ] && plat="${OPK_HOST_PLAT}"
			desc="$(ob_get_binary_parameter "${pkg}" 'Description')"
			desc="$(ob_substvars "${desc}" "${pkg}")"
			gen_control_bin "${pkg}" "${OPK_BINARY_VERSION}" \
				"${arch}" "${plat}" "${desc}"
			install_maintainer_scripts "${pkg}"
			gen_conffiles "${pkg}"
			gen_md5sums "${pkg}"
		done
	fi

	return 0
}