summaryrefslogtreecommitdiffstats
path: root/src/proquivs
blob: 693d92b892089e3effb0504a603b014efa4d9a43 (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
#!/bin/sh

set -eu

. /usr/share/sh/libopkbuild.shso.2

FIELDS='Package Source Version Architecture Platform Section Maintainer
	Essential Depends Recommends Suggests Pre-Depends Conflicts Provides
	Replaces Description Homepage'

field_package='proquivs-dummy'
field_source='proquivs-dummy'
field_version='1.0'
field_architecture='all'
field_platform='all'
field_section='share'
field_maintainer='"J. Random Hacker" <jrandom@example.com>'
field_essential=''
field_depends=''
field_recommends=''
field_suggests=''
field_pre_depends=''
field_conflicts=''
field_provides=''
field_replaces=''
field_description='Trivial package for testing
This is a trivial package generated by proquivs.  It should not be uploaded to
the ProteanOS package archive.'
field_homepage=''

control()
{
	local ctl="${1}"
	shift 1
	local name=
	local value=

	exec 3>"${ctl}"

	cat 1>&3 <<-EOF
		#######################################################
		# Fields are commented out and set to default values. #
		# Uncomment and edit as needed.                       #
		#######################################################
		EOF

	for name in ${FIELDS}; do
		eval "value=\"\$(printf '%s' \"\${field_$(\
			printf '%s' "${name}" | tr 'A-Z-' 'a-z_')}\" | \
			sed 's/^/ /')\""
		printf '%s:%s\n' "${name}" "${value}" | sed 's/^/# /' 1>&3
	done

	exec 3>&-

	return 0
}

field_cb()
{
	local name="${1}"
	local value="${2}"
	local user_data="${3}"
	shift 3

	case " ${FIELDS} " in
		*" ${name} "*)
			;;
		*)
			return 0
			;;
	esac
	eval "field_$(printf '%s' "${name}" | tr 'A-Z-' 'a-z_')=\"\${value}\""

	return 0
}

build()
{
	local ctl="${1}"
	shift 1
	local name=
	local value=

	ob_parse_control "${ctl}" field_cb '' '' "${FIELDS}"

	if ! mkdir 'proquivs'; then
		printf 'proquivs: Error: Failed to create package\n' 1>&2
		return 1
	fi
	cd 'proquivs'

	printf '2.0\n' 1>'format'

	exec 3>'control'
	printf 'Maintainer: %s\n' "${field_maintainer}" 1>&3
	if [ -n "${field_homepage}" ]; then
		printf 'Homepage: %s\n' "${field_homepage}" 1>&3
	fi
	exec 3>&-

	cat 1>'changelog' <<-EOF
		${field_source} (${field_version}) UNRELEASED

		  * Initial release.

		 -- ${field_maintainer}  $(LC_ALL='POSIX' \
			 date '+%a, %d %b %Y %H:%M:%S %z')
		EOF

	cat 1>'build' <<-EOF
		#!/usr/bin/make -f

		build install:
			$(printf '\t')@:
		EOF
	chmod 0755 'build'

	mkdir -- "${field_package}.pkg"

	exec 3>"${field_package}.pkg/control"
	printf 'Architecture: %s\n' "${field_architecture}" 1>&3
	printf 'Platform: %s\n' "${field_platform}" 1>&3
	for name in Section Essential Depends Recommends Suggests Pre-Depends \
			Conflicts Provides Replaces; do
		eval "value=\"\$(printf '%s' \"\${field_$(\
			printf '%s' "${name}" | tr 'A-Z-' 'a-z_')}\" | \
			sed 's/^/ /')\""
		if [ -n "${value}" ]; then
			printf '%s:%s\n' "${name}" "${value}" 1>&3
		fi
	done
	printf 'Description:%s\n' "$(printf '%s' "${field_description}" | \
		sed 's/^/ /')" 1>&3
	exec 3>&-

	1>"${field_package}.pkg/docs"

	if ! opkbuild -b; then
		cd '..'
		rm -Rf 'proquivs'
		printf 'proquivs: Error: Failed to create package\n' 1>&2
		return 1
	fi

	cd '..'
	rm -Rf 'proquivs'
	# Remove .changes file.  This package shouldn't be uploaded to a
	# pro-archman incoming queue.
	rm -f -- "${field_package}_${field_version}$(: \
		)_$(cat /etc/proteanos_arch)_$(cat /etc/proteanos_plat).changes"

	printf '\nproquivs: Package "%s_%s_%s_%s.opk" built\n' \
		"${field_package}" "${field_version}" \
		"${field_architecture}" "${field_platform}"

	return 0
}

usage()
{
	printf 'Usage: %s control|build <control-file>\n' "${0}"
	printf 'The "control" command writes a template to the specified '$(: \
		)'file name.\n'
	printf 'The "build" command creates a package as described in the '$(: \
		)'specified file.\n'
	return 0
}

main()
{
	local cmd=
	local ctl=

	if [ ${#} -ne 2 ]; then
		usage 1>&2
		return 1
	fi
	cmd="${1}"
	ctl="${2}"
	case "${cmd}" in
		'control' | 'build')
			;;
		*)
			return 1
			;;
	esac

	if "${cmd}" "${ctl}"; then
		return 0
	else
		return ${?}
	fi
}

main "${@}"