summaryrefslogtreecommitdiffstats
path: root/debeagle.sh
blob: dad40db829ca3c5e1446bc3907c0a8e4e6b5baa8 (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
#!/bin/sh
#
# debeagle - Install Debian on a Beagle SBC
#
# Copyright (C) 2014  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/>.

set -u

main()
{
	local opt=
	local mirror=
	local suite=
	local target=

	while getopts 'm:' opt; do
		case ${opt} in
			m) mirror="${OPTARG}";;
			?)
				print_usage >&2
				exit 1
				;;
		esac
	done
	shift $(($OPTIND - 1))

	case ${#} in
		1)
			suite="${1}"
			target=''
			;;
		2)
			suite="${1}"
			target="${2}"
			;;
		*)
			print_usage >&2
			exit 1
			;;
	esac

	if [ "x${target}" = 'x' ]; then
		target="/dev/$(prompt_block_dev)"
	elif [ -e "/sys/block/${target#/dev/}" ]; then
		printf 'Installing to %s\n' \
			"$(get_block_dev_name "${target#/dev/}")"
	else
		printf 'Device %s not found\n' "${target}" >&2
		exit 1
	fi
}

print_usage()
{
	printf 'Usage: %s [-m <mirror>] <suite> [<target>]\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
}

pretty_print_size_dec()
{
	local size="${1}"

	awk -v size=${size} 'BEGIN {
		split("B KB MB GB TB", units, " ");
		i=1;
		while (size >= 1000) {
			size /= 1000;
			++i;
		};
		printf("%.2f %s\n", size, units[i]); }'
}

pretty_print_size_bin()
{
	local size="${1}"

	awk -v size=${size} 'BEGIN {
		split("B KiB MiB GiB TiB", units, " ");
		i=1;
		while (size >= 1024) {
			size /= 1024;
			++i;
		};
		printf("%.2f %s\n", size, units[i]); }'
}

get_block_dev_name()
{
	local dev="${1}"
	local vendor=
	local model=

	if [ -e "/sys/block/${dev}/device/vendor" ]; then
		vendor="$(cat "/sys/block/${dev}/device/vendor")"
		model="$(cat "/sys/block/${dev}/device/model")"
		name="${vendor} ${model}"
	else
		name="$(cat "/sys/block/${dev}/device/name")"
	fi

	printf '%s' "${name}"
}

prompt_block_dev()
{
	local dev=
	local name=
	local size=
	local size_s=
	local desc=
	local devs=

	set --
	for dev in /sys/block/*; do
		dev="${dev#/sys/block/}"
		case "${dev}" in
			loop*|sr*) continue;;
		esac
		name="$(get_block_dev_name "${dev}")"
		size="$(cat "/sys/block/${dev}/size")"
		size="$(expr ${size} \* 512)"
		size_s="$(pretty_print_size_dec "${size}")"
		size_s="${size_s} ($(pretty_print_size_bin "${size}"))"
		desc="$(printf '%s (%s) - %s\n' "${name}" "${dev}" "${size_s}")"
		set -- "${@}" "${desc}"
		devs="$(printf '%s\n%s\t%s\n' "${devs}" "${desc}" "${dev}")"
	done

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

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

main "${@}"