file upgraded to version 5.36
[dragora.git] / archive / dragora-tools / dragora-keymap
blobe5e75b839ee93e029b37058d214f120b746f446d
1 #! /bin/sh -
3 # A script to configure the keyboard map on console
5 # Copyright (c) 2019 Matias Fonzo, <selk@dragora.org>.
7 # Licensed under the Apache License, Version 2.0 (the "License");
8 # you may not use this file except in compliance with the License.
9 # You may obtain a copy of the License at
11 # http://www.apache.org/licenses/LICENSE-2.0
13 # Unless required by applicable law or agreed to in writing, software
14 # distributed under the License is distributed on an "AS IS" BASIS,
15 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 # See the License for the specific language governing permissions and
17 # limitations under the License.
19 # Exit immediately on any error
20 set -e
22 PROGRAM="${0##*/}"
23 TMPDIR="${TMPDIR:-/tmp}"
24 TMPFILE="${TMPDIR}/${PROGRAM}.${RANDOM-0}$$"
25 LOCKFILE="${TMPDIR}/dragora-keymap.lockfile"
26 OUTPUT="${TMPFILE}.data"
27 KEYMAPS="${KEYMAPS:-/usr/share/keymaps/i386}"
29 chkstatus_or_exit()
31 status=$?
33 # Clean up temporary files
34 rm -f -- "$TMPFILE" "$LOCKFILE"
36 if test $status -ne 0
37 then
38 echo "Return status = $status" 1>&2
39 exit 2;
42 unset status
45 # Sanity checks
47 if test ! -d "$TMPDIR"
48 then
49 echo "${PROGRAM}: \`${TMPDIR}' is not a qualified temporary directory" 1>&2
50 exit 1;
52 if test ! -w "$TMPDIR"
53 then
54 echo "${PROGRAM}: \`${TMPDIR}' is not a writable temporary directory" 1>&2
55 exit 1;
57 if test ! -d "$KEYMAPS"
58 then
59 echo "${PROGRAM}: \`${KEYMAPS}' the specified directory is not valid" 1>&2
60 exit 1;
63 trap 'chkstatus_or_exit' EXIT HUP INT QUIT ABRT TERM
65 umask 077; # Remove access for all but user.
67 # Unfortunately, loadkeys(1) is limited to the superuser;
68 # we set a lock to allow only one instance of the script.
70 if ( set -C ; echo ": $PROGRAM - locked" > $LOCKFILE ) 2> /dev/null
71 then
72 true
73 else
74 if test -e "$LOCKFILE"
75 then
76 echo "Only one instance of \`${PROGRAM}' is allowed." 1>&2
77 exit 1;
78 else
79 echo "${PROGRAM}: \`${LOCKFILE}' lock failed." 1>&2
80 exit 2;
84 # Compose output file to be shown
86 # Use a portable find(1) in terms of options, plus sort(1),
87 # redirects the standard error to see any possible error
88 search="$(find "$KEYMAPS" -name '*.map*' -print | sort 2> /dev/tty)"
90 if test -z "$search"
91 then
92 echo "No keyboard maps were found in \`${KEYMAPS}'." 1>&2
93 exit 1;
96 # Header
97 printf "%s\n" \
98 'dialog --colors \' \
99 ' --backtitle "\Zb${PROGRAM} - Keyboard map configuration" \' \
100 ' --title "MAIN MENU" --menu \' \
101 '"Welcome to \Zb${PROGRAM}\Zn!\n\n\' \
102 'The following menu list is organized as \n\' \
103 'keyboard \"layout/language\". \n\n\' \
104 'Select a keyboard map for the console:" 19 56 10 \' \
105 > $OUTPUT
107 # Append 'search' to the body
109 for item in $search
111 level="${item%/*}" # Remove all except the last slash.
112 level="${level##*/}" # Starting-point under which it was found.
113 item="${item%.map*}" # Remove extension(s).
114 item="${item##*/}" # Get the base name.
116 # Ignore unwanted map directories
117 case $level in
118 include | fgGIod)
119 continue
122 echo "\"${level}/$item\" \"\" \\" >> $OUTPUT
124 esac
125 done
126 unset item level \
127 search
129 # End of
130 printf "2> \$TMPFILE\n\n" >> $OUTPUT
132 # Import menu
133 . $OUTPUT
135 # Read answer from 'TMPFILE'
137 echo "" >> $TMPFILE; # Append new line for reading.
138 IFS= read -r REPLY < $TMPFILE || exit 2;
140 # Compose values by layout/map to be set
142 layout="${REPLY%/*}"
143 map="${REPLY##*/}"
145 unset REPLY
147 # The temporary files are not needed anymore
148 rm -f -- "$TMPFILE" "$OUTPUT"
150 umask 022; # Remove write permission for group and other.
152 # Set keyboard map
153 loadkeys "${layout}/$map"
155 while true
157 testkeys="$(
158 dialog --colors \
159 --no-mouse --no-cancel --no-ok \
160 --backtitle "\Zb${PROGRAM} - Keymap test" \
161 --title "TEST" --hline "${layout}/$map" \
162 --inputbox "Try the chosen keyboard map by typing something.\n\n\
163 Once satisfied type \"\Zb1\ZB\" (or \Zby\ZB) and press \ZbENTER\ZB. \n\
164 Type \"\Zb2\ZB\" (or \Zbn\ZB) plus \ZbENTER\ZB to choose a different
165 keyboard map." 12 56 2>&1 > /dev/tty
167 case $testkeys in
168 1 | Y | y)
169 break
171 2 | N | n)
172 rm -- "$LOCKFILE"
173 exec "$0"
175 esac
176 done
177 unset testkeys
179 dialog --colors \
180 --backtitle "\Zb${PROGRAM} - Configuration file" \
181 --title "SAVE" --yesno \
182 "Would do you like to save this change for the next reboot?." 7 54
184 dialog --clear
186 echo "Setting 'RC_KEYMAP=${layout}/$map' on /etc/rc.conf ..."
187 echo ",s/^\(RC_KEYMAP\)=.*/\1=${layout}\/${map}/"$'\nw' | \
188 ed -s /etc/rc.conf && echo Done.