1#!/bin/sh
2#
3# Licensed to the Apache Software Foundation (ASF) under one or more
4# contributor license agreements.  See the NOTICE file distributed with
5# this work for additional information regarding copyright ownership.
6# The ASF licenses this file to You under the Apache License, Version 2.0
7# (the "License"); you may not use this file except in compliance with
8# the License.  You may obtain a copy of the License at
9#
10#     http://www.apache.org/licenses/LICENSE-2.0
11#
12# Unless required by applicable law or agreed to in writing, software
13# distributed under the License is distributed on an "AS IS" BASIS,
14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15# See the License for the specific language governing permissions and
16# limitations under the License.
17#
18#
19# Look for program[s] somewhere in $PATH.
20#
21# Options:
22#  -s
23#    Do not print out full pathname. (silent)
24#  -pPATHNAME
25#    Look in PATHNAME instead of $PATH
26#
27# Usage:
28#  PrintPath [-s] [-pPATHNAME] program [program ...]
29#
30# Initially written by Jim Jagielski for the Apache configuration mechanism
31#  (with kudos to Kernighan/Pike)
32
33##
34# Some "constants"
35##
36pathname=$PATH
37echo="yes"
38
39##
40# Find out what OS we are running for later on
41##
42os=`(uname) 2>/dev/null`
43
44##
45# Parse command line
46##
47for args in $*
48do
49    case $args in
50	-s  ) echo="no" ;;
51	-p* ) pathname="`echo $args | sed 's/^..//'`" ;;
52	*   ) programs="$programs $args" ;;
53    esac
54done
55
56##
57# Now we make the adjustments required for OS/2 and everyone
58# else :)
59#
60# First of all, all OS/2 programs have the '.exe' extension.
61# Next, we adjust PATH (or what was given to us as PATH) to
62# be whitespace separated directories.
63# Finally, we try to determine the best flag to use for
64# test/[] to look for an executable file. OS/2 just has '-r'
65# but with other OSs, we do some funny stuff to check to see
66# if test/[] knows about -x, which is the prefered flag.
67##
68
69if [ "x$os" = "xOS/2" ]
70then
71    ext=".exe"
72    pathname=`echo -E $pathname |
73     sed 's/^;/.;/
74	  s/;;/;.;/g
75	  s/;$/;./
76	  s/;/ /g
77	  s/\\\\/\\//g' `
78    test_exec_flag="-r"
79else
80    ext=""	# No default extensions
81    pathname=`echo $pathname |
82     sed 's/^:/.:/
83	  s/::/:.:/g
84	  s/:$/:./
85	  s/:/ /g' `
86    # Here is how we test to see if test/[] can handle -x
87    testfile="pp.t.$$"
88
89    cat > $testfile <<ENDTEST
90#!/bin/sh
91if [ -x / ] || [ -x /bin ] || [ -x /bin/ls ]; then
92    exit 0
93fi
94exit 1
95ENDTEST
96
97    if `/bin/sh $testfile 2>/dev/null`; then
98	test_exec_flag="-x"
99    else
100	test_exec_flag="-r"
101    fi
102    rm -f $testfile
103fi
104
105for program in $programs
106do
107    for path in $pathname
108    do
109	if [ $test_exec_flag $path/${program}${ext} ] && \
110	   [ ! -d $path/${program}${ext} ]; then
111	    if [ "x$echo" = "xyes" ]; then
112		echo $path/${program}${ext}
113	    fi
114	    exit 0
115	fi
116
117# Next try without extension (if one was used above)
118	if [ "x$ext" != "x" ]; then
119            if [ $test_exec_flag $path/${program} ] && \
120               [ ! -d $path/${program} ]; then
121                if [ "x$echo" = "xyes" ]; then
122                    echo $path/${program}
123                fi
124                exit 0
125            fi
126        fi
127    done
128done
129exit 1
130
131