summaryrefslogtreecommitdiff
path: root/setup-keymap.in
blob: 4a57fde0dd04ea7b1ac74bc631de52224ec59a15 (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
#!/bin/sh

PREFIX=
. $PREFIX/lib/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


show_layouts() {
	local owpd; owpd="$PWD"
	cd "$MAPDIR"
	ls --color=never
	cd "$owpd"
}

show_variants() {
	local i; for i in "$MAPDIR/$1"/* ; do
		i="$(basename "$i")"
		printf "%s " "${i%%.*}"
	done
	echo
}

select_layout() {
	while true; do
		if [ -z "$layout" ] || ! [ -d "$MAPDIR/$layout" ]; then
			layout=none
		fi
		echo "Available keyboard layouts:"
		show_layouts
		ask "Select keyboard layout:" "$layout"
		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
		echon "Available variants: "
		show_variants "$layout"
		if [ ! -f "$MAPDIR/$layout/$variant.bmap" ] ; then
			variant=""
		fi
		ask "Select variant (or 'abort'):" "$variant"
		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
}

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

deflayout="$1"
defvariant="$2"
while true; do
	if [ -n "$deflayout" ]; then
		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