#!/bin/sh

DISPLAY=":0"
LOGDIR="/opt/var/log"
XORG_CONF=" -config /usr/etc/X11/xorg.conf -configdir /usr/etc/X11/xorg.conf.d "
OTHER_OPTIONS=" -logfile ${LOGDIR}/Xorg.0.log -ac -noreset -r +accessx 0 "

#backup previous Xorg.0.log file
if [ -e ${LOGDIR}/Xorg.0.log ]; then
	mv ${LOGDIR}/Xorg.0.log ${LOGDIR}/prev.Xorg.0.log
fi

#backup previous Xorg.0.log.old file
if [ -e ${LOGDIR}/Xorg.0.log.old ]; then
	mv ${LOGDIR}/Xorg.0.log.old ${LOGDIR}/prev.Xorg.0.log.old
fi

if [ -e ~/.xinitrc ]; then
	XINITRC=~/.xinitrc
else
	XINITRC=/usr/etc/X11/xinitrc
fi

XSERVER_OPTIONS=" ${DISPLAY} ${OTHER_OPTIONS} ${XORG_CONF} "

if [ "$1" = "--gdb" ]; then
	if [ -x /usr/bin/pvrsrvinit ]; then
		pvrsrvinit start
	fi
	gdb --args Xorg ${XSERVER_OPTIONS}
else
	if [ "$1" = "--only" ]; then
		if [ -x /usr/bin/pvrsrvinit ]; then
			pvrsrvinit start
		fi

		Xorg ${XSERVER_OPTIONS} &
	else
		xinit ${XINITRC} -- /usr/bin/Xorg ${XSERVER_OPTIONS} &
	fi
fi

