summaryrefslogtreecommitdiff
path: root/setup-keymap.in
blob: b8c521d16287bde3e33fc29810b57cff702d7649 (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
#!/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() {
	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
		printf %s "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() {
	$MOCK 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
$MOCK 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