2 # Copyright (C) 2016 Ian Kelling
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
8 # http://www.apache.org/licenses/LICENSE-2.0
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
17 # avoiding bashisms so it can be used in edge cases where I don't have bash,
18 # however, I'm not super confident that I've avoided them all
21 local help="usage: path_add [options] PATH...
23 --end: adds to end of path, which will give it lowest priority
24 --ifexists: add to path only if directory exists"
25 local found x y ifexists end loop newpath
29 # portable substring matching is ugly http://mywiki.wooledge.org/BashFAQ/041
33 if [ "$1" = --end ]; then
35 elif [ "$1" = --ifexists ]; then
37 elif [ "$1" = --help ]; then
48 # if we arent passed a PATH, just return 0 for convenience
53 # build up the path without the components we want to add
56 if [ "$x" = "$y" ]; then
63 if [ ! "$newpath" ]; then
74 if ! $ifexists ||
[ -d "$x" ]; then
75 if [ ! "$PATH" ]; then