#!/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 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() { $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