#!/bin/sh
#

XSERVER=Xfbdev
BINDIR=/usr/bin

if [ -f $BINDIR/Xepson ]; then
  XSERVER=Xepson
fi
# if [ -f $BINDIR/Xorg ]; then
#  XSERVER=Xorg
# fi
if [ -f $BINDIR/Xomap ]; then
  XSERVER=Xomap
fi
if [ -f $BINDIR/Xw100 ]; then
  XSERVER=Xw100
fi
if [ -f $BINDIR/Xvesa ]; then
  XSERVER=Xvesa
fi
if [ -f $BINDIR/Ximageon ]; then
  XSERVER=Ximageon
fi
if [ -f $BINDIR/Xglamo ]; then
  XSERVER=Xglamo
fi

if [ -f /etc/profile ]; then
  . /etc/profile
fi

. /etc/init.d/functions

fallback_screen_arg() {
    geom=`fbset | grep geometry`
    w=`echo $geom | awk '{ print $2 }'`
    h=`echo $geom | awk '{ print $3 }'`
    b=`echo $geom | awk '{ print $6 }'`
    echo -n "${w}x${h}x${b}"
}

SCREEN_SIZE=`fallback_screen_arg`
export USER=root
export XSERVER_DEFAULT_ORIENTATION=normal

ARGS="-br -pn $INPUT_EXTRA_ARGS"
DPI="160"
MOUSE=""
KDRIVEARGS=""

# use ucb 1x00 touchscreen if present
if [ -z "$TSLIB_TSDEVICE" ] && [ -e /dev/touchscreen/ucb1x00 ]; then
  MOUSE="-mouse /dev/touchscreen/ucb1x00"
fi

# kdrive 1.4 onwards needs -mouse args
# Xorg doesn't support "-mouse" option, and uses /dev/input/mice automatically
#if [ -e "$TSLIB_TSDEVICE" ] && [ -e /dev/input/event1 ]; then
#  MOUSE="-mouse tslib -mouse mouse"
#else
#	if [ -e "$TSLIB_TSDEVICE" ]; then
#		MOUSE="-mouse tslib"
#	else
#		if [ -e /dev/input/mice ] && [ "$XSERVER" != "Xorg" ] ; then
#			MOUSE="-mouse mouse"
#		fi
#	fi
#fi

#cat /proc/bus/input/devices | grep event1 > /dev/null
#if [ "$?" == 0 ]; then
	MOUSE="-mouse tslib,,device=/dev/input/event0 -mouse mouse,,device=/dev/input/mouse1"
#else
#	MOUSE="-mouse tslib"
#fi

if [ -e /etc/default/xserver ] ; then
    . /etc/default/xserver
fi

if [ "$XSERVER" != "Xorg" ] ; then
    ARGS="$ARGS $MOUSE $KDRIVEARGS"
fi
