#!/bin/csh
#*****************************************************************************
# E.S.O. - VLT project
#
# "@(#) $Id: Xsession,v 1.160 1999/04/14 09:29:33 vltsccm Exp $" 
#
# Xsession (.xsession)
#
# who         when      what
# ---------   --------  ----------------------------------------------
# G.FILIPPI   18/08/93  created 
# G.FILIPPI   13/09/93  xrdb preloaded with xterm, xedit, xman app-def.
# G.FILIPPI   03/11/93  te16 added to xhost list
# G.FILIPPI   09/12/93  te1  added to xhost list
# G.FILIPPI   04/02/94  te27 added to xhost list
# P.FORSTMANN 30/08/94  Standardized xhost list
# P.FORSTMANN 04/11/94  Added alt key handling
# -1.6-------
# P.FORSTMANN 23/02/95  Deleted xhost

#-----------------------------------------------------------------------
# CREDITS: 
#     T.MELEN and C.OUNNAS for the previous .xsession
#
#-----------------------------------------------------------------------

#************************************************************************
#   NAME
#   Xsession, xsession  -  X startup file at login time.
#
#   SYNOPSIS
#   $HOME/.xsession 
#   $HOME/.xsession -> /vlt/System/Xsession
#
#   DESCRIPTION
#
#   This is the standard file that xdm look for at login time.
#
#   It does the following actions:
#     - set the DISPLAY variable
#     - if not already existent, create standard login/logout files
#     - load the user resource file $HOME/.Xresources into xrdb
#     - create the Home Session, normally the window manager and some 
#       clients by invoking the user $HOME/.xclients or the default
#       /vlt/System/Xclients. 
#  
#   You have two possibilities:
#
#     - using the default script by creating a link to it:
#          $ ln -s /vlt/System/Xsession $HOME/.xsession
#
#     - create your own startup script.
#       Make a local copy of the the standard file
#              $ cp /vlt/System/Xsession $HOME/.xsession
#       and edit it according to your needs. 
#
#   This file is meaningfull only on "xdm" environment, at present used only
#   on SUN Solaris 1. 
#
#   On HP-VUE does not use this file.
#
#   Options:
#
#     -display <DisplayUnit>      Display unit, see X(1).
#
#
#   FILES
#   $HOME/.myhost$$    temporary file. Normally deleted atthe end. An abnormal
#                      end of the window manager can leave the file in your
#                      HOME directory. Clean it from them periodically.
#   $HOME/.xsession    startup session script searched by xdm
#   /vlt/System/.xsession 
#                      VLT default startup session script
#   /usr/lib/X11/xdm/Xsession 
#                      xdm startup file. It executes $HOME/.xsession, 
#                      if available. or provides a default screen setup.
#
#   ENVIRONMENT
#
#   RETURN VALUES
#   
#   0 on success, 1 on error.
#    
#   CAUTIONS
#   $HOME/.xclients is not automatically read by X. Is up to 
#   .xsession to execute it. If want to use $HOME/.xclients, or the 
#   default /vlt/System/Xclients and you have a local copy, be sure 
#   that your .session is reading  $HOME/.xclients or 
#   /vlt/System/Xclients.
#
#   An abnormal end of the window manager leaves a $HOME/.myhost$$ file
#   in your HOME directory. Periodically, lean it from this type of file.
#
#   EXAMPLES
#
#   SEE ALSO
#   X(1)
#   O'Reilly & Ass. - The X Window System - vol.3 - User's Guide
#   
#   BUGS     
#
#   xhost is still used. It should be substitute by xauth
#
#------------------------------------------------------------------------

#------------------------------------------------------------------------
#
# Set up interupt handler
#
onintr InteruptHandler

#------------------------------------------------------------------------
# Check input parameters
#
while ( $#argv )
	switch ( $1 )
	case "-display":
		setenv DISPLAY $2
		shift; shift
		breaksw
	default:
		echo2 "$0: Unknown option $1"
		exit 1
	endsw
end

#------------------------------------------------------------------------
#
# Check that the DISPLAY enviroment variable is defined and export it
#
if ( ! $?DISPLAY ) then
	echo2 "$0: DISPLAY enviroment variable not defined"
	exit 1
endif

#------------------------------------------------------------------------
#
# Check that users home directory is available
#
if ( "$HOME" != "/" || "$USER" == "root" ) set homeOK = true
#

#------------------------------------------------------------------------
# Provide a default environment if nothing is present or not accessible:
# The provided files are:
#       .cshrc 
#       .login
#       .logout
#       .xclients 
#       .mwmrc

if ( $?homeOK && ! -r $HOME/.cshrc ) then
    rm -f $HOME/.cshrc
    ln -s /vlt/System/Cshrc $HOME/.cshrc
endif

if ( $?homeOK && ! -r $HOME/.login ) then
    rm -f $HOME/.login
    ln -s /vlt/System/Login $HOME/.login
endif

if ( $?homeOK && ! -r $HOME/.logout ) then
    rm -f $HOME/.logout
    ln -s /vlt/System/Logout $HOME/.logout
endif

if ( $?homeOK && ! -x $HOME/.xclients ) then
    rm -f $HOME/.xclients
    ln -s /vlt/System/Xclients $HOME/.xclients
endif

if ( $?homeOK && ! -x $HOME/.mwmrc ) then
    rm -f $HOME/.mwmrc
    ln -s /vlt/System/Mwmrc $HOME/.mwmrc
endif

#------------------------------------------------------------------------
# create environment variables. (.login is not execute because it can 
# contain commands that are not meaningful in a non interactive context.)
#
source $HOME/.cshrc

#
# Define $HOST and $HOME_SERVER if not already done
if ( ! $?HOST )		setenv HOST `hostname`
if ( ! $?HOME_SERVER )	setenv HOME_SERVER $HOME

#
# Prepend /usr/bin/X11 to the path if it's not alredy there
echo $PATH | grep /usr/bin/X11 > /dev/null
if ( $status ) then
    setenv PATH /usr/bin/X11:$PATH
    echo2 "WARNING $0 - /usr/bin/X11 was not in the PATH, prepended"
endif



#------------------------------------------------------------------------
#
# Let display be the network equivalent of DISPLAY
switch ( $DISPLAY )
	case unix\:?.?:
		set display = `echo $DISPLAY | sed -e "s/^unix:/"$HOST":/"`
		breaksw
	case unix\:?:
		set display = `echo $DISPLAY | sed -e "s/^unix:/"$HOST":/"`.0
		breaksw
	case \:?.?:
		set display = `echo $DISPLAY | sed -e "s/^:/"$HOST":/"`
		breaksw
	case \:?:
		set display = `echo $DISPLAY | sed -e "s/^:/"$HOST":/"`.0
		breaksw
	case *\:?.?:
		set display = $DISPLAY
		breaksw
	case *\:?:
		set display = ${DISPLAY}.0
		breaksw
	default:
		echo2 "$0: Unable to handle display name '$dispname'"
		exit 1
endsw
#
# Set DISPLAY to its network equivalence
#
setenv DISPLAY $display

#
# Define console if screen is on the console (this test is far from perfect!)
#
switch ( "$DISPLAY" )
case 'unix:?.?':	
case 'unix:?':	
case ':?.?':	
case ':?':	
	setenv console -C
endsw

#
# Make this host the default for applications started by the window manager
# the host name is passed using $MYHOST file 
setenv MYHOST $HOME/.myhost$$
myhost $HOST

#
# Alt key handling

xmodmap -e 'remove mod1 = Mode_switch'
xmodmap -e 'add mod1 = Alt_R'
xmodmap -e 'add mod1 = Alt_L'
xmodmap -e 'keysym Alt_R = Alt_R Meta_R'

#-----------------------------------------------------------------------
# Initialize X-resource data base

#
# Logout resources (I have to to this because I have not find how to 
# pass this resources via the normal site application default mechanism)
xrdb -load /vlt/System/X11/app-defaults/XLogout

# The following should be done also on VUE environemnt (See Vueprofile).
#
# I initialize the xrdb with some site appl-defaults resource file.
#   This provides that application like xterm, etc that are normally
#   started also on other systems, to have the same appearance as 
#   when they are started from a computer with the Standard Environment.
#
xrdb -merge /vlt/System/X11/app-defaults/XTerm
xrdb -merge /vlt/System/X11/app-defaults/Xedit
xrdb -merge /vlt/System/X11/app-defaults/Xman

#
# Add the user default resource file. if any
if ( $?homeOK && -r $HOME/.Xresources ) xrdb -merge $HOME/.Xresources

#-----------------------------------------------------------------------
# Start home session and wait .... 

$HOME/.xclients

#
# ..... the control resume here when the logout client, normally "xlogout",
#       is terminated (see /vlt/System/Xclients) 

#-----------------------------------------------------------------------
# Clean-up and exit
#

#
# Delete the file holding the default host
rm -f $MYHOST

#
# logout
if ( -r $HOME/.logout ) source $HOME/.logout

#
# Normal termination
exit 0


#-----------------------------------------------------------------------
#
InteruptHandler:

	echo2 "$0: Interupt received, terminating"

	#
	# Delete the file holding the default host
	rm -f $MYHOST

	#
	# logout
	if ( -r $HOME/.logout ) source $HOME/.logout

	exit 1

#-----------------------------------------------------------------------

#___oOo___
