#!/bin/ksh
# ###########################################################################
#
#  Common Desktop Environment
#
#  (c) Copyright 1993, 1994 Hewlett-Packard Company
#  (c) Copyright 1993, 1994 International Business Machines Corp.
#  (c) Copyright 1993, 1994 Sun Microsystems, Inc.
#  (c) Copyright 1993, 1994 Novell, Inc.
#
#      ************** DO NOT EDIT THIS FILE **************
#
#  /usr/dt/config/Xsetup is a factory-default file and will
#  be unconditionally overwritten upon subsequent installation.
#  Before making changes to the file, copy it to the configuration 
#  directory, /etc/dt/config. You must also update the setup
#  resource in /etc/dt/config/Xconfig.
#
# ###########################################################################
#
# List of displays and keyboard languages
#
# This list is used to determine the keyboard language mapping for a
# particular X station. Each item in the list is a display name
# followed by a locale name to use for that display's keyboard
# language mapping. To add a new mapping, simply add a new item to
# the list. For example, if you have displays named chili:0, carp:0
# and pecos:0 with japanese, french and german keyboards, respectively,
# your KBD_MAP list would be:
#
# KBD_MAP[0]="chili:0     Ja_JP"
# KBD_MAP[1]="carp:0      Fr_FR"
# KBD_MAP[2]="pecos:0     Gr_GR"
#
#
KBD_MAP[0]=""

# ###########################################################################
# 
# Directory specifications
# 
# ###########################################################################

#
# Wait for boot sequence to end (if booting)
#
if [ "$LOCATION" != "remote" ]; then
  /usr/sbin/rc.bootx
fi


  XDIR=/usr/bin/X11

# @(#)72	1.8.1.1  src/cde/cde1/dtlogin/config/_common.ksh.src, desktop, cde720 7/14/11 18:52:19



# ###########################################################################
#  
# Append desktop font aliases to font path
#
# ###########################################################################


    #
    # Append desktop font paths. Note: these directories should be
    # accessable by the X server. The file precedence is:
    #
    #   /etc/dt/config/xfonts/$LANG
    #   /etc/dt/config/xfonts/C
    #   /usr/dt/config/xfonts/$LANG
    #   /usr/dt/config/xfonts/C
    #
    if [ "$DTXSERVERLOCATION" != "remote" -a -z "$XSTATION" ]; then
      #
      # Since X server is local, optimize by checking local desktop
      # font directories and making one call to xset.
      #
      if [ "$LANG" != "C" ]; then
        if [ -f /etc/dt/config/xfonts/$LANG/fonts.dir ]; then
          fontpath=/etc/dt/config/xfonts/$LANG
        fi
      fi

      if [ -f /etc/dt/config/xfonts/C/fonts.dir ]; then
        if [ -z "$fontpath" ]; then
          fontpath=/etc/dt/config/xfonts/C
        else
          fontpath=$fontpath,/etc/dt/config/xfonts/C
        fi
      fi

      if [ "$LANG" != "C" ]; then
        if [ -f /usr/dt/config/xfonts/$LANG/fonts.dir ]; then
          if [ -z "$fontpath" ]; then
            fontpath=/usr/dt/config/xfonts/$LANG
          else
            fontpath=$fontpath,/usr/dt/config/xfonts/$LANG
          fi
        fi
      fi

      if [ -f /usr/dt/config/xfonts/C/fonts.dir ]; then
        if [ -z "$fontpath" ]; then
          fontpath=/usr/dt/config/xfonts/C
        else
          fontpath=$fontpath,/usr/dt/config/xfonts/C
        fi
      fi

      if [ ! -z "$fontpath" ]; then
        $XDIR/xset +fp $fontpath
      fi

    else
      #
      # Since X server not local, we don't know if the desktop font
      # directories exist on the X server machine, so we have to
      # set them one at a time.
      #
      if [ "$LANG" != "C" ]; then 
        if [ -d /etc/dt/config/xfonts/$LANG ]; then
          $XDIR/xset +fp /etc/dt/config/xfonts/$LANG 1>/dev/null
        fi
      fi
      if [ -d /etc/dt/config/xfonts/C ]; then
        $XDIR/xset +fp /etc/dt/config/xfonts/C 1>/dev/null
      fi
      if [ "$LANG" != "C" ]; then
        if [ -d /usr/dt/config/xfonts/$LANG ]; then
          $XDIR/xset +fp /usr/dt/config/xfonts/$LANG 1>/dev/null
        fi
      fi
      if [ -d /usr/dt/config/xfonts/C ]; then
        $XDIR/xset +fp /usr/dt/config/xfonts/C 1>/dev/null
      fi
    fi

# ###########################################################################
#
#  Setup keyboard mapping for local display if required
#
# ###########################################################################

SetKeyboardMap()
{
  # $1 = language name


  KBD_LIST=""
  KBD_LIST="$KBD_LIST $IMKEYMAPPATH/$1/keyboard"
  if [ "$IMKEYMAPPATH" = "/usr/lib/nls/im.alt" ]; then
    KBD_LIST="$KBD_LIST $XMODDIR/${1}@alt/keyboard"
  fi
  KBD_LIST="$KBD_LIST $XMODDIR/$1/keyboard"

  for i in $KBD_LIST; do
    if [ -r $i ]; then
      $XDIR/xmodmap $i
      return 0
    fi
  done

  return 1
}


# ##############################################################
# Look to see if the X property _XKB_RULES_NAMES exists.
# If it does, the X server has already set the keyboard mapping.
# ##############################################################
/usr/bin/X11/xprop -root | /usr/bin/grep -q "_XKB_RULES_NAMES"
if [ "$?" != "0" ]
then

    

    XMODDIR=/usr/lpp/X11/defaults/xmodmap
    #
    # Determine keyboard language
    #
    KBD_LANG=""
    if [ "$DTXSERVERLOCATION" = "local" ]; then
      #
      # Local display so query for keyboard map
      #
      # Check for long keyboard name
      LONG_KBD_LANG=`/usr/lpp/X11/bin/querykbd`
      if [ -r $XMODDIR/$LONG_KBD_LANG/keyboard ];
      then
          KBD_LANG=$LONG_KBD_LANG
      else
          KBD_LANG=`/usr/lpp/X11/bin/querykbd | cut -f1 -d'.'`
      fi
    else
      #
      # Non-local display so use KEY_MAP list
      #
      let i=0
      while true; do
        [ -z "${KBD_MAP[i]}" ] && break
        if [ "$DISPLAY" = "${KBD_MAP[i]%% *}" ]; then
          KBD_LANG=${KBD_MAP[i]##* }
          break
        fi
        let i=$i+1
      done
    fi

    #
    # Set up keyboard mapping
    # 
    KBD=""

    if [ ! -z "$KBD_LANG" ]; then
      if [ "$KBD_LANG" != "NULL" -a "$KBD_LANG" != "C" ]; then
        SetKeyboardMap $KBD_LANG
        if [ $? != 0 ]; then
          SetKeyboardMap $LANG
        fi
      else
        SetKeyboardMap $LANG
      fi
    fi
    
fi
