automate/001_swap-caps.zsh

82 lines
1.7 KiB
Bash

#!/usr/bin/env zsh
# Create a keyboard file in /tmp to test the script
conf_print_keyboard_tmp() {
cat <<-EOF
# KEYBOARD CONFIGURATION FILE
# Consult the keyboard(5) manual page.
XKBMODEL="pc104"
XKBLAYOUT="gb"
XKBVARIANT=""
XKBOPTIONS=""
BACKSPACE="guess"
EOF
}
#conf_print_keyboard_tmp | tee /tmp/keyboard
# Default destination directory if not provided
DEST=${1:-/etc/skel}
# Create the .xinitrc.d directory if it doesn't exist
mkdir -p ${DEST}/.xinitrc.d
# Write the setxkbmap configuration to a script file
cat <<EOF | sudo tee ${DEST}/.xinitrc.d/setxkbmap.sh >/dev/null
#!/bin/zsh
setxkbmap -model pc104 -layout gb,us -option caps:swapecape
EOF
# Make the script executable
chmod +x ${DEST}/.xinitrc.d/setxkbmap.sh
# Keyboard configuration adjustments
config_file="/etc/default/keyboard"
# config_file="/tmp/keyboard"
cp $config_file $config_file-bak
declare -A rules
rules=(
"XKBMODEL" "pc104"
"XKBLAYOUT" "gb"
"XKBVARIANT" ""
"XKBOPTIONS" "caps:swapescape"
"BACKSPACE" "guess"
)
echo ""
declare -p rules
echo ""
# Iterate through the keys of the associative array
for key in "${(@k)rules}"; do
# Access the value associated with the current key
value="${rules[$key]}"
# Now you can use both $key and $value inside this loop
echo "$key = $value"
done
echo ""
for rule in "${(@k)rules}"; do
value="${rules[$rule]}"
regex="s|^#.\?\(${rule}\s*\).*$|\1=${value}|"
# sudo sed -i "${regex}" ${config_file}
if [[ -n "$value" ]]; then
echo "sudo sed -i '${regex}' ${config_file}"
sudo sed -i "${regex}" ${config_file}
fi
done
echo ""
cat ${config_file}
# Trigger udevadm to apply changes
sudo udevadm trigger --subsystem-match=input --action=change