#!/bin/sh

if [ $# = 0 ]; then exit 0 ; fi
cmd1=`which $1`
if [ x"cmd1" = x ]; then exit 1; fi
shift

PATH=/usr/X11R6/bin:$PATH
LD_LIBRARY_PATH=/usr/X11R6/lib:/usr/local/lib:$LD_LIBRARY_PATH

export TSLIB_TSDEVICE TSLIB_TSEVENTTYPE PATH LD_LIBRARY_PATH

if [ -f /etc/sysconfig/x11 ]; then
    . /etc/sysconfig/x11
    export X11_RIGHT_MOUSE_BUTTON X11_RIGHT_MOUSE_BUTTON_MOD X11_MIDDLE_MOUSE_BUTTON_MOD
fi

test -f /etc/sysconfig/keyboard/kernel.map -a -f /usr/bin/loadkeys && sudo /usr/bin/loadkeys /etc/sysconfig/keyboard/kernel.map

sudo modprobe mousedev >/dev/null 2>/dev/null

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

removelist=


# set up default Xauth info for this machine
hostname=`hostname -f`
authdisplay=${display:-:0}
mcookie=`mcookie`
for displayname in $authdisplay $hostname$authdisplay; do
    if ! xauth list "$displayname" | grep "$displayname " >/dev/null 2>&1; then
        xauth -q << EOF 
add $displayname . $mcookie
EOF
	removelist="$displayname $removelist"
    fi
done

xinit $cmd1 $* -- -nolisten tcp -screen 480x640@270 -fp /usr/X11R6/lib/X11/fonts/misc,

if [ x"$removelist" != x ]; then
    xauth remove $removelist
fi

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