summaryrefslogtreecommitdiffstats
path: root/src/opkbootstrap.sh
blob: 3ad3c276d31c366ad84726aa4acb9119df8eb9b6 (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
#!@@SHELL@@
#
# opkbootstrap
# src/opkbootstrap.sh
# Main program script.
#
# 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/>.

TARGET=                                 # set in main
LISTS_DIR=/var/lib/opkg/lists
PKGS_DIR=/var/cache/opkg/archives

ARCH_MACHINE=                           # set in main
ARCH_SYSTEM=                            # set in main
PLAT_SYSTEM=                            # set in main

PKGS_ESSENTIAL=                         # set in find_essential
PKGS_ALL=                               # set in find_essential and find_depends
PKGS_INCLUDE=                           # set in main
PKGS_EXCLUDE=                           # set in main

# main <target> <arch> <src> [<src>]...
main()
{
	SCRIPT_NAME="${0}"

	while getopts 'hVi:x:' opt; do
		case "${opt}" in
			h)
				print_help
				exit
				;;
			V)
				print_version
				exit
				;;
			i)
				PKGS_INCLUDE="${PKGS_INCLUDE},${OPTARG}"
				;;
			x)
				PKGS_EXCLUDE="${PKGS_EXCLUDE},${OPTARG}"
				;;
			?)
				print_usage >&2
				exit 1
				;;
		esac
	done
	shift $(($OPTIND - 1))

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

	TARGET="${1}"
	ARCH_MACHINE="${2%:*}"
	ARCH_SYSTEM="${2#*:}"
	PLAT_SYSTEM="${3}"
	shift 3

	set_paths

	# Find packages.
	for src; do
		get_src_list ${src}
	done
	find_essential
	find_depends

	# Download packages.
	get_pkgs

	# Unpack packages ("first stage" installation in debootstrap terms).
	unpack_pkgs

	# Configure packages ("second stage" installation in debootstrap terms).
	if [ "${ARCH_MACHINE}" = "${ARCH_SYSTEM}" ]; then
		setup_chroot
		configure_pkgs
	else
		printf 'Cross installation currently not supported.  Exiting...\n'
		exit
	fi
}

# print_usage
print_usage()
{
	cat <<EOF
Usage: ${SCRIPT_NAME} [OPTION]... <target> <arch> <plat> <src> [<src>]...
EOF
}

# print_help
print_help()
{
	print_usage

	cat <<EOF
Bootstrap the installation of a base system into a target directory.

    -h              display this help and exit
    -V              display version information and exit
    -i A,B,C        includes specified packages in the base system
    -x A,B,C        excludes specified packages from the base system
EOF
}

# print_version
print_version()
{
	cat <<EOF
@@PACKAGE@@ @@VERSION@@
Copyright (C) 2012 Patrick "P. J." McDermott
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Written by P. J. McDermott.
EOF
}

# error <fmt> [<args>]...
error()
{
	_msg="${1}"
	shift
	printf "Error: ${_msg}\n" ${@}
	exit 2
}

# set_paths
set_paths()
{
	TARGET=$(cd "${TARGET}" && pwd)
	LISTS_DIR="${TARGET}/${LISTS_DIR}"
	PKGS_DIR="${TARGET}/${PKGS_DIR}"
}

# get_src_list src|src/gz <name> <value> [<comp>]
# get_src_list dist|dist/gz <name> <value> [<comp>]...
get_src_list()
{
	case "${1}" in
		src)
			shift
			get_pkg_src_list false ${@}
			;;
		src/gz)
			shift
			get_pkg_src_list true ${@}
			;;
		dist)
			shift
			get_dist_src_list false ${@}
			;;
		dist/gz)
			shift
			get_dist_src_list true ${@}
			;;
		*)
			error 'Invalid source type "%s"' "${1}"
			;;
	esac
}

# get_pkg_src_list <gzip> <name> <value> [<comp>]
get_pkg_src_list()
{
	if [ ${#} -ne 3 -a ${#} -ne 4 ]; then
		error 'Malformed src configuration'
	fi

	_gzip=${1}
	_name="${2}"
	_value="${3}"
	_comp="${4}"

	_url="${_value}/${_comp}/Packages"

	if ${_gzip}; then
		wget -q -O - "${_url}.gz" | gzcat > "${LISTS_DIR}/${_name}"
	else
		wget -q -O "${LISTS_DIR}/${_name}" "${_url}"
	fi
}

# get_dist_src_list <gzip> <name> <value> [<comp>]...
get_dist_src_list()
{
	:
}

# find_essential
find_essential()
{
	for _list in "${LISTS_DIR}/"*; do
		_pkg=
		_essential=false
		while read _line; do
			if [ -z "${line}" ]; then
				if ${_essential}; then
					PKGS_ESSENTIAL="${PKGS_ESSENTIAL} ${_pkg}"
					PKGS_ALL="${PKGS_ALL} ${_pkg}"
				fi
				_pkg=
				_essential=false
				continue
			fi
			if [ "${_line#Package: }" != "${_line}" ]; then
				_pkg="${_line#Package: }"
				continue
			fi
			if [ "${_line}" = 'Essential: yes' ]; then
				_essential=true
				continue
			fi
			# TODO: Make sure Architecture is one of:
			# all, ${ARCH_SYSTEM}, or ${PLAT_SYSTEM}
		done < "${_list}"
	done
}

# find_depends
find_depends()
{
	:
}

# get_pkgs
get_pkgs()
{
	:
}

# unpack_pkgs
unpack_pkgs()
{
	cd "${TARGET}"

	for _opk in "${PKGS_DIR}"/*; do
		_pkg="${opk##*/}"
		_pkg="${pkg%%_*}"

		tar -xzOf "${_opk}" data.tar.gz | tar -xz

		mkdir -p tmp/control
		(cd tmp/control; tar -xzf "${_opk}" control.tar.gz)
		for _script in conffiles md5sums preinst postinst prerm postrm; do
			if [ -e tmp/control/${_file} ]; then
				mv tmp/control/${_script} "${INFO_DIR}/${_pkg}.${_file}"
			fi
		done
		# TODO: Generate pkg.list
		rm -Rf tmp/control
	done
}

# setup_chroot
setup_chroot()
{
	:
}

# configure_pkgs
configure_pkgs()
{
	:
}

main ${@}