#!/bin/bash

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

typeset -i num=0
for lock in /tmp/.X*-lock ; do
    test -e $lock || break
    let num++
done

dspnum=":$num"
  args=""
  done=no
if test -z "$XAUTHORITY" ; then
  auth="$HOME/.Xauthority"
else
  auth="$XAUTHORITY"
fi

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

# handle TCP port 6000
. /etc/sysconfig/displaymanager
if test "$DISPLAYMANAGER_XSERVER_TCP_PORT_6000_OPEN" != "yes" ; then
    args="$args -nolisten tcp"
fi

if test -x "$(type -p keygen)" -a "$done" != "yes" ; then
    host=localhost

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

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

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