#!/bin/bash

## Copyright (C) 2025 - 2025 ENCRYPTED SUPPORT LLC <adrelanos@whonix.org>
## See the file COPYING for copying conditions.

set -o errexit
set -o nounset
set -o errtrace
set -o pipefail

command -v stsponge >/dev/null

MYDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
cd "$MYDIR"

## Sanity test.
localectl --no-pager >/dev/null

keymap_layouts="$(localectl --no-pager list-x11-keymap-layouts)"
stsponge ./localectl-list-x11-keymap-layouts-static.txt <<< "$keymap_layouts"

localectl --no-pager list-x11-keymap-options | stsponge ./localectl-list-x11-keymap-options-static.txt

## Variants have to be handled on a per-layout basis.
## NOTE: localectl also supports displaying *all* layout variants, but this is
##   of limited use since the set of available variants is dependent upon the
##   layout in use. Not implemented in localectl-static yet.
mkdir --parents -- ./localectl-list-x11-keymap-variants-static.d

for keymap_layout in $keymap_layouts; do
  ## localectl will exit non-zero if $keymap_layout refers to a layout that
  ## has no variants.
  localectl --no-pager list-x11-keymap-variants "$keymap_layout" 2>/dev/null \
    | stsponge "./localectl-list-x11-keymap-variants-static.d/$keymap_layout" \
    || true
done
