1#!/bin/sh
2#
3# @configure_input@
4#
5# Copyright (C) 1997 - 2006 Free Software Foundation, Inc.
6#
7# Author: Ovidiu Predescu <ovidiu@net-community.com>
8# Date: October 1997
9# Author: Nicola Pero <n.pero@mi.flashnet.it>
10# Date: 2002 - 2006
11#
12# This file is part of the GNUstep Makefile Package.
13#
14# This library is free software; you can redistribute it and/or
15# modify it under the terms of the GNU General Public License
16# as published by the Free Software Foundation; either version 3
17# of the License, or (at your option) any later version.
18#
19# You should have received a copy of the GNU General Public
20# License along with this library; see the file COPYING.
21# If not, write to the Free Software Foundation,
22# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23
24# Please note that all local variables are prefixed with 'openapp_' to
25# avoid conflicts.
26
27# Try to execute the application passed as argument. The application
28# is searched through the GNUstep directories if a complete or
29# relative path name is not specified. The arguments passed after the
30# application name are passed unmodified to the application. The
31# option --debug case the application to be launched inside a debugger
32# (originally we had a separate script, called debugapp, for that).
33
34if [ -z "$1" ]; then
35  echo usage: `basename "$0"` [--find] [--debug] application [arguments...]
36  echo `basename "$0"` --help for more help
37  exit 1
38fi
39
40# Here read all the --find/--debug/--help/etc arguments, up to the
41# first non-argument.  That one is the name of the application we want
42# to launch.  We leave any parameters after the app name unchanged so
43# that we have them available in $@ to give to the application.
44
45while [ x"$1" != x ]; do
46  case "$1" in
47  --help)
48    echo usage: `basename "$0"` [--find] [--debug] application [arguments...]
49    echo
50    echo "application is the complete or relative name of the application"
51    echo "program with or without the .app extension, like Ink.app."
52    echo
53    echo "[arguments...] are the arguments to the application."
54    echo
55    echo "If --find is used, openapp prints out the full path of the application "
56    echo "executable which would be executed, without actually executing it.  It"
57    echo "will also list all paths that are attempted."
58    echo
59    echo "If --debug is used, openapp starts the application in the debugger "
60    echo "(by default gdb, but can be changed using the GDB shell variable, "
61    echo "or the --gdb=... argument)."
62    echo
63    echo "A rarely used option is --library-combo=... which can be used"
64    echo "in non-flattened (advanced/non standard) setups to start up"
65    echo "the application using a specified library-combo."
66    echo
67    echo "Invoking this program as 'debugapp' is equivalent to using the --debug"
68    echo "option."
69    exit 0
70    ;;
71  --find)
72    openapp_only_find=yes;
73    if [ -z "$2" ]; then
74      echo "Missing application name.  Please try openapp --help for more help."
75      exit 1
76    fi
77    shift;;
78  --debug)
79    # debugapp manually sets this variable before invoking openapp.
80    # So if you change this, make sure to update debugapp too.
81    openapp_debug_mode=yes;
82    if [ -z "$2" ]; then
83      echo "Missing application name.  Please try openapp --help for more help."
84      exit 1
85    fi
86    shift;;
87  --gdb=*)
88    GDB=`echo "$1" | sed 's/--gdb=//'`
89    if [ -z "$2" ]; then
90      echo "Missing application name.  Please try openapp --help for more help."
91      exit 1
92    fi
93    shift;;
94  --library-combo=*)
95    # FIXME - Reset the existing library combo environment ?  We haven't read
96    # the config file yet, so GNUSTEP_MAKEFILES might not be set yet! :-/
97    # . $GNUSTEP_MAKEFILES/GNUstep-reset.sh
98    LIBRARY_COMBO=`echo "$1" | sed 's/--library-combo=//'`
99    if [ -z "$2" ]; then
100      echo "Missing application name.  Please try openapp --help for more help."
101      exit 1
102    fi
103    shift;;
104  *)
105    openapp_app="$1";
106    shift;
107    # Exit from the loop so the remaining arguments are in $@ and we
108    # can pass them unchanged to the application.
109    break;;
110  esac
111done
112
113# Try to determine GNUSTEP_MAKEFILES to source GNUstep.sh
114if [ -z "$GNUSTEP_CONFIG_FILE" ]; then
115  GNUSTEP_CONFIG_FILE=@GNUSTEP_CONFIG_FILE@
116fi
117
118if [ -z "$GNUSTEP_USER_CONFIG_FILE" ]; then
119  GNUSTEP_USER_CONFIG_FILE=@GNUSTEP_USER_CONFIG_FILE@
120fi
121
122if [ -f "$GNUSTEP_CONFIG_FILE" ]; then
123  . "$GNUSTEP_CONFIG_FILE"
124fi
125
126GNUSTEP_HOME=~
127
128if [ -n "$GNUSTEP_USER_CONFIG_FILE" ]; then
129  case "$GNUSTEP_USER_CONFIG_FILE" in
130    /*) # An absolute path
131        if [ -f "$GNUSTEP_USER_CONFIG_FILE" ]; then
132          . "$GNUSTEP_USER_CONFIG_FILE"
133        fi;;
134     *) # Something else
135        if [ -f "$GNUSTEP_HOME/$GNUSTEP_USER_CONFIG_FILE" ]; then
136          . "$GNUSTEP_HOME/$GNUSTEP_USER_CONFIG_FILE"
137        fi;;
138  esac
139fi
140
141if [ -z "$GNUSTEP_MAKEFILES" ]; then
142  GNUSTEP_MAKEFILES="@GNUSTEP_MAKEFILES@"
143fi
144# OK, we now have GNUSTEP_MAKEFILES, source GNUstep.sh
145
146# Ahm ... TODO: we shouldn't need to source GNUstep.sh if we are
147# running flattened. :-) Anyway it won't harm (just slow things down a
148# lot).  This must be after parsing the --library-combo parameter.
149
150# Also, we'd like to get the GNUSTEP_*_APPS directories so we can
151# search them, so tell GNUstep.sh to export all variabes.
152GNUSTEP_SH_EXPORT_ALL_VARIABLES=yes
153. "$GNUSTEP_MAKEFILES/GNUstep.sh"
154unset GNUSTEP_SH_EXPORT_ALL_VARIABLES
155
156if [ -z "$GNUSTEP_IS_FLATTENED" ]; then
157  GNUSTEP_IS_FLATTENED=@GNUSTEP_IS_FLATTENED@
158fi
159
160if [ -z "$GDB" ]; then
161  GDB=gdb
162fi
163
164# Remove leading slashes at the end of the application name
165openapp_app=`echo "$openapp_app" | sed 's%/*$%%'`
166
167# Check if the user has provided the .app suffix; if not, add it.
168# Save the appname (without the .app suffix) if we have it, so
169# we save a sed (to remove the .app suffix) later on.
170case "$openapp_app" in
171  *.app) openapp_appname="";;
172  *)     openapp_appname="$openapp_app"; openapp_app="$openapp_app.app";;
173esac
174
175case "$openapp_app" in
176    /*) # An absolute path.
177      if [ -n "$openapp_only_find" ]; then
178        echo "Trying $openapp_app..."
179      fi
180      if [ -d "$openapp_app" ]; then
181	openapp_full_appname="$openapp_app"
182      fi
183      ;;
184    */*) # A relative path
185      if [ -n "$openapp_only_find" ]; then
186        echo "Trying $openapp_app..."
187      fi
188      if [ -d "$openapp_app" ]; then
189	openapp_full_appname=`(cd "$openapp_app"; pwd)`
190      fi
191      ;;
192    *)
193      # We should first search the standard GNUstep locations.
194      for openapp_dir in "$GNUSTEP_USER_APPS" "$GNUSTEP_LOCAL_APPS" "$GNUSTEP_NETWORK_APPS" "$GNUSTEP_SYSTEM_APPS"; do
195	  # Standard locations ... in $domain_apps/$openapp_app
196          if [ -n "$openapp_only_find" ]; then
197            echo "Trying $openapp_dir/$openapp_app..."
198          fi
199	  if [ -d "$openapp_dir/$openapp_app" ]; then
200	    openapp_full_appname=`(cd "$openapp_dir/$openapp_app"; pwd)`
201	    break
202	  fi
203      done
204      if [ -z "$openapp_full_appname" ]; then
205
206        # Now search the Admin Apps locations ... but only if they are
207        # Administrators.  But how do we know if we are an Admin ?  We
208        # need a portable way.  Our first attempt here is to check if
209        # we can write to an ADMIN directory, we are an Administrator.
210        # So, if any GNUSTEP_*_ADMIN_APPS directory exists and we can
211        # write to it, then we consider us enough powerful to execute
212        # applications from it and search in it.
213        #
214        # FIXME: Unfortunately, this doesn't work if the Admin directory
215        # is mounted read-only, so a better test is required!
216        #
217        for openapp_dir in "$GNUSTEP_USER_ADMIN_APPS" "$GNUSTEP_LOCAL_ADMIN_APPS" "$GNUSTEP_NETWORK_ADMIN_APPS" "$GNUSTEP_SYSTEM_ADMIN_APPS"; do
218	  if [ -d "$openapp_dir"  -a  -w "$openapp_dir" ]; then
219            if [ -n "$openapp_only_find" ]; then
220              echo "Trying $openapp_dir/$openapp_app..."
221            fi
222	    if [ -d "$openapp_dir/$openapp_app" ]; then
223	      openapp_full_appname=`(cd "$openapp_dir/$openapp_app"; pwd)`
224	      break
225	    fi
226          fi
227        done
228
229	# And now search the standard PATH (may include '.')
230	old_IFS="$IFS"
231	IFS=:
232	for openapp_dir in $PATH; do
233          if [ -n "$openapp_only_find" ]; then
234            echo "Trying $openapp_dir/$openapp_app..."
235          fi
236	  if [ -d "$openapp_dir/$openapp_app" ]; then
237	    openapp_full_appname=`(cd "$openapp_dir/$openapp_app"; pwd)`
238            break
239	  fi
240	done
241	IFS="$old_IFS"
242      fi
243    ;;
244esac
245
246if [ -z "$openapp_full_appname" ]; then
247  echo "Can't find the required application: $openapp_app!"
248  if [ -d "./$openapp_app" ]; then
249    echo "There is a $openapp_app in this directory; please use 'openapp ./$openapp_app' if you want to open it!"
250  fi
251  exit 1
252fi
253
254# get base app name
255if [ -z "$openapp_appname" ]; then
256  openapp_appname=`echo "$openapp_app" | sed 's/\.app$//'`
257fi
258openapp_appname=`basename "$openapp_appname"`
259
260if [ -z "$EXEEXT" ]; then
261  EXEEXT=@EXEEXT@
262fi
263
264if [ -n "$EXEEXT" ]; then
265  openapp_appname="$openapp_appname$EXEEXT"
266fi
267
268if [ "$GNUSTEP_IS_FLATTENED" = "no" ]; then
269
270  case "$LIBRARY_COMBO" in
271    apple-*) openapp_app_executable="$openapp_full_appname/Contents/MacOS/$openapp_appname";;
272    *)       openapp_app_executable="$openapp_full_appname/$GNUSTEP_HOST_CPU-$GNUSTEP_HOST_OS/$LIBRARY_COMBO/$openapp_appname";;
273  esac
274
275else
276
277  case "$LIBRARY_COMBO" in
278    apple-*) openapp_app_executable="$openapp_full_appname/Contents/MacOS/$openapp_appname";;
279    *)       openapp_app_executable="$openapp_full_appname/$openapp_appname";;
280  esac
281
282fi
283
284
285if [ ! -f "$openapp_app_executable" ]; then
286  echo "Could not find $openapp_app_executable executable/script"
287  exit 1
288fi
289
290if [ -n "$openapp_only_find" ]; then
291  echo " => Using $openapp_app_executable"
292  exit 0
293fi
294
295if [ -n "$openapp_debug_mode" ]; then
296
297  # Search for a core file in the current directory.
298  openapp_corearg=
299  openapp_corefiles="core*"
300  for openapp_corefile in $openapp_corefiles; do
301    if [ -f "$openapp_corefile" ]; then
302      echo "Core image ($openapp_corefile) has been found in working directory. Use it (y/n)? ";
303      # Need an argument here for Solaris
304      read REPLY;
305      if [ $REPLY = y ]; then
306        echo "Using it.";
307        corearg="--core=$openapp_corefile";
308        break;
309      else
310        echo "Ignoring it.";
311      fi
312    fi
313  done
314  unset openapp_corefile
315  unset openapp_corefiles
316
317  if [ -z "$openapp_corearg" ]; then
318
319    # Old versions of gdb don't support --args, so we only use it if
320    # 'gdb --help' lists it.
321    openapp_args=
322    if ("$GDB" --help | grep -e '\-\-args' > /dev/null); then
323      openapp_args="--args"
324    fi
325    # Arguments passed to debugapp are passed over to the
326    # application, in the same way as it happens for openapp.
327
328    "$GDB" $openapp_args "$openapp_app_executable" "$@"
329
330  else
331
332    "$GDB" "$openapp_app_executable" "$openapp_corearg"
333
334  fi
335
336else # non-debug follows
337
338  exec "$openapp_app_executable" "$@"
339
340fi
341