#!/bin/sh
#
# openapp.  Generated from openapp.in by configure.
#
# Copyright (C) 1997 Free Software Foundation, Inc.
#
# Author: Ovidiu Predescu <ovidiu@net-community.com>
# Date: October 1997
# Author: Nicola Pero <n.pero@mi.flashnet.it>
# Date: 2002
#
# 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%/*$%%'`

# Now build the list of directory names we look for.  If the user has
# given us a full application directory name (for example, Gorm.app)
# then we only search for the given directory name; if instead the
# user has given us the application name without the suffix (for
# example, 'Gorm') we want to search for Gorm.app, then for
# Gorm.debug, then for Gorm.profile (in that order).
# If the appname is known, save it to avoid running a grep later to get it.
case "$app" in
  *.app)     app_list="$app"; appname="";;
  *.debug)   app_list="$app"; appname="";;
  *.profile) app_list="$app"; appname="";;
  *)         app_list="$app.app:$app.debug:$app.profile"; appname="$app";
esac

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

      for dir in $SPATH; do
        for appdir in $app_list; do
          # First, search in $dir/Applications/any_path_here/$appdir
          #echo "$dir/Applications/"'*'"/$appdir"
 	  for d in $dir/Applications/*/$appdir ; do
	    # If nothing is found, * expands to '*'.  Check that it's
	    # not the case, and that we have a real match.
	    if [ "$d" != "$dir/Applications/"'*'"/$appdir" ]; then
	      #echo "  $d"
	      if [ -d "$d" ]; then
	        full_appname=`(cd "$d"; pwd)`
	        break 3
	      fi
	    fi
	  done
	  # Now, in $dir/Applications/$appdir
	  #echo "$dir/Applications/$appdir"
	  if [ -d "$dir/Applications/$appdir" ]; then
	    full_appname=`(cd $dir/Applications/$appdir; pwd)`
	    break 2
	  fi
	  # Finally, in $dir/$appdir
	  #echo "$dir/$appdir"
	  if [ -d "$dir/$appdir" ]; then
	    full_appname=`(cd $dir/$appdir; pwd)`
	    break 2
	  fi
	done
      done;;
esac
IFS="$old_IFS"
unset app_list
unset appdir

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

#echo "found: $full_appname"

# get base app name
if [ -z "$appname" ]; then
  appname=`echo $app | sed 's/\.[a-z]*$//'`
fi
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

exec "$full_appname/$appname" "$@"

