setup-keymap.in 2.38 KB
Newer Older
1 2 3 4 5 6 7
#!/bin/sh

PREFIX=
. $PREFIX/lib/libalpine.sh

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

8 9
if [ -f "$ROOT/etc/conf.d/loadkmap" ]; then
	. "$ROOT/etc/conf.d/loadkmap"
10 11
	variant=$(basename "KEYMAP")
	variant="${variant%%.*}"
12 13 14
fi


15 16 17
show_layouts() {
	local owpd; owpd="$PWD"
	cd "$MAPDIR"
18
	ls --color=never
19 20 21 22 23 24 25 26 27
	cd "$owpd"
}

show_variants() {
	local i; for i in "$MAPDIR/$1"/* ; do
		i="$(basename "$i")"
		printf "%s " "${i%%.*}"
	done
	echo
28 29 30 31 32 33 34 35
}

select_layout() {
	while true; do
		if [ -z "$layout" ]; then
			layout=none
		fi
		echo "Available keyboard layouts:"
36
		show_layouts
37 38 39 40
		echon "Select keyboard layout [$layout]: "
		default_read layout "$layout"
		if [ "$layout" = "abort" ] || [ "$layout" = "none" ] ; then
			goodbye 0
41 42 43 44 45 46 47
		fi
		set -- $layout
		if [ $# -eq 2 ]; then
			layout="$1"
			variant="$2"
		fi
		if [ -d "$MAPDIR/$layout" ] ; then
48 49 50 51 52 53
			return 0
		fi
	done
}

setup_mapfile() {
54
	local name=$(basename "$1")
55
	local conf="$ROOT/etc/conf.d/loadkmap"
56 57
	mkdir -p "$ROOT/etc/keymap"
	mkdir -p "$ROOT/etc/conf.d/"
58
	if cp "$1" "$ROOT/etc/keymap/$name" ; then
59
		[ -f "$conf" ] && sed -i '/^KEYMAP=/d' "$conf"
60
		echo "KEYMAP=/etc/keymap/$name" >> "$conf"
61
		# we actually load the keymap now
62 63
		rc-service loadkmap restart
		rc-update -q add loadkmap boot
64 65 66 67 68 69 70
		goodbye 0
	fi
}

select_variant() {
	while true; do
		echon "Available variants: "
71
		show_variants "$layout"
72 73 74 75 76 77 78 79
		if [ ! -f "$MAPDIR/$layout/$variant.bmap" ] ; then
			variant=""
		fi
		echon "Select variant [$variant]: "
		default_read variant "$variant"
		if [ "$variant" = "abort" ] || [ "$variant" = "none" ]; then
			break;
		fi
80 81
		if [ -f "$MAPDIR/$layout/$variant.bmap"* ]; then
			setup_mapfile "$MAPDIR/$layout/$variant.bmap"*
82
		fi
83
	done
84 85 86
}

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

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

deflayout="$1"
95
defvariant="$2"
96 97 98 99 100 101 102 103
while true; do
	if [ -n "$deflayout" ]; then
		layout="$deflayout"
		unset deflayout
	else
		select_layout
	fi

104 105
	if [ -n "$defvariant" ]; then
		variant="$defvariant"
106 107
	fi

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