#!/bin/sh
#
#	NAME
#		XPkeymap
#
#	SYNOPSIS
#		XPkeymap [-display DISPLAY_NAME] [-xdm] [-debug] [-r]
#
#		Examples:
#		    $TEKXP_ROOT/bin/XPkeymap &
#		    $TEKXP_ROOT/bin/XPkeymap -display myXP:0.0 &
#		 (to reset mappings when the keyboard selection is changed:)
#		    $TEKXP_ROOT/bin/XPkeymap -r &
#
#	DESCRIPTION
# This script loads system, site, and user keymap files: the system files to 
# initialize compose sequences and diacriticals, and the user $HOME/.Xkm file.
# Currently, compose sequences are supported only for European
# VT200 keyboards set to X-style layouts (x_esc and x_f11),
# as DEC Xlib supports compose sequences internally.  The script 
# obtains the current keyboard language and type from the TekXpress server.
# It aborts if this is not a TekXpress display.
# If the script is run on an unsupported host for which the Tek binary 
# utilities are not available, or if Tek compose is already initialized 
# and the -r option is not given, only the user and keyboard-independent 
# site keymap files are loaded.
#
#	OPTIONS
#	    -display
#		Specifies the display, overriding $DISPLAY.
#	    -xdm
#		Ignored so XPstartup can blindly pass its args.
#	    -debug
#		Turns on script trace (set -x).
#	    -r
#		Forces a reset and reinitialization of any existing compose
#		definition.  Normally, if a compose definition already exists,
#		it is not reset and the system and keyboard-specific site files
#		are not read.  This option should be given when the keyboard
#		selection is changed.
#
#	ENVIRONMENT VARIABLES
#	    DISPLAY
#	        points to the TekXpress display.  Overridden by the
#	        -display argument. 
#	    TEKXP_ROOT
#	        contains the path to the XP directory tree that
#	        contains the following files.  Default is
#	        "/usr/lib/X11/XP". 
#	    XP_ROOT
#	        Old equivalent of TEKXP_ROOT used in V3 XPstartup;
#	        "/usr/lib/X11".  Overridden by TEKXP_ROOT. 
#	    TEKXP_BIN
#	        Points to a site-defined directory for TEKXP programs,
#	        so that one file server can support sessions on multiple
#		types of hosts.  If not defined, $TEKXP_ROOT/bin is used.
#
#	FILES
#	    $TEKXP_ROOT/config/$kbd_type/*/Xkm*
#	    USER_XKM=$HOME/.Xkm
#
# Process args
#
while [ $# != 0 ]
do
    case "$1"
    in
        -debug)
            set -x
	    debug=true
            shift
	    ;;
        -xdm)
	    # ignore XPstartup -xdm arg
            shift
	    ;;
        -r)
            reset=true
            shift
	    ;;
        -display)
	    #Reset the DISPLAY environment variable
	    DISPLAY=$2
	    export DISPLAY
	    shift;shift
	    ;;
        *)
            echo >&2 "$0: invalid option: $1"
            echo >&2 "usage: $0 [-display DISPLAY_NAME] [-xdm] [-debug]"
            exit 1
            ;;
    esac
done

USER_XKM=$HOME/.Xkm
if [ ! "$TEKXP_ROOT" ]
then
    if [ "$XP_ROOT" ]
    then
    	# XP_ROOT is the old variable set to /usr/lib/X11.
    	TEKXP_ROOT=$XP_ROOT/XP
    else
	TEKXP_ROOT=/usr/lib/X11/XP
    fi
fi
#
# The system administrator or user may add $TEKXP_ROOT/bin to the their path
# so that they can access xmodmap and xsetprop.  If they do not add this
# directory, XPstartup will add it to the beginning of their path.  However, 
# the scripts do not assume that the user's path includes the XP bin directory;
# XPkeymap looks for the environment variable TEKXP_BIN which points to a
# site-defined directory for TEKXP programs, else uses the common bin
# directory configured during V4 tape install.  The TEKXP_BIN variable
# allows using one XP tree with multiple host binaries on a file server in a
# heterogeneous network.  If the xsetprop utility cannot be found in the 
# Tekxp bin directory, we fall back to the user's path, assume that the
# xsetprop and Tek xmodmap utilities are not available, and skip the system and
# keyboard-dependent site keymap file loading.
#
if [ ! "$TEKXP_BIN" ]
then
    TEKXP_BIN=$TEKXP_ROOT/bin
fi

/bin/rm -f /tmp/Xkm.$$

if [ -r $TEKXP_BIN/xsetprop ]
then
    if $TEKXP_BIN/xsetprop -e KB_COMPOSE_TABLE_SHORTS > /dev/null 2>&1
    then
	exists=true
    fi
    if [ "$exists" = "" -o "$reset" != "" ]
    then

    if kbd_type=`$TEKXP_BIN/xsetprop -p _TEK_KEYBOARD_TYPE 2> /dev/null`
    then
	:
    else
	# this is not a TekXpress display: abort.
	exit 0
    fi
    if kbd_lang=`$TEKXP_BIN/xsetprop -p _TEK_KEYBOARD_NATIONALITY 2> /dev/null`
    then
	# not set by V3
	kbd_layout=`$TEKXP_BIN/xsetprop -p _TEK_KEYBOARD_LAYOUT 2> /dev/null`
	kbd_mode=`$TEKXP_BIN/xsetprop -p _TEK_KEYBOARD_MODE 2> /dev/null`
    else
	# work with pre-V4 firmware that used _TEK_LANGUAGE
	kbd_lang=`$TEKXP_BIN/xsetprop`
    fi

    # we can load system/site keymappings.  First, reset any existing compose.
    if [ "$exists" != "" ]
    then
	$TEKXP_BIN/xmodmap -nocompose > /dev/null 2>&1
    fi

    # read xmodmap files.  Do not read Tek Compose files if set to DEC layouts
    # (no Multi_key keysym, use DEC compose support).  We don't have mapping 
    # files for ibm keyboards yet, but allow them.
    if [ "$kbd_lang" != "" -a \
      \( "$kbd_type" != vt200 -o \
      \( "$kbd_layout" != vms -a "$kbd_layout" != ultrix \) \) ]
    then
    # Succeeds for all vt200 layouts with V3 (no kbd_layout prop).
    if [ "$kbd_lang" != usascii ]
    then
	cat -s $TEKXP_ROOT/config/$kbd_type/*Compose/Xkm >/tmp/Xkm.$$ 2>/dev/null
	# Tek- and/or site-supplied language-independent keymappings for 
	# keyboards other than North American
    fi

    # language-specific compose files must be read after the fake
    # compose keysyms are assigned.
    # The zzTekCompose/Xkm file does an "execute" command at the end so that
    # the fake keysyms can be used to define compose sequences.
    # System Administrators may add Compose sequences by adding files such as
    # $TEKXP_ROOT/config/$kbd_type/localCompose/Xkm	and
    # $TEKXP_ROOT/config/$kbd_type/localCompose/Xkm.$kbd_lang
    # that will be processed first.
    
    # On some system V host Xkm.swiss-german is too long of a name.
    if [ "$kbd_lang" = swiss-german ]
    then
	cat -s $TEKXP_ROOT/config/$kbd_type/*/Xkm.swiss-gr >>/tmp/Xkm.$$ 2>/dev/null
    else
        cat -s $TEKXP_ROOT/config/$kbd_type/*/Xkm.$kbd_lang >>/tmp/Xkm.$$ 2>/dev/null
    fi

    # There are a few sequences that are not in effect in DP mode.
    if [ "$kbd_mode" = "" ]
    then
    # On some system V host Xkm.swiss-german is too long of a name.
        if [ "$kbd_lang" = swiss-german ]
        then
	    cat -s $TEKXP_ROOT/config/$kbd_type/zzTekIntl/nodp.swiss-gr >>/tmp/Xkm.$$ 2>/dev/null
	else
	    cat -s $TEKXP_ROOT/config/$kbd_type/zzTekIntl/nodp.$kbd_lang >>/tmp/Xkm.$$ 2>/dev/null
    fi

    fi
    # Tek- and/or site-supplied language-specific compose sequences

    if [ "$kbd_lang" != usascii ]
    then
	cat -s $TEKXP_ROOT/config/$kbd_type/*compose/Xkm >>/tmp/Xkm.$$ 2>/dev/null
	# Tek- and/or site-supplied language-independent compose sequences
    fi
    fi    
    fi    
else
    # No Tek utilities - assume the rest can be loaded with a host-supplied 
    # xmodmap in the PATH.
    TEKXP_BIN=
fi
cat -s $TEKXP_ROOT/config/*/Xkm >>/tmp/Xkm.$$ 2>/dev/null
# Site-supplied keyboard type- and language-independent keymappings

# for debugging, print system xmodmap errors.
if [ "$debug" ]
then
	$TEKXP_BIN/xmodmap /tmp/Xkm.$$
	/bin/rm -f /tmp/Xkm.*.dbg
	mv /tmp/Xkm.$$ /tmp/Xkm.$$.dbg
else
	$TEKXP_BIN/xmodmap /tmp/Xkm.$$ > /dev/null 2>&1
fi
# Suppress the usual xmodmap message.  It would be nice to check the return
# status, and caution the user to make sure he is using a Tektronix xmodmap, 
# but xmodmap is not returning valid error status.
/bin/rm -f /tmp/Xkm.$$

# Append the user's keymappings.

if [ -r $USER_XKM ]
then
	$TEKXP_BIN/xmodmap $USER_XKM
fi
