summaryrefslogtreecommitdiffstats
path: root/installers/pc.sh
blob: 26ae258bae626e8907c8206ecfbc385a24164ec6 (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
#!/bin/sh

set -u

# This should be maintained in order of compatibility, i.e. each arch can also
# run programs built for the arches before it.
ARCHES='i686 amd64'

# Description		CPU	Plat
PLATS='
Acer Aspire One AO751h	i686	ao751h
Dell Dimension 2400	i686	dimension2400
'

main()
{
	local opt=
	local mirror=
	local install_arch=
	local host_arch=
	local host_plat=

	while getopts 'm:' opt; do
		case ${opt} in
			m) mirror="${OPTARG}";;
			?)
				print_usage >&2
				exit 1
				;;
		esac
	done
	shift $(($OPTIND - 1))
	if [ ${#} -ne 1 ]; then
		print_usage >&2
		exit 1
	fi
	root="${1}"

	install_arch="$(detect_arch)"
	read host_arch host_plat <<-EOF
		$(prompt_plat)
		EOF
	host_arch="${host_arch}-${install_arch#*-}"
	check_arch_compat "${install_arch}" "${host_arch}"

	printf '\nInstalling base system'
	printf '\n======================\n\n'

	"${0%installers/pc.sh}./miniprokit.sh" install \
		-a "${host_arch}" -P "${host_plat}" \
		${mirror:+-m} ${mirror} "${root}"
	"${0%installers/pc.sh}./miniprokit.sh" shell "${root}" \
		<"${0%pc.sh}data/pc.sh"
}

print_usage()
{
	printf 'Usage: %s [-m <mirror>] <root>\n' "${0}"
}

error()
{
	local status=${1}
	local fmt="${2}"
	shift 2

	printf "Error: ${fmt}\n" "${@}" >&2

	exit ${status}
}

_select()
{
	# Field width of the prompt numbers.
	select_width=$(expr ${#} : '.*')

	select_i=

	while :; do
		case "${select_i}" in
			'')
				select_i=0
				for select_word in "${@}"; do
					select_i=$(expr ${select_i} + 1)
					printf "%${select_width}d) %s\\n" \
						${select_i} "${select_word}" \
						>&2
				done
				;;
			*[!0-9]*)
				printf 'Please enter a number in range.\n' >&2
				;;
			*)
				if [ ${select_i} -gt 0 ] && \
						[ ${select_i} -le ${#} ]; then
					shift $(expr $select_i - 1)
					select_result="${1}"
					break
				fi
				printf 'Please enter a number in range.\n' >&2
				;;
		esac

		# Prompt and read input.
		printf '%s' "${PS3-#? }" >&2
		read -r select_i || exit
	done
}

detect_arch()
{
	local uname_m=
	local uname_s=
	local arch=

	uname_m="$( (uname -m) 2>/dev/null)" || uname_m='?'
	uname_s="$( (uname -s) 2>/dev/null)" || uname_s='?'
	case "${uname_m}:${uname_s}" in
		'i686:Linux') arch='i686-linux-glibc';;
		'x86_64:Linux') arch='amd64-linux-glibc';;
		'?:?') error 2 'Architecture unknown';;
		*) error 2 'Architecture unsupported';;
	esac

	printf '%s\n' "${arch}"
}

prompt_plat()
{
	local desc=
	local arch=
	local plat=

	set --
	while IFS='	' read -r desc arch plat; do
		[ "x${desc}" = 'x' ] && continue
		set -- "${@}" "${desc}"
	done <<-EOF
		${PLATS}
		EOF

	printf 'Select a platform:\n' >&2
	_select "${@}"

	while IFS='	' read -r desc arch plat; do
		if [ "x${desc}" = "x${select_result}" ]; then
			printf '%s %s\n' "${arch}" "${plat}"
			break
		fi
	done <<-EOF
		${PLATS}
		EOF
}

check_arch_compat()
{
	local install_arch="${1}"
	local host_arch="${2}"
	local arches=
	local install_cpu=
	local host_cpu=

	if [ "x${install_arch}" = "x${host_arch}" ]; then
		return 0
	fi

	arches="$(printf ' %s ' ${ARCHES})"
	install_cpu="${install_arch%%-*}"
	host_cpu="${host_arch%%-*}"
	if [ "x${arches#* ${host_cpu} * ${install_cpu} }" = "x${arches}" ]
	then
		error 2 'Cannot install a system of architecture %s on %s %s' \
			"${host_arch}" 'a system of architecture' \
			"${install_arch}"
	fi
}

main "${@}"