summaryrefslogtreecommitdiffstats
path: root/src/oh-gencontrol
blob: a1fa9d61b9d20ca1ee74e3f7d91b94ecafd73136 (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
#! /bin/sh
#
# opkhelper
# src/oh-gencontrol
# Generates a control directory with a control file and scripts.
#
# Copyright (C) 2012 Patrick "P. J." McDermott
#
# This program 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.
#
# This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.

. @@LIBDIR@@/opkhelper/controlfields

print_usage()
{
	printf 'Usage: %s -s | pkgname...\n' "$1"
}

missing_field()
{
	printf 'oh-gencontrol: Error: Missing field "%s" in control file.\n' \
		"${1}" >&2
	exit 2
}

invalid_pkg_name()
{
	printf 'oh-gencontrol: Error: Invalid package name: "%s"\n' "${1}" >&2
	exit 2
}

copy_required_field()
{
	if [ ${#} -eq 2 ]; then
		{ printf '%s: ' "${1}" && oh_get_field "${1}";} >> "${2}"
		[ ${?} -eq 1 ] && missing_field "${1}"
	else
		{ printf '%s: ' "${2}" && oh_get_field "${1}" "${2}";} >> "${3}"
		[ ${?} -eq 1 ] && missing_field "${2}"
	fi
}

copy_optional_field()
{
	if [ ${#} -eq 2 ]; then
		field=$(printf '%s: ' "${1}" && oh_get_field "${1}")
		[ ${?} -eq 0 ] && printf '%s\n' "${field}" >> "${2}"
	else
		field=$(printf '%s: ' "${2}" && oh_get_field "${1}" "${2}")
		[ ${?} -eq 0 ] && printf '%s\n' "${field}" >> "${3}"
	fi
}

gen_control_src()
{
	srcpkg=$(oh_get_field Source) || missing_field Source
	oh_validate_pkg_name "${srcpkg}" || invalid_pkg_name "${srcpkg}"

	printf 'oh-gencontrol: Generating control directory for package "%s"...\n' \
		"${srcpkg}-src"

	control_dir="${srcpkg}-src.control"
	control="${control_dir}/control"

	mkdir "${control_dir}"
	chmod 644 "${control_dir}"

	printf 'Package: %s\n' "${srcpkg}-src" > ${control}
	printf 'Source: %s\n' "${srcpkg}" >> ${control}
	copy_required_field Version ${control}
	printf 'Architecture: all\n' >> ${control}
	# TODO: Calculate "Installed-Size".
	copy_required_field Maintainer ${control}
	copy_optional_field Homepage ${control}

	chmod 644 ${control}
}

gen_control_bin()
{
	binpkg="${1}"
	oh_validate_pkg_name "${binpkg}" || invalid_pkg_name "${binpkg}"

	printf 'oh-gencontrol: Generating control directory for package "%s"...\n' \
		"${binpkg}"

	data_dir="${binpkg}.data"
	control_dir="${binpkg}.control"
	control="${control_dir}/control"

	mkdir "${control_dir}"
	chmod 644 "${control_dir}"

	# Binary and source package names and version.
	copy_required_field ${binpkg} Package ${control}
	copy_required_field Source ${control}
	copy_required_field Version ${control}

	# TODO: Handle architecture.
	{ printf 'Architecture: ' && oh_get_field ${binpkg} Architecture;} \
		>> ${control}
	[ ${?} -eq 1 ] && missing_field Architecture

	# Optional fields.
	copy_optional_field ${binpkg} Essential ${control}
	copy_optional_field ${binpkg} Depends ${control}
	copy_optional_field ${binpkg} Recommends ${control}
	copy_optional_field ${binpkg} Suggests ${control}
	copy_optional_field ${binpkg} Pre-Depends ${control}
	copy_optional_field ${binpkg} Conflicts ${control}
	copy_optional_field ${binpkg} Provides ${control}
	copy_optional_field ${binpkg} Replaces ${control}

	# TODO: Calculate "Installed-Size".
	copy_required_field Maintainer ${control}
	copy_required_field ${binpkg} Description ${control}
	copy_optional_field Homepage ${control}

	chmod 644 ${control}

	# Maintainer scripts.
	for script in preinst postinst prerm postrm; do
		if [ -L ../${binpkg}.pkg/${script} ]; then
			target=$(ls -l "../${binpkg}.pkg/${script}" | \
				sed 's/^.* -> \(.*\)$/\1/')
			ln -s ${target} ${control_dir}/${script}
		elif [ -f ../${binpkg}.pkg/${script} ]; then
			cp ../${binpkg}.pkg/${script} ${control_dir}
			chmod 755 ${control_dir}/${script}
		fi
	done

	# Conffiles.
	# Adapted from dh_installdeb of debhelper.
	if [ -d ${data_dir}/etc ]; then
		find ${data_dir}/etc -type f | sed "s@^${data_dir}@@" \
			>> ${control_dir}/conffiles
		if [ -z "$(head -n 1 ${control_dir}/conffiles)" ]; then
			rm -f ${control_dir}/conffiles
		else
			chmod 644 ${control_dir}/conffiles
		fi
	fi

	# MD5 checksums.
	find ${data_dir} -type f | sort | xargs md5sum | sed "s@  ${data_dir}@  @" \
		> ${control_dir}/md5sums
	if [ -z "$(head -n 1 ${control_dir}/md5sums)" ]; then
		rm -f ${control_dir}/md5sums
	else
		chmod 644 ${control_dir}/md5sums
	fi
}

if [ ${#} -eq 0 ]; then
	print_usage ${0} >&2
	exit 1
fi

is_src=false
opts=$(getopt -n "${0}" -o 's' -- "${@}")
if [ ${?} -ne 0 ]; then
	print_usage "${0}" >&2
	exit 1;
fi
eval set -- "${opts}"
while true; do
	case "${1}" in
		-s)
			is_src=true
			shift
			;;
		--)
			shift
			break
			;;
		*)
			print_usage "${0}" >&2
			exit 1
			;;
	esac
done

if ${is_src}; then
	gen_control_src
else
	if [ ${#} -eq 0 ]; then
		print_usage "${0}" >&2
		exit 1
	fi

	while [ ${#} -gt 0 ]; do
		gen_control_bin "${1}"
		shift
	done
fi