#!/bin/sh
#

XSERVER=Xfbdev

if [ -f /usr/bin/Xepson ]; then
  XSERVER=Xepson
fi
if [ -f /usr/bin/Xorg ]; then
  XSERVER=Xorg
fi
if [ -f /usr/bin/Xomap ]; then
  XSERVER=Xomap
fi
if [ -f /usr/bin/Xw100 ]; then
  XSERVER=Xw100
fi
if [ -f /usr/bin/Xvesa ]; then
  XSERVER=Xvesa
fi
if [ -f /usr/bin/Ximageon ]; then
  XSERVER=Ximageon
fi
if [ -f /usr/bin/Xglamo ]; then
  XSERVER=Xglamo
fi

. /etc/profile

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}"
}

module_id() {
    ## used to read from assets, but sometimes assets is corrupted
    # grep "Module ID" /proc/hal/assets | sed "s/.*://"
    ## used to read from /proc/hal/model, but that is removed in 2.6
    # echo ' iPAQ' `cat /proc/hal/model`
    awk 'BEGIN { FS=": " } /Hardware/ { print $2 } ' </proc/cpuinfo
}

SCREEN_SIZE=`fallback_screen_arg`
export USER=root

ARGS="-br -pn $INPUT_EXTRA_ARGS"

# use ucb 1x00 touchscreen if present
if [ -z "$TSLIB_TSDEVICE" ] && [ -e /dev/touchscreen/ucb1x00 ]; then
  ARGS="$ARGS -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" ] && [ "$XSERVER" != "Xorg" ]; then
  ARGS="$ARGS -mouse tslib"
else
  if  [ -e /dev/input/mice ] && [ "$XSERVER" != "Xorg" ] ; then
    ARGS="$ARGS -mouse mouse"
  fi
fi

# start off server in conventional location.
case `module_id` in
	"HP iPAQ H3100" | "HP iPAQ H3800")
		ARGS="$ARGS -dpi 100 -rgba vrgb -screen 320x240@90" ;;
	"HP iPAQ H3600" | "HP iPAQ H3700" | "HP iPAQ H3900")
		ARGS="$ARGS -dpi 100 -rgba vbgr -screen 320x240@270" ;;
	"HP iPAQ H5400" | "HP iPAQ H2200")
		ARGS="$ARGS -dpi 100 -rgba rgb" ;;
        "HP iPAQ HX4700")
                ARGS="$ARGS -dpi 200" ;;
	"Ramses")
		# What is this "vt2" in aid of?
		ARGS="$ARGS -dpi 100 -rgba vrgb -screen 320x240@90 vt2" ;;
	# both 'Sharp-Collie' and just 'Collie' have been reported
	*Poodle)
		ARGS="$ARGS -dpi 100 -rgba vrgb -screen 320x240@270" ;;
	*Collie)
		ARGS="$ARGS -dpi 100 -rgba vrgb -screen 320x240@270"
		;;
	"SHARP Shepherd" | "SHARP Husky" | "SHARP Corgi")
		ARGS="$ARGS -dpi 200 -rgba rgb" ;;
	"SHARP Spitz" | "SHARP Akita" | "SHARP Borzoi")
		ARGS="$ARGS -dpi 200 -rgba rgb -screen 480x640@270" ;;
	"Simpad")
		ARGS="$ARGS -dpi 100 -rgba rgb" ;;
	"Generic OMAP1510/1610/1710")
		ARGS="$ARGS -dpi 220 -mouse /dev/input/event0" ;;
	"TI-OSK")
		ARGS="$ARGS -dpi 100 -mouse tslib" ;;
	"Cellon C8000 Board")
		ARGS="$ARGS -dpi 100 -screen ${SCREEN_SIZE},10,1" ;;
	"HTC Universal")
		ARGS="$ARGS -dpi 100 -screen 480x640@270" ;;
	"HTC Tornado")
		ARGS="$ARGS -dpi 100 -hide-cursor" ;;
	"Atmel AT91SAM9261-EK" | "Atmel AT91SAM9263-EK")
		ARGS="$ARGS -mouse tslib -dpi 100 -rgba bgr -screen 240x320" ;;	
	"Generic OMAP1510/1610/1710" | "Nokia N770")
		ARGS="$ARGS -dpi 225 -screen ${SCREEN_SIZE} -mouse tslib" ;;
	"Nokia N800")
		ARGS="$ARGS -dpi 225 -screen ${SCREEN_SIZE} -mouse tslib" ;;
	"Nokia RX-44")
		ARGS="$ARGS -dpi 225 -screen ${SCREEN_SIZE} -mouse tslib" ;;
	"GTA01" )
		ARGS="$ARGS -dpi 285 -screen 480x640" ;;
	"Motorola Ezx Platform")
		ARGS="$ARGS -dpi 170 -screen 240x320" ;;
	"ARM-IntegratorCP" | "ARM-Versatile PB")
		ARGS="$ARGS -rgba vrgb" ;;
	"Compulab CM-x270")
		modprobe mbxfb
	        ARGS="$ARGS -fb /dev/fb1" ;;
	"GTA02")
 		ARGS="$ARGS -dpi 285 -screen ${SCREEN_SIZE}" 
		;;
	"Boundary Devices Neon board")
		ARGS="$ARGS -dpi 100 -screen ${SCREEN_SIZE} -mouse tslib"
		;;
	"Hoeft&Wessel HW90350")
		 ARGS="$ARGS -dpi 100 -screen ${SCREEN_SIZE} -mouse tslib"
		 ;;
	# This is a fallback for PCs
	"")
		if [ -f /usr/bin/Xvesa ]; then
			ARGS="$ARGS -dpi 100 -mode 0x0114"
		else
			ARGS="$ARGS -dpi 100"
		fi
esac

echo "tslib: $TSLIB_TSDEVICE"
echo "exec $XSERVER $ARGS $*"
exec $XSERVER $ARGS $*
