#! /usr/pkg/bin/ksh93
# ##########################################################################
#
#   Xsession
#
#   Common Desktop Environment (CDE)
#
#   Configuration script for the Login Manager
#
#   (c) Copyright 1996 Digital Equipment Corporation.
#   (c) Copyright 1993,1994,1996 Hewlett-Packard Company.
#   (c) Copyright 1993,1994,1996 International Business Machines Corp.
#   (c) Copyright 1993,1994,1996 Sun Microsystems, Inc.
#   (c) Copyright 1993,1994,1996 Novell, Inc. 
#   (c) Copyright 1996 FUJITSU LIMITED.
#   (c) Copyright 1996 Hitachi.
#
#       ************** DO NOT EDIT THIS FILE **************
#
#   /usr/pkg/dt/bin/Xsession is a factory-default file and will
#   be unconditionally overwritten upon subsequent installation.
#   Modification is discouraged.
#
#   $TOG: Xsession.src /main/19 1999/03/30 09:38:50 mgreess $
#
#   Usage: $0 [-session session_name]
#
# ##########################################################################
# ##########################################################################
# 
# 
#   This script starts the user's session. It searches for one of three
#   types of startup mechanisms, in the following order:
# 
#     DT     existence of CDE DT Session Manager on the system
#     XDM    "$HOME/.xsession" (executable)
#     xinit  "$HOME/.xinitrc"  (executable)
# 
#   If none of these startup mechanisms exist, a default window manager
#   and terminal emulator client are started.
# 
# ##########################################################################
#
# Variables must be explicitly exported
# 
set +a
# ##########################################################################
# 
#   Initialize session startup logging
# 
# ##########################################################################
  exec >/dev/null 2>/dev/null
  LOGDIR=$HOME/.dt
  LOGFILENAME=$LOGDIR/startlog
  MSGLOGFILENAME=$LOGDIR/errorlog
  
  if [ ! -d $LOGDIR ]; then
    mkdir $LOGDIR
    if [ -d $LOGDIR ]; then
      chmod 755 $LOGDIR
    fi
  fi
  [ -f $LOGFILENAME.older ] && rm -f $LOGFILENAME.older
  [ -f $LOGFILENAME.old ] && mv -f $LOGFILENAME.old $LOGFILENAME.older
  [ -f $LOGFILENAME ] && mv -f $LOGFILENAME $LOGFILENAME.old
  touch $LOGFILENAME
  [ ! -f $MSGLOGFILENAME ] && touch $MSGLOGFILENAME
  if [ -w $LOGFILENAME ]; then
    exec >>$LOGFILENAME 2>&1
  fi
  Log()
  {
    echo "--- $1" >>$LOGFILENAME 2>&1
  }
  Log "$(date)"
  Log "$0 starting..."
# ##########################################################################
# 
#   Global environment section
# 
#    DT pre-sets the following environment variables for each user.
#    
#     (internal)
# 
#     DISPLAY      set to the value of the first field in the Xservers file.
#     HOME         set to the user's home directory (from /etc/passwd)
#     LANG         set to the display's current NLS language (if any)
#     LC_ALL       set to the value of $LANG
#     LOGNAME      set to the user name
#     PATH         set to the value of the Dtlogin "userPath" resource
#     USER         set to the user name
#     SHELL        set to the user's default shell (from /etc/passwd)
#     TZ           set to the value of the Dtlogin "timeZone" resource
# 
#
#     (Xsession)
# 
#     TERM         set to xterm
#     EDITOR       set to the default editor
#     KBD_LANG     set to the value of $LANG for certain languages
#     MAIL         set to "/var/mail/$USER"
# 
# 
#    Three methods are available to modify or add to this list depending 
#    on the desired scope of the resulting environment variable. 
#    
#     1. X server and/or all users on a display       (Xconfig file)
#     2. all users on a display                       (Xsession file)
#     3. individual users                             (.dtprofile file)
# 
#    See DT on-line help, the DT Users Guide, or the Dtlogin(1X) man
#    page for details on setting environment variables.
#     
#     
# ##########################################################################
  [ -z "$EDITOR" ] && EDITOR=/usr/pkg/dt/bin/dtpad
  [ -z "$MAIL" ] && MAIL="/var/mail/$USER"
  [ -z "$LANG" ] && LANG="C"
  TERM=dtterm
  SESSION_SVR=`hostname`
  export PATH EDITOR MAIL TERM SESSION_SVR LANG
 
  #
  # Locate configuration file directories
  #
  XDIR=/usr/X11R7/bin
  DT_BINPATH=/usr/pkg/dt/bin
  DT_INSTALL=/usr/pkg/dt
  DT_INSTALL_CONFIG=/usr/pkg/dt/config
  DT_CONFIG=/usr/pkg/usr/pkg/etc/dt/config
  DT_CONFIG_PATH="$DT_INSTALL_CONFIG $DT_CONFIG"
# ##########################################################################
#
#  Default desktop component configuration variable settings
#
#  This section sets the default value for variables controlling
#  some desktop components.
#
# ##########################################################################
  # 
  # Input method server startup
  # 
  if [ -z "$DTSTARTIMS" ]; then
     DTSTARTIMS=True
  fi
  if [ "$DTSTARTIMS" = "False" ]; then
     unset DTSTARTIMS
  fi
  # 
  # Default desktop screen saver action list 
  #  
  export DTSCREENSAVERLIST="StartDtscreenSwarm StartDtscreenQix     StartDtscreenFlame StartDtscreenHop StartDtscreenImage StartDtscreenLife     StartDtscreenRotor StartDtscreenPyro StartDtscreenWorm StartDtscreenBlank"
  # 
  # Session startup clients and args
  # 
  if [ "$SESSIONTYPE" = "altDt" ]; then
      dtstart_session[0]="$SDT_ALT_SESSION"
      dtstart_hello[0]="$SDT_ALT_HELLO"
  else
      DTSESSION_ARGS=""
      if [ $# -ge 2 ]; then
          if [ "$1" = "-session" ]; then
              DTSESSION_ARGS="$1 $2"
          fi
      fi
      dtstart_session[0]="$DT_BINPATH/dtsession $DTSESSION_ARGS"
      dtstart_hello[0]="$DT_BINPATH/dthello -file $DT_INSTALL/copyright &"
  fi
  dtstart_session[1]="$HOME/.xsession"
  dtstart_session[2]="$HOME/.xinitrc"
  dtstart_session[3]="$XDIR/xterm -geometry 80x24+10+10"
  dtstart_hello[1]="$XDIR/xsetroot -default &"
  dtstart_searchpath="$DT_BINPATH/dtsearchpath -ksh"
  dtstart_ttsession="$DT_BINPATH/ttsession -s"
  dtstart_dtdbcache="$DT_BINPATH/dtdbcache -init"
  #
  # dtdbcache file's directory should match
  # _DTDTSMMTEMPDIR in DtSvc/DtUtil1/DtsMM.h
  #
  dtdbcacherm="rm -f /tmp/dtdbcache_$DISPLAY"
  dtstart_appgather="$DT_BINPATH/dtappgather &"
  xdmstart_session[0]="$HOME/.xsession"
  xdmstart_session[1]="/etc/X11/xdm/Xsession"
  xdmstart_session[2]="xterm -geometry 80x24+10+10 -ls"
  xdmstart_hello="$XDIR/xsetroot -default &"
  SESSIONLOGDIR=$LOGDIR/sessionlogs
  SESSIONLOGFILENAME="$SESSIONLOGDIR/$SESSION_SVR"_DISPLAY=$DISPLAY
  if [ ! -d $SESSIONLOGDIR ]; then
    mkdir $SESSIONLOGDIR
    if [ -d $SESSIONLOGDIR ]; then
      chmod 755 $SESSIONLOGDIR
    fi
  fi
  touch $SESSIONLOGFILENAME
  if [ -w $SESSIONLOGFILENAME ]; then
      dtstart_sessionlogfile="$SESSIONLOGFILENAME"
  else
      dtstart_sessionlogfile="/dev/null"
  fi
  rm -f $SESSIONLOGFILENAME
  #
  # Determine Xsession parent
  #
  pexec=$(LC_TIME=C ps -p $PPID | awk 'NR==2 {print $5}')
  Log "Xsession started by $pexec"
# ##########################################################################
#  
# Append desktop font aliases to font path
#
# ##########################################################################
  if [ "${pexec##*/}" != "dtlogin" ]; then
    #
    # If Xsession launched by dtlogin, it is assumed that the desktop
    # font path has already been added by Xsetup, so no need to add it here.
    # 
    #
    # Append desktop font paths. Note: these directories should be
    # accessable by the X server. The file precedence is:
    #
    #   /usr/pkg/etc/dt/cde/fontaliases
    #   /usr/pkg/dt/share/cde/fontaliases
    #
    Log "setting font path..."
    if [ "$DTXSERVERLOCATION" != "remote" ]; then
      #
      # Since X server is local, optimize by checking local desktop
      # font directories and making one call to xset.
      #
      if [ -f /usr/pkg/etc/dt/cde/fontaliases/fonts.dir ] ||          [ -f /usr/pkg/etc/dt/cde/fontaliases/fonts.alias ]; then
        fontpath=/usr/pkg/etc/dt/cde/fontaliases
      fi
      if [ -f /usr/pkg/dt/share/cde/fontaliases/fonts.dir ] ||          [ -f /usr/pkg/dt/share/cde/fontaliases/fonts.alias ]; then
        if [ -z "$fontpath" ]; then
          fontpath=/usr/pkg/dt/share/cde/fontaliases
        else
          fontpath=$fontpath,/usr/pkg/dt/share/cde/fontaliases
        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.
      #
      $XDIR/xset fp+ /usr/pkg/etc/dt/cde/fontaliases 1>/dev/null
      $XDIR/xset fp+ /usr/pkg/dt/share/cde/fontaliases 1>/dev/null
    fi
  fi 
# ##########################################################################
# 
#   Source user's desktop profile
# 
#   This section determines if the user has a desktop profile in their
#   home directory. If not, the desktop default profile is copied to
#   the home directory. The desktop profile is then sourced. The purpose
#   is to incorporate any per-user/per-session environment customizations
#   and thereby propagate them to applications and desktop components.
# 
# ##########################################################################
  DTSYSPROFILE=sys.dtprofile
  DTPROFILE=.dtprofile
  if [ ! -f $HOME/$DTPROFILE ]; then
    for i in $DT_CONFIG_PATH
    do
      if [ -f $i/$DTSYSPROFILE ]; then
        /usr/bin/awk '
          BEGIN {printit=1}
          /SYSPROFILE COMMENT START/ {printit=0; next}
          /SYSPROFILE COMMENT END/ {printit=1; next}
          printit==1 {print}' <$i/$DTSYSPROFILE >$HOME/$DTPROFILE
        /bin/chmod 755 $HOME/$DTPROFILE
        break
      fi
    done
  fi
  #
  # source the .dtprofile.
  #
  if [ -f $HOME/$DTPROFILE ]; then
    dtprofile_errors=`/usr/pkg/bin/ksh93 -n $HOME/$DTPROFILE 2>&1`
    if [ "0" = "$?" ]; then
      Log "sourcing $HOME/$DTPROFILE..."
      . $HOME/$DTPROFILE
    else
      Log "not sourcing $HOME/$DTPROFILE due to errors..."
      Log "$dtprofile_errors"
    fi
  fi
# ##########################################################################
# 
#  External Xsession processing section
# 
#  This section searches the Xsession.d subdirectory and sources
#  the files contained therein.  The purpose is to set up any
#  per-user/per-session environment customizations and thereby propagate
#  them to applications and desktop components.
# 
# ##########################################################################
  DT_XSESSION_DIR=Xsession.d
  for i in $DT_CONFIG_PATH
  do
    if [[ -d $i/$DT_XSESSION_DIR ]]; then
      # 
      # Run custom Xsession scripts for this session.
      # 
      for SCRIPT in $(ls $i/$DT_XSESSION_DIR); do
      	if [ -x $i/$DT_XSESSION_DIR/$SCRIPT -a 	    \( ! -d $i/$DT_XSESSION_DIR/$SCRIPT \) ]; then
               Log "sourcing $i/$DT_XSESSION_DIR/$SCRIPT..."
               . $i/$DT_XSESSION_DIR/$SCRIPT
          fi
      done
    fi
  done
# ##########################################################################
# 
#   Startup section.
#      
#   Note: The ksh syntax ${parameter%% *} is used when appropriate to
# 	    remove any command line options that may have been included
# 	    in the definition of a DT executable below.
#      
# ##########################################################################
# 
# Return first command in array named by $1 that is executable
# 
GetFirst()
{
  let i=0
  while true; do
    eval "cmd=\${$1[$i]}"
    [ -z "$cmd" ] && break
    [ -x "${cmd%% *}" ] && echo "$cmd" && break
    Log "could not start $cmd"
    let i=$i+1
  done
}
# 
# Start first command in array named by $1 that is executable. If
# $2 is 'eval', command result will be 'eval'ed. 
# 
StartFirst()
{
  first=$(GetFirst $1)
  if [ ! -z "$first" ]; then
    Log "starting $first"
    if [ "$2" = "eval" ]; then
      eval `eval "PATH=$DT_BINPATH:$PATH $first"`
    else
      eval "PATH=$DT_BINPATH:$PATH $first"
    fi
  fi
}
  # 
  # Prepare for session startup
  # 
  if [ "$DTSOURCEPROFILE" = "true" ]
  then
    case ${SHELL##*/} in
       sh | ksh | ksh93 | dtksh) shellprofile="$HOME/.profile";;
       bash) shellprofile="$HOME/.bash_profile";;
       csh | tcsh) shellprofile="$HOME/.login";;
       *) Log "non-standard shell $SHELL"
    esac
  fi
  if [ "$shellprofile" -a ! -f "$shellprofile" ]
  then
    Log "could not read $shellprofile"
    unset shellprofile
  fi
  if [ "$SESSIONTYPE" = "xdm" ]; then
    startup=$(GetFirst xdmstart_session)    # get xdm session client
    StartFirst xdmstart_hello               # start xdm hello client
  else
    startup=$(GetFirst dtstart_session)     # get desktop session client
    StartFirst dtstart_hello                # start desktop hello client
    StartFirst dtstart_searchpath eval      # setup desktop search paths
    tooltalk=$(GetFirst dtstart_ttsession)  # get tooltalk client
    dtdbcache=$(GetFirst dtstart_dtdbcache) # get dtdbcache client
    StartFirst dtstart_appgather            # setup session applications
  fi
  # 
  # Start the session.
  # 
  if [ $shellprofile ]; then
    Log "execing $startup using $shellprofile..."
    case ${SHELL##*/} in
      sh | bash | ksh | ksh93 | dtksh)
        dotprofile_errors=`$SHELL -n $shellprofile 2>&1`
        if [ "0" = "$?" ]; then
          source_profile=". $shellprofile"
          source_login="source $shellprofile"
        else
          Log "parse errors found in $shellprofile..."
          Log "$dotprofile_errors"
          source_profile="echo 'not execing $shellprofile due to errors'"
          source_login="echo 'not execing $shellprofile due to errors'"
        fi;;
           
      *)
        source_profile=". $shellprofile"
        source_login="source $shellprofile";; 
    esac
  else
    Log "execing $startup..."
    source_profile="echo 'not execing $HOME/.profile (see $HOME/.dtprofile)'"
    source_login="echo 'not execing $HOME/.login (see $HOME/.dtprofile)'"
  fi
  if [ -z "$dtdbcache" ]; then
	dtdbcache="echo could not start $dtstart_dtdbcache"
  fi
  export DT=true;
  case ${SHELL##*/} in
      sh | bash) $SHELL -c "$source_profile; 			   unset DT; 			   $dtdbcache;                            PATH=$DT_BINPATH:\$PATH $tooltalk;                              $startup > $dtstart_sessionlogfile 2>&1" ;;
      ksh | ksh93 | dtksh) $SHELL -c "$source_profile; 			   unset DT; 			   $dtdbcache;                            PATH=$DT_BINPATH:\$PATH $tooltalk;                           $startup >| $dtstart_sessionlogfile 2>&1" ;;
      csh | tcsh) $SHELL -c "unsetenv _ PWD;                                  $source_login;                            unsetenv DT;          			   $dtdbcache;                            (set path = ( $DT_BINPATH \$path ); $tooltalk );                            $startup  >&! $dtstart_sessionlogfile" ;;
      *) unset DT
	 $dtdbcache
         StartFirst dtstart_ttsession
         $startup >| $dtstart_sessionlogfile 2>&1 ;;
  esac
$dtdbcacherm		# remove the actions/datatypes cachefile
# ####################         eof      #################################
