#!/bin/sh -x


# usage: rundemos
#
# This program sets up some env vars and executes rbuttonfly on 
# the subdirectory 'demo'.


# truepath <path>
# This function determines the real path of a possibly linked file

truepath() {

	# Check the arg count
	if [ "$#" != "1" ]; then
		echo "usage: truepath <path>"
		exit 1
	fi
	
	path="$1"
	
	# Is the path absolute?
	if [ `echo $path | sed -e "s,^/.*,YES,g"` = "YES" ]; then
		dir=`dirname $path`
	else
		dir="`pwd`/`dirname $path`"
	fi
	
	prog="`basename $path`"
	cd $dir
	dir="`pwd`"
	
	while :; do
	        file="$dir/$prog"
	        if [ `ls -ld $file | wc -w` != `ls -ld / | wc -w` ]; then
	                file="`ls -l $file | awk '{ print $11 }'`"
	                dir="`dirname $file`"
	                prog="`basename $file`"
	                cd $dir
	                dir="`pwd`"
	        else
	                break;
	        fi
	done
	
	echo $file
}


# Determine the actual demo directory and go there

DEMODIR=`truepath $0`
DEMODIR=`dirname $DEMODIR`
export DEMODIR

cd $DEMODIR


# Determine gfx type

GFXPATH=""

gfxinfo=`/usr/gfx/gfxinfo | head -1`
impact='IMPACT'
reality='REV'
infinite='KONA'

if   [ "`echo $gfxinfo | grep $impact`" ]; then
	GFXPATH=`pwd`/bin/IMP:
elif [ "`echo $gfxinfo | grep $reality`" ]; then
	GFXPATH=`pwd`/bin/RE2:
elif [ "`echo $gfxinfo | grep $infinite`" ]; then
	GFXPATH=`pwd`/bin/IRE:
fi


# Determine cpu type

CPUPATH=""

CPU="`hinv | grep "CPU:" | cut -d' ' -f3`"
case "$CPU" in
        R4000|R4400|R4600)
                CPUPATH=`pwd`/bin/R4000:
                ;;
        R5000)
                CPUPATH=`pwd`/bin/R5000:
                ;;
        R8000)
                CPUPATH=`pwd`/bin/R8000:
                ;;
        R10000)
                CPUPATH=`pwd`/bin/R10000:
                ;;
esac


# Setup the environment variables

PATH=$GFXPATH$CPUPATH`pwd`/bin:$PATH:`pwd`/etc
export PATH

LD_LIBRARY_PATH=`pwd`/lib:$LD_LIBRARY_PATH
export LD_LIBRARY_PATH

IL_FILE_FORMAT_PATH=`pwd`/lib/filefmt:$IL_FILE_FORMAT_PATH
export IL_FILE_FORMAT_PATH

rbuttonfly "Demos" ./demo
