#!/bin/bash

#
# move this file to ~/.xserverrc, if you don't want to allow
# everybody to get access to your X-Server
#

  auth=$HOME/.Xauthority
dspnum=":0"
  args=""

while [ -n "$1" ] ; do
    case "$1" in
	\:[0-9])
	    dspnum="$1" ;     shift ;;
	*)
	    args="$args $1" ; shift ;;
    esac
done

if [ -x "`type -p keygen`" ] ; then
    if [ ! -x  "`type -p hostname`" ] ; then
        echo "startx: can't get my hostname - exiting" 1>&2
	exit 1
    else
	host=`hostname -f`
    fi

    trap "echo" 1 2 15
    cookie="MIT-MAGIC-COOKIE-1 `keygen`"
     tcpip="$host$dspnum"
      unix="${host%%.*}/unix$dspnum"

    xauth -f $auth source - <<-EOF
	add $tcpip $cookie
	add $unix  $cookie
	EOF
    cookie=

    exec Xwrapper $dspnum -auth $auth $args
else
    exec Xwrapper $dspnum $args
fi

