aboutsummaryrefslogtreecommitdiffstats
path: root/setup-keymap.in
blob: 39cc8a5a97c4bdb817926f49e8be110e3536f25a (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
#!/bin/sh

PREFIX=@PREFIX@
: ${LIBDIR=$PREFIX/lib}
. "$LIBDIR/libalpine.sh"

MAPDIR="$ROOT/usr/share/bkeymaps"

if [ -f "$ROOT/etc/conf.d/loadkmap" ]; then
	. "$ROOT/etc/conf.d/loadkmap"
	variant=$(basename "KEYMAP")
	variant="${variant%%.*}"
fi

usage() {
	cat <<-__EOF__
		usage: setup-keymap [-h] [LAYOUT [VARIANT]]

		Sets the system keymap layout and variant.

		options:
		 -h  Show this help

		Sets keymap to LAYOUT (and VARIANT) or prompt if unspecified.
	__EOF__
	exit $1
}


show_layouts() {
	output=""
	for file in "$MAPDIR"/*; do
		base=$(basename "$file")
		output="$output${base%.bmap.gz} "
	done
	print_column "$output"
}

show_variants() {
	output=""
	for file in "$MAPDIR/$1"/*; do
		base=$(basename "$file")
		output="$output${base%.bmap.gz} "
	done
	print_column "$output"
}

print_column() {
	if [ -z "$1" ]; then
		echo "Error: No data to display." >&2
		return 1
	fi

	local terminal_width=$(stty size | cut -d' ' -f2)
	if [ -z "$terminal_width" -o "$terminal_width" -eq 0 ]; then
		terminal_width=80
	fi

	max_word_length=$(echo "$1" | awk '{for(i=1;i<=NF;i++) x = (length($i) > x ? length($i) : x)} END {print x}')
	number_of_columns=$((terminal_width / (max_word_length + 1)))

	if [ "$number_of_columns" -eq 0 ]; then
		number_of_columns=1
	fi

	words=$(echo $1)
	i=0
	for word in $words; do
		printf "%-${max_word_length}s " "$word"
		i=$((i + 1))
		if [ $((i % number_of_columns)) -eq 0 ]; then
			printf "\n"
		fi
	done
	if [ $((i % number_of_columns)) -ne 0 ]; then
		printf "\n"
	fi
}

select_layout() {
	while true; do
		if [ -z "$layout" ] || ! [ -d "$MAPDIR/$layout" ]; then
			layout=none
		fi
		show_layouts
		echo ""
		ask "Select keyboard layout:" "$layout"
		echo ""
		if [ "$resp" = "abort" ] || [ "$resp" = "none" ] ; then
			goodbye 0
		fi
		set -- $resp
		layout="$1"
		variant="$2"
		if [ -d "$MAPDIR/$layout" ] ; then
			return 0
		fi
	done
}

setup_mapfile() {
	local name="$(basename "$1")"
	local conf="$ROOT/etc/conf.d/loadkmap"
	mkdir -p "$ROOT/etc/keymap"
	mkdir -p "$ROOT/etc/conf.d/"
	if cp "$1" "$ROOT/etc/keymap/$name" ; then
		[ -f "$conf" ] && sed -i '/^KEYMAP=/d' "$conf"
		echo "KEYMAP=/etc/keymap/$name" >> "$conf"
		# we actually load the keymap now
		rc-service loadkmap restart
		rc-update -q add loadkmap boot
		goodbye 0
	fi
}

select_variant() {
	while true; do
		show_variants "$layout"
		if [ ! -f "$MAPDIR/$layout/$variant.bmap" ] ; then
			variant=""
		fi
		echo ""
		ask "Select variant (or 'abort'):" "$variant"
		echo ""
		variant="$resp"
		if [ "$variant" = "abort" ] || [ "$variant" = "none" ]; then
			break;
		fi
		if [ -f "$MAPDIR/$layout/$variant.bmap"* ]; then
			setup_mapfile "$MAPDIR/$layout/$variant.bmap"*
		fi
	done
}

goodbye() {
	apk del --quiet --no-progress .setup-keymap-deps
	exit $1
}

while getopts "h" opt; do
	case $opt in
		h) usage 0;;
		'?') usage "1" >&2;;
	esac
done
shift $(( $OPTIND - 1 ))

trap 'goodbye 1' INT
apk add --quiet --virtual .setup-keymap-deps kbd-bkeymaps

deflayout="$1"
defvariant="$2"
while true; do
	if [ -n "$deflayout" ]; then
		if [ "$deflayout" = "none" ]; then
			goodbye 0
		fi
		layout="$deflayout"
		unset deflayout
	else
		select_layout
	fi

	if [ -n "$defvariant" ]; then
		variant="$defvariant"
	fi

	# if variant is defined, this could match, otherwise we'll have to choose a variant
	if [ -f "$MAPDIR/$layout/$variant.bmap"* ]; then
		setup_mapfile "$MAPDIR/$layout/$variant.bmap"*
	else
		# if there is only one variant, just pick it
		count=$(ls "$MAPDIR"/"$layout" | wc -l)
		if [ $count -eq 1 ]; then
			setup_mapfile "$MAPDIR/$layout/"*
			continue
		fi
		select_variant
	fi
done