caps-ctrl: use backticks, drop colors module.
[grml-scripts.git] / usr_bin / caps-ctrl
1 #!/bin/zsh
2 # Filename:      caps-ctrl
3 # Purpose:       switch caps to control key and vice versa for linux console and X
4 # Authors:       grml-team (grml.org),  (c) Matthias Kopfermann <maddi@grml.org>
5 # Bug-Reports:   see http://grml.org/bugs/
6 # License:       This file is licensed under the GPL v2.
7 ################################################################################
8
9 if . /etc/grml/script-functions ; then
10    check4progs xmodmap loadkeys dumpkeys || exit 1
11 fi
12 . /etc/grml/lsb-functions || ( alias einfo=echo ; alias eerror=echo; alias eend=echo )
13
14 emulate zsh
15
16 if [[ -z $DISPLAY  ]] ; then # test if X is not running when calling us
17          if [ $(id -u) != 0 ] ; then # test if user root did invoke this command
18             eerror "As of Linux 2.6.15 you need root permissions for changing"
19             eerror "the keyboard on console using loadkeys for security reasons."
20             eerror "Run this program with root permissions. Exiting." ; eend 1
21             exit 1
22          fi
23          dumpkeys | grep -q '^keycode  58 = Caps_Lock' && \
24          ( einfo "caps-ctrl - switching caps lock and control key."
25
26            loadkeys <<- EOT
27            keycode 58 = $(repeat 15 echo -n 'Control ')
28            keycode 29 = $(repeat 7 echo -n 'Caps_Lock ')
29                 EOT
30            eend $?
31
32         ) || (
33           einfo "caps-ctrl - switching caps lock and control key."
34
35            loadkeys <<- EOT
36            keycode 58 = $(repeat 15 echo -n 'Caps_Lock ')
37            keycode 29 = $(repeat 7 echo -n 'Control ')
38                 EOT
39            eend $?
40          )
41
42 else     # running under X
43         (
44         einfo "caps-ctrl - switching caps lock and control key."
45         einfo "If you notice errors, please make sure the xmodmap you have is right"
46         einfo "or use e.g. \"setxkbmap us\" beforehand."
47         xmodmap -pke | grep 'Caps_Lock' > /dev/null || (
48         xmodmap - <<- EOT
49         keycode 66 = Caps_Lock
50                 EOT
51         )
52
53         xmodmap - <<- EOT
54         remove Lock = Caps_Lock
55         remove Control = Control_L
56         !remove Control = Control_R
57
58         keysym Control_L = Caps_Lock
59         keysym Caps_Lock = Control_L
60         !keysym Control_R = Caps_Lock
61         !keysym Caps_Lock = Control_R
62
63         add lock = Caps_Lock
64         add Control = Control_L
65         !add Control = Control_R
66                 EOT
67         eend $?
68         )
69 fi # end of test if X or console is used
70
71 ## END OF FILE #################################################################