#!/bin/sh

# $Xorg: startx.cpp,v 1.3 2000/08/17 19:54:29 cpqbld Exp $
#
# This is just a sample implementation of a slightly less primitive
# interface than xinit.  It looks for user .xinitrc and .xserverrc
# files, then system xinitrc and xserverrc files, else lets xinit choose
# its default.  The system xinitrc should probably do things like check
# for .Xresources files and merge them in, startup up a window manager,
# and pop a clock and serveral xterms.
#
# Site administrators are STRONGLY urged to write nicer versions.
#
# $XFree86: xc/programs/xinit/startx.cpp,v 3.16tsi Exp $

# Rewritten for more reliable argument processing
# (c) 1999  John Latham, Dept. Computer Science, University Of Manchester, UK.
# email: jtl@cs.man.ac.uk

# This fixes certain bugs, e.g. if user has .xserverrc but still supplies the
# server as an argument. Same for client. Red Hat 6.2 startx looked like it was
# trying to fix this for the server, but didn't! Red hat 7.1 has reverted.

# Automatic choice of next available display by default added by John Latham
# on 19 October 1999.

# Minor fix 21 September 2000.

# August 2001: Merged in Red Hat 7.1 startx version -- see opening XConsortium
# comments above.

# August 2002: Put single quotes around server and client options, and start
# initx with an eval, so that quoted arguments containing spaces should work...
# These got missed out of /usr/X11R6/bin/startx, and were only added to
# CS dotfiles /usr/local/etc/bash_profiles/startx.linux !!
# Added to /usr/X11R6/bin/startx August 2004.

# October 2002: Merged in Red Hat 7.3 startx version -- see opening XConsortium
# comments above.

# August 2004: Merged in Fedora Core 2 startx version.

# November 2009: added unset of XDG_SESSION_COOKIE etc..

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

# November 2009: added these to make sound work!
unset XDG_SESSION_COOKIE
unset DBUS_SESSION_BUS_ADDRESS
unset SESSION_MANAGER

#The name of the user's default client; if it exists
userclientrc=$HOME/.xinitrc

#The name of the user's default server; if it exists
userserverrc=$HOME/.xserverrc

#The name of the system's default client; if it exists
sysclientrc=/etc/X11/xinit/xinitrc

#The name of the system's default server; if it exists
sysserverrc=/etc/X11/xinit/xserverrc

ClientOptions=""
ServerOptions=""

# Start off with very basic default, probably changed below.
ServerName="/usr/X11R6/bin/X"

# Start off with very basic default, probably changed below.
ClientName="/usr/X11R6/bin/xterm"

if [ -f $userclientrc ]; then
    ClientName="$userclientrc"
elif [ -f $sysclientrc ]; then
    ClientName="$sysclientrc"
fi

if [ -f $userserverrc ]; then
    ServerName="$userserverrc"
elif [ -f $sysserverrc ]; then
    ServerName="$sysserverrc"
fi

Display=""

if [ -f "$1" -a "$1" != "--" ]
then
  ClientName="$1"
  shift
fi

ClientOptions=""
while [ "$1" != "" -a "$1" != "--" ]
do
  ClientOptions="$ClientOptions '$1'"
  shift
done

if [ "$1" = "--" ]; then shift; fi

if [ -x "$1" ]
then
  ServerName="$1"
  shift
fi

while [ "$1" != "" ]
do
  case "$1" in
  :[0-9]*) Display="$1" ;;
  *) ServerOptions="$ServerOptions '$1'" ;;
  esac
  shift
done

if test "$Display" = ""
then
  Display=":$[`(echo /tmp/.X-1-lock; /bin/ls /tmp/.X?-lock 2>/dev/null) \
               |tail -1|sed \"s,^/tmp/.X\(.*\)-lock$,\1,\"` + 1]";

fi

if [ "$XAUTHORITY" = "" ]
then
  XAUTHORITY=$HOME/.Xauthority
  export XAUTHORITY
fi

AuthRemoveList=

# Set up default Xauth info for this machine.
case `uname` in
Linux*) if [ -z "`hostname --version 2>&1 | grep GNU`" ]
        then
          HostName=`hostname -f`
        else
          HostName=`hostname`
        fi
        ;;
*)      HostName=`hostname`
        ;;
esac

mcookie=`mcookie`
for ThisDisplayName in $Display $HostName$Display
do
  if ! xauth list "$ThisDisplayName" | grep "$ThisDisplayName " >/dev/null 2>&1
  then
    xauth -q << EOF
add $ThisDisplayName . $mcookie
EOF
    AuthRemoveList="$ThisDisplayName $AuthRemoveList"
  fi
done

eval xinit $ClientName $ClientOptions -- $ServerName $Display $ServerOptions

if [ "$AuthRemoveList" != "" ]
then
  xauth remove $AuthRemoveList
fi

if command -v deallocvt > /dev/null 2>&1
then
  deallocvt
fi
