#!/bin/csh -f

# This is a C shell script that switches X terminal keyboards from
# qwerty to Dvorak and back.  It assumes that your DISPLAY environment
# variable is set correctly.  The script reads the keymap from the
# X terminal, remaps the keys using awk, and then sends the modified
# map back to the terminal.  It also sets a resource on the terminal
# to indicate what the current mapping is. 
#
# Copyright 1998, Larry D. Pyeatt
#
# set the awkscriptpath variable to the directory where toqwerty.awk and
# todvorak.awk can be found
set awkscriptpath = /usr/local/bin

set keyboard = `xrdb -query | grep keyboard.mapping | awk '{RS=":"}{print $2}'`

switch ($1)
case qwerty:
        if( $keyboard == "dvorak") then
	    xmodmap -pke > tempmap
	    awk -f $awkscriptpath/toqwerty.awk tempmap > newmap
	    xmodmap newmap
	    rm tempmap
	    rm newmap
	    echo "keyboard.mapping: qwerty" | xrdb -merge -
	    echo Changing keyboard to qwerty.
	else
            echo Keyboard is already qwerty.
	endif
	breaksw
case dvorak:
        if($keyboard == "dvorak")  then
            echo Keyboard is already dvorak.
	else
	    xmodmap -pke > tempmap
	    awk -f $awkscriptpath/todvorak.awk tempmap > newmap
	    xmodmap newmap
	    rm tempmap
	    rm newmap
	    echo "keyboard.mapping: dvorak" | xrdb -merge -
	    echo Changing keyboard to dvorak.
	    endif
	breaksw
default
	echo Usage: "keymap dvorak" or "keymap qwerty"
	exit
endsw

