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

PREFIX=
. $PREFIX/lib/libalpine.sh

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

Natanael Copa's avatar
Natanael Copa committed
8 9
if [ -f "$ROOT/etc/conf.d/keymaps" ]; then
	. "$ROOT/etc/conf.d/keymaps"
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
	variant=$(basename $KEYMAP .bmap.gz)
fi


show_keymaps() {
	local opwd="$PWD"
	cd "$ROOT/usr/share/bkeymaps"
	ls --color=never
	cd "$opwd"
}

select_layout() {
	while true; do
		if [ -z "$layout" ]; then
			layout=none
		fi
		echo "Available keyboard layouts:"
		show_keymaps
		echon "Select keyboard layout [$layout]: "
		default_read layout "$layout"
		if [ "$layout" = "abort" ] || [ "$layout" = "none" ] ; then
			goodbye 0
		elif [ -d "$MAPDIR/$layout" ] ; then
			return 0
		fi
	done
}

setup_mapfile() {
	local name=$(basename $1)
Natanael Copa's avatar
Natanael Copa committed
40
	local conf="$ROOT/etc/conf.d/keymaps"
41 42 43 44 45 46 47
	mkdir -p "$ROOT/etc/keymap"
	mkdir -p "$ROOT/etc/conf.d/"
	if gzip -9 -c "$1" > "$ROOT/etc/keymap/$name.gz" ; then
		[ -f "$conf" ] && sed -i '/^KEYMAP=/d' "$conf"
		echo "KEYMAP=/etc/keymap/$name.gz" >> "$conf"
		# we actually load the keymap now
		zcat /etc/keymap/$name.gz | loadkmap
48
		rc-update -q add keymaps boot
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
		goodbye 0
	fi
}

select_variant() {
	while true; do
		echon "Available variants: "
		for i in $(ls $MAPDIR/$layout) ; do
			echon "$(basename $i .bmap) "
		done
		echo ""
		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
		if [ -f "$MAPDIR/$layout/$variant.bmap" ]; then
			setup_mapfile "$MAPDIR/$layout/$variant.bmap"
		fi
	done	
}

goodbye() {
	if [ $was_installed -ne 0 ]; then
		pkg_deinst bkeymaps
	fi
	exit $1
}

apk_info -e bkeymaps
was_installed=$?

pkg_inst bkeymaps

deflayout="$1"
while true; do

	if [ -n "$deflayout" ]; then
		layout="$deflayout"
		unset deflayout
	else
		select_layout
	fi

	# if there is only one variant, just pick it
	count=$(ls $MAPDIR/$layout | wc -l)
	if [ $count -eq 1 ]; then
		setup_mapfile "$MAPDIR/$layout/$(ls $MAPDIR/$layout)"
		continue
	fi

	select_variant
done