#! /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 "$GNUSTEP_PATHPREFIX_LIST" ]; then
  echo "The GNUSTEP_PATHPREFIX_LIST environment variable is missing."
  echo "Did you forget to set up your environment using GNUstep.sh ?"
  exit 1
fi

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

        # Search for a local tool

        # We used to scan all ./*/$(GNUSTEP_HOST_LDIR)/ directories,
        # but this facility was removed (GNUSTEP_HOST_LDIR is no
        # longer even defined in this file), now we perform a much
        # simpler search.

        # The really important one is looking into ./obj, anyway here
        # is the order in which we search local directories - 
        # First, we search in ./
        # Second, we search in ./obj
        # Third, we search in ./Tools/
        # Fourth, we search in ./Tools/obj
        for dir in . obj Tools Tools/obj; do
          # echo "$dir/$tool";
          if [ -x "$dir/$tool" ]; then
            full_toolname=$dir/$tool
            # echo "Found: $dir/$tool";
            break;
          fi
        done

	if [ -z "$full_toolname" ]; then 
          
          # Local tool not found - Search for an installed one
          # we search in GNUSTEP_PATHPREFIX_LIST, which means we
          # search in the order in:
          # GNUSTEP_USER_ROOT
          # GNUSTEP_LOCAL_ROOT
          # GNUSTEP_NETWORK_ROOT
          # GNUSTEP_SYSTEM_ROOT
	  #
	  # We look in the GNUSTEP_HOST_CPU/GNUSTEP_HOST_OS/LIBRARY_COMBO
	  # subdirectory first, then the GNUSTEP_HOST_CPU/GNUSTEP_HOST_OS
	  # subdirectory, then the top-level directory.
	  # (For flattened systems we skip the first two options.
	  #
          if [ -n "$GNUSTEP_PATHPREFIX_LIST" ]; then
            SPATH=$GNUSTEP_PATHPREFIX_LIST
	    IFS=:
	    for dir in $SPATH; do
	      tmpdir=$dir/Tools
	      if [ -z "$GNUSTEP_FLATTENED" ]; then
	        tmpgnudir=$dir/Tools/$GNUSTEP_HOST_CPU/$GNUSTEP_HOST_OS
		tmplibdir=$tmpgnudir/$LIBRARY_COMBO
		# echo "$tmplibdir/$tool";
		if [ -x "$tmplibdir/$tool" ]; then
		  # echo "Found: $tmplibdir/$tool";
		  full_toolname=$tmplibdir/$tool
		  break;
		fi
		if [ -x "$tmpgnudir/$tool" ]; then
		  # echo "Found: $tmpgnudir/$tool";
		  full_toolname=$tmpgnudir/$tool
		  break;
		fi
	      fi
	      # echo "$tmpdir/$tool";
	      if [ -x "$tmpdir/$tool" ]; then
		# echo "Found: $tmpdir/$tool";
		full_toolname=$tmpdir/$tool
		break;
	      fi
	    done
	    unset tmpdir
	    unset tmpgnudir
	    unset tmplibdir
	    IFS=" "
          fi
        fi
        ;;
esac

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

IFS=" "
exec "$full_toolname" "$@"
