#!/bin/bash
#
# Wrapper script to run Microwindows applications from inside the source tree
# The Nano-X server will be started if necessary
#
# Usage: ./runapp [-p|-L|-R|-D|-A] [bin/]application [arguments]
#			-p	no server quit on application exit
#			-L	left portrait mode
#			-R	right portrait mode
#			-D	upside down portrait mode
#			-A	auto switch portrait mode with mouse at screen edge
#
appdir=bin

# first determine the directory that contains this script itself
scriptdir=""
scriptname=$0

case "$0" in
  */*)
    # $0 contains a path, use it
    scriptdir=`dirname "$0"`
    scriptname=`basename "$0"`
    ;;
  *)
    # no directory in $0, search in PATH
    saved_ifs=$IFS
    IFS=:
    for d in $PATH
    do
      IFS=$saved_ifs
      if [ -x "$d/$scriptname" ]
      then
        scriptdir="$d"
        break
      fi
    done
    ;;
esac

# now find the top-level directory of the build tree
if [ -x "$scriptdir/bin/nano-X" ]
then topdir="$scriptdir"
elif [ -x "$scriptdir/../bin/nano-X" ]
then topdir="$scriptdir/.."
elif [ -x "$scriptdir/../../bin/nano-X" ]
then topdir="$scriptdir/../.."
elif [ -x "$scriptdir/../../../bin/nano-X" ]
then topdir="$scriptdir/../../.."
else
  topdir="."
fi

topdir=`cd "$topdir" && pwd`
bindir=$topdir/bin
serverargs=

# Check for Nano-X server argument
case "$1" in
  -*)
    serverargs=$1
	shift
    ;;
esac

progname=$1

# now find application specified
if [ "$progname" = "" ]
then
  	# no app specified, give usage
  	ls -C $appdir
	echo -n "Please enter demo program name: "
	read progname
fi
if [ "$progname" = "" ]
then
  	# no app specified, exit
  	exit 1
fi

case "$progname" in
  */*)
    # progname contains a path, use it
    application=$progname
    ;;
  *)
    # no directory in progname
	application=$appdir/$progname
    ;;
esac
# check if application present
if [ ! -x $application ]
then
	echo "$scriptname: can't find $application"
	exit 1
fi

# determine if nano-X server required
appname=`basename "$application"`
case "$appname" in
  mw*)
	need_server=NO
    ;;
  *)
	need_server=YES
    ;;
esac

# setup the environment

if [ "`uname -s`" = "Darwin" ]
then
  if [ -n "$DYLD_LIBRARY_PATH" ]
  then
    DYLD_LIBRARY_PATH="$topdir/lib:$DYLD_LIBRARY_PATH"
  else
    DYLD_LIBRARY_PATH="$topdir/lib"
  fi
  export DYLD_LIBRARY_PATH
else
  if [ -n "$LD_LIBRARY_PATH" ]
  then
    LD_LIBRARY_PATH="$topdir/lib:$LD_LIBRARY_PATH"
  else
    LD_LIBRARY_PATH="$topdir/lib"
  fi
  export LD_LIBRARY_PATH
fi

NANOXSERVER="$bindir/nano-X"
#echo $need_server $NANOXSERVER $application libs=$DYLD_LIBRARY_PATH libs=$LD_LIBRARY_PATH

# now check if nano-X server needs to be run, and exec application
shift
server_running=`ps | grep nano-X | sed '/grep/d'`
if [ "$need_server" = "YES" ] && [ "$server_running" = "" ]
then
  # find nano-X server
  if [ -x "$bindir/nano-X" ]
  then NANOXSERVER="$bindir/nano-X"
  else
    echo "$scriptname: could not find nano-X server in $bindir"
    exit 1
  fi
	$NANOXSERVER $serverargs & exec $application $@
else
	exec $application $@
fi
