#! /bin/sh
#
# Copyright (C) 1997, 1999 Free Software Foundation, Inc.
#
# Author: Scott Predescu <ovidiu@net-community.com>
# Author: Ovidiu Predescu <ovidiu@net-community.com>
# Date: February 1999
# 
# 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 GNUstep tool passed as argument. The tool is
# searched through the GNUstep directories if a complete or relative path name
# is not specified. The arguments passed after the tool name are passed
# unmodified to the tool.

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

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

# traps the parameters
case $1 in
  --help)
    echo usage: `basename $0` tool [--library-combo=...] [arguments...]
    echo tool is the complete or relative name of the tool executable
    echo without any extension, like dread.
    echo
    echo [arguments...] are the arguments to the tool.
    exit 0
    ;;
  *)
    tool=$1; shift;;
esac

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

case $tool in
  /*)	# An absolute path.
  	full_toolname=$tool;;
  */*)	# A relative path
	tool_dir=`dirname $tool`; 
	tool_dir=`(cd $tool_dir; pwd)`; 
	tool_name=`basename $tool`;
	full_toolname=${tool_dir}/${tool_name};;
  *)	# A path that should be searched into GNUstep tool paths

        if [ -z "$GNUSTEP_FLATTENED" ]; then
          # search for a local one
          for file in */$GNUSTEP_HOST_LDIR/$tool; do
            if [ -x $file ]; then
              full_toolname=$file;
              break;
            fi
          done
	fi

        if [ -n $GNUSTEP_PATHPREFIX_LIST ]; then
          SPATH=$GNUSTEP_PATHPREFIX_LIST
        else
          SPATH=$PATH
        fi
        SPATH=.:$SPATH
        IFS=:
        for dir in $SPATH; do
          if [ -x $dir/Tools/$tool ]; then
            full_toolname=$dir/Tools/$tool
            break;
          fi
        done
        ;;
esac

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

IFS=" "
exec $full_toolname "$@"
