#!/bin/sh
#
# Copyright (C) 1997 Free Software Foundation, Inc.
#
# Author: Ovidiu Predescu <ovidiu@net-community.com>
# Date: October 1997
# 
# This file is part of the GNUstep Makefile Package.
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
# 
# You should have received a copy of the GNU General Public
# License along with this library; see the file COPYING.LIB.
# If not, write to the Free Software Foundation,
# 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

# Try to execute the application passed as argument. The application is
# searched through the GNUstep directories if a complete or relative path name
# is not specified. The arguments passed after the application name are passed
# unmodified to the application.

if [ -z "$1" ]; then
  echo usage: `basename $0` application [--library-combo=...] [arguments...]
  echo `basename $0` --help for help
  exit 1
fi

if [ -z "$GNUSTEP_FLATTENED" ]; then
  GNUSTEP_FLATTENED=
fi

# trap the --library-combo parameter
case $1 in
  --help)
    echo usage: `basename $0` application [--library-combo=...] [arguments...]
    echo
    echo application is the complete or relative name of the application
    echo program with the .app extension, like Ink.app.
    echo
    echo [arguments...] are the arguments to the application.     
    exit 0
    ;;
  *)
    app=$1; shift;;
esac

# Remove leading slashes at the end of the application name
app=`echo $app | sed 's%/*$%%'`

case $app in
  /*)	# An absolute path.
	full_appname=$app;;
  */*)	# A relative path
	full_appname=`(cd $app; pwd)`;;
  *)	# A path that should be searched into the GNUstep paths
	if [ -n $GNUSTEP_PATHPREFIX_LIST ]; then
	    SPATH=$GNUSTEP_PATHPREFIX_LIST
	else
	    SPATH=$PATH
	fi
	SPATH=.:$SPATH
	IFS=:
	for dir in $SPATH; do
	  if [ -d $dir/Applications/$app ]; then
	    full_appname=`(cd $dir/Applications/$app; pwd)`
	    break;
	  fi
	  if [ -d $dir/$app ]; then
	    full_appname=`(cd $dir/$app; pwd)`
	    break;
	  fi
	done;;
esac

if [ -z "$full_appname" ]; then
  echo "Can't find the required application: $app!"
  exit 1
fi

# get base app name
appname=`echo $app | sed 's/\.[a-z]*$//'`
appname=`basename $appname`

if [ -n "$GNUSTEP_FLATTENED" ]; then

  if [ -z "$EXEEXT" ]; then
    EXEEXT=
  fi

  if [ -n "$EXEEXT" ]; then
    appname="$appname$EXEEXT"
  fi

fi


if [ ! -f "$full_appname/$appname" ]; then
  echo "Could not find $full_appname/$appname executable/script"
  exit 1
fi

IFS=" "
exec "$full_appname/$appname" "$@"

