#!/bin/sh

# Special features
#DEMO_ARGS="-im hiragana.im" # Input Method for Hiragana
#DEMO_ARGS="-im latin1.im" # Input Method for Latin1 characters
DEMO_ARGS=""

# Sanity checking
if [ ! -f ./start_demo ]
then
    echo "Please run start_demo from the directory containing it."
    exit 1
fi
if [ ! -f ./examples/launcher/launcher -a ! -f launcher ]
then
	echo "Could not find the demo program. Please read the INSTALL file."
	exit 1
fi
if [ "$TERM" == xterm ]
then
    echo "Please run start_demo from the Linux Console, not from within X11."
    exit 1
fi
if [ "$TERM" != linux ]
then
    echo "Please run start_demo from the Linux Console."
    exit 1
fi
if [ ! -w /dev/psaux ]
then
    echo "Warning: /dev/psaux is not writable (any mouse there will be ignored)."
fi
if [ ! -r /dev/ttyS0 ]
then
    echo "Warning: /dev/ttyS0 is not readable (any mouse there will be ignored)."
fi
if [ ! -r /dev/ttyS1 ]
then
    echo "Warning: /dev/ttyS1 is not readable (any mouse there will be ignored)."
fi
if [ ! -w /dev/fb0 ]
then
    echo "/dev/fb0 is not writable."
    exit 1
fi
if [ -f /tmp/.QtEmbedded -a ! -w /tmp/.QtEmbedded ]
then
    echo "Please remove /tmp/.QtEmbedded"
    exit 1
fi

# Information
cat <<EOF
------------------------------------------------------------------------------

                           Qt/Embedded Demo
                        ----------------------

            Please report any bugs to Qt-bugs@trolltech.com

------------------------------------------------------------------------------

EOF

# Set the mouse protocol
cat << EOF

(Note: ALT-CTRL-BACKSPACE will kill the demo at any time if the mouse fails)

EOF
unset QWS_MOUSE_PROTO

export QWS_DISPLAY=LinuxFb:0

# Which demo?
DEMO=launcher

unset QWS_SIZE
MODE=1024x768-75

FB0=$(grep '^0 ' /proc/fb)

FUSER=$(which fuser)
if [ ! -f "$FUSER" ]
then
    FUSER=/usr/sbin/fuser
fi
if [ ! -f "$FUSER" ]
then
    FUSER=/sbin/fuser
fi

if [ -z "$FB0" ]
then
    echo "WARNING: /proc/fb indicates that no framebuffer device exists"
fi

if [ "$FB0" != "0 VESA VGA" ]
then
    # Can probably set the mode.

    FBSET=$(which fbset)
    if [ ! -f "$FBSET" ]
    then
	FBSET=/usr/sbin/fbset
    fi
    if [ ! -f "$FBSET" ]
    then
	FBSET=/sbin/fbset
    fi
    if [ -f "$FBSET" ]
    then
	echo "Change to display mode $MODE? [yes]"
	read y
	if [ "$y" != n -a "$y" != no ]
	then
	    if $FBSET -depth 16 $MODE
	    then
		true
	    else
		echo "WARNING: $FBSET -depth 16 $MODE failed"
	    fi
	fi
    fi
fi

# Turn off stuff that is annoying in a demo
setterm -clear -powersave off -blank 0 -cursor off

# Kill gpm if it is running
gpm -k
/usr/sbin/gpm -k

# Reset the terminal upon exit
#trap "( kbd_mode -a; setterm -reset; reset; )" EXIT
trap "( kbd_mode -a; stty sane; setterm -cursor on )" EXIT

# Set up execution environment
if [ -f launcher ]
then
    true
else
    QTDIR=`pwd`
fi
LD_LIBRARY_PATH=$QTDIR/lib-e-$QT_EMBEDDED:$QTDIR/lib:$LD_LIBRARY_PATH
export QTDIR LD_LIBRARY_PATH

# Start the demo, throwing away debug messages
cd examples/$DEMO || cd ../$DEMO

# Run the demo...
E=3
while [ "$E" = 3 ]
do
    # Kill any existing fb0 applications
    setterm -clear
    $FUSER -k /dev/fb0
    ./$DEMO $DEMO_ARGS
    E=$?
done

# Kill any remaining clients
$FUSER -k /dev/fb0
