summaryrefslogtreecommitdiffstats
path: root/installers/data/pc.sh
blob: 651c0bc85657cf59ab627a2de50015b17cf6b7b2 (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
set -u

_read()
{
	# The miniprokit call in the installer redirects this script to stdin,
	# so we need to reconnect stdin to the tty.  We can't do this for the
	# whole script with exec, because the shell reads the script in parts.
	read "${@}" 0<&1
}

_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}"
				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
}

printf '\nConfigure the network'
printf '\n=====================\n\n'
printf 'Hostname (e.g. "proteanos"): '
_read -r hostname
printf '%s\n' "${hostname}" >/etc/hostname
net_wlan_ifaces=false
printf 'Network configuration:\n'
_select 'Automatic' 'Manual'
if [ "x${select_result}" = 'xManual' ]; then
	exec 3>/etc/network/interfaces.local
	printf 'auto lo\niface lo inet loopback\n\n' >&3
	while true; do
		printf 'Interface (e.g. "eth0" or "wlan0") or empty to finish: '
		_read -r net_iface
		[ "x${net_iface}" = 'x' ] && break
		case "${net_iface%%[0-9]}" in
			eth|wlan);;
			*) printf 'Unknown interface type\n';;
		esac
		net_static=false
		printf 'Interface configuration:\n'
		_select 'DHCP' 'Static'
		if [ "x${select_result}" = 'xStatic' ]; then
			net_static=true
			printf 'auto %s\niface %s inet static\n' \
				"${net_iface}" "${net_iface}" >&3
			printf 'Address: (e.g. "192.168.1.2"): '
			_read -r net_addr
			printf '\taddress %s\n' "${net_addr}" >&3
			printf 'Network mask: (e.g. "255.255.255.0"): '
			_read -r net_mask
			printf '\tnetmask %s\n' "${net_mask}" >&3
			printf 'Default gateway: (e.g. "%s") or empty: ' \
				'192.168.1.1'
			_read -r net_gateway
			if [ "x${net_gateway}" != 'x' ]; then
				printf '\tgateway %s\n' "${net_gateway}" >&3
			fi
			printf 'DNS servers: (e.g. "192.168.1.1 10.0.0.1"): '
			_read -r net_dns
			net_dns="$(printf '%s\n' "${net_dns}" | \
				sed 's/[^0-9. ]//g')"
			exec 4>/etc/resolv.conf
			for net_ns in ${net_dns}; do
				printf 'nameserver %s\n' "${net_ns}" >&4
			done
			exec 4>&-
			printf 'Enable DHCP server:\n'
			_select 'Yes' 'No'
			if [ "x${select_result}" = 'xYes' ]; then
				printf 'Start host address (e.g. "%s"): ' \
					'192.168.1.100'
				_read -r net_dhcpd_start
				printf '\tdhcpd-start %s\n' "${net_dhcpd_start}" \
					>&3
				printf 'End host address (e.g. "%s"): ' \
					'192.168.1.254'
				_read -r net_dhcpd_end
				printf '\tdhcpd-end %s\n' "${net_dhcpd_end}" >&3
				printf 'Lease time (e.g. "864000"): '
				_read -r net_dhcpd_lease
				printf '\tdhcpd-option lease %s\n' \
					"${net_dhcpd_lease}" >&3
			fi
		else
			printf 'auto %s\niface %s inet dhcp\n' \
				"${net_iface}" "${net_iface}" >&3
		fi
		case "${net_iface%%[0-9]}" in wlan)
			net_wlan_ifaces=true
			if ${net_static}; then
				printf 'Wi-Fi configuration:\n'
				_select 'Access point' 'Client'
				if [ "x${select_result}" = 'xClient' ]; then
					printf '\twpa-mode 0\n' >&3
				else
					printf '\twpa-mode 2\n' >&3
				fi
			else
				# Force client mode with DHCP.
				printf '\twpa-mode 0\n' >&3
			fi
			printf 'ESSID (name): '
			_read -r net_wpa_ssid
			printf '\twpa-ssid %s\n' "${net_wpa_ssid}" >&3
			printf 'Security:\n'
			_select 'None' 'WPA2-PSK'
			if [ "x${select_result}" = 'xWPA2-PSK' ]; then
				printf '\twpa-proto RSN\n' >&3
				printf '\twpa-key-mgmt WPA-PSK\n' >&3
				printf 'Pre-shared key (passphrase): '
				_read -r net_wpa_psk
				printf '\twpa-psk %s\n' "${net_wpa_psk}" >&3
			else
				printf '\twpa-key-mgmt NONE\n' >&3
			fi
			;;
		esac
		printf '\n' >&3
	done
	exec 3>&-
else
	printf 'Install WPA supplicant:\n'
	_select 'Yes' 'No'
	if [ "x${select_result}" = 'xYes' ]; then
		net_wlan_ifaces=true
	fi
fi
if ${net_wlan_ifaces}; then
	opkg install wpa-supplicant
fi

printf '\nSet up users and passwords'
printf '\n==========================\n\n'
while ! passwd 0<&1; do
	:
done
printf '\n'

tzpkgs="$(opkg info 'tzdata-*' | sed -n 's/^Package: //p')"
opkg install tzcode tzdata ${tzpkgs}
printf '\nConfigure the time zone'
printf '\n=======================\n\n'
tz="$(tzselect 0<&2)"
printf '\n'
ln -sf "/usr/share/zoneinfo/${tz}" /etc/localtime
seltzpkg="$(opkg search "/usr/share/zoneinfo/${tz}" | cut -d ' ' -f 1)"
rmtzpkgs=''
for tzpkg in ${tzpkgs}; do
	if ! [ "x${tzpkg}" = "x${seltzpkg}" ]; then
		rmtzpkgs="${rmtzpkgs} ${tzpkg}"
	fi
done
opkg remove tzcode ${rmtzpkgs}

opkg install lilo
boot="$(mountpoint -n /boot | cut -d ' ' -f 1)"
boot="${boot%%[0-9]}"
printf '\nInstall the boot loader'
printf '\n=======================\n\n'
printf 'Name of root partition on running system (e.g. "sda1"): '
_read -r root
printf '\n'
# XXX: Temporary kludge.
cp -p /boot/vmlinuz /boot/vmlinuz.old
install-lilo "${boot}" "/dev/${root}" >&2

printf '\nCustom configuration'
printf '\n====================\n\n'
printf 'Additional packages to install (e.g. "fbi cmatrix acpi") or empty: '
_read -r custom_pkgs
printf '\n'
if [ "x${custom_pkgs}" != 'x' ]; then
	opkg install ${custom_pkgs}
fi

printf '\nInstallation complete!\n'