ThirdParty packages: Add comment in README file. Contributed by Darrin Stephens
[OpenFOAM-1.6-ext.git] / bin / foamCleanPath
blob371764d3c16c376344d6c7fb1e7e3d4a1510c845
1 #!/bin/sh
2 #------------------------------------------------------------------------------
3 # ========= |
4 # \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
5 # \\ / O peration |
6 # \\ / A nd | Copyright held by original author
7 # \\/ M anipulation |
8 #------------------------------------------------------------------------------
9 # License
10 # This file is part of OpenFOAM.
12 # OpenFOAM is free software; you can redistribute it and/or modify it
13 # under the terms of the GNU General Public License as published by the
14 # Free Software Foundation; either version 2 of the License, or (at your
15 # option) any later version.
17 # OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
18 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 # for more details.
22 # You should have received a copy of the GNU General Public License
23 # along with OpenFOAM; if not, write to the Free Software Foundation,
24 # Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
26 # Script
27 # foamCleanPath
29 # Description
30 # Usage: foamCleanPath [-strip] path [wildcard] .. [wildcard]
32 # Prints its argument (which should be a ':' separated path)
33 # without all
34 # - duplicate elements
35 # - (if '-strip') non-accessible directories
36 # - elements whose start matches a wildcard
38 # Note:
39 # - this routine will fail when directories have embedded spaces
40 # - false matches possible if a wildcard contains '.' (sed regex)
41 #------------------------------------------------------------------------------
42 if [ "$#" -lt 1 -o "$1" = "-h" -o "$1" = "-help" ]
43 then
44 cat <<USAGE 1>&2
45 Usage: ${0##*/} [-strip] path [wildcard] .. [wildcard]
47 Prints its argument (which should be a ':' separated list) cleansed from
48 - duplicate elements
49 - elements whose start matches one of the wildcard(s)
50 - (if '-strip') non-accessible directories
51 USAGE
52 exit 1
56 strip=''
57 if [ "$1" = "-strip" ]
58 then
59 strip=true
60 shift
64 dirList="$1"
65 shift
67 ##DEBUG echo "input>$dirList<" 1>&2
69 # preserve current IFS and split on whitespace
70 oldIFS="$IFS"
71 IFS=' '
73 # "wildcard1 ... wildcardN" may have been passed as a single parameter
74 set -- $*
76 # strip out wildcards via sed
77 while [ "$#" -ge 1 ]
79 wildcard=$1
80 shift
81 ##DEBUG echo "remove>$wildcard<" 1>&2
82 dirList=`echo "$dirList" | sed -e "s@${wildcard}[^:]*:@@g"`
83 done
85 # split on ':' (and on space as well to avoid any surprises)
86 IFS=': '
87 set -- $dirList
89 ##DEBUG echo "intermediate>$dirList<" 1>&2
91 # rebuild the list from scratch
92 unset dirList
93 for dir
95 ##DEBUG echo "check>$dir<" 1>&2
96 #- dirs must exist
97 if [ -e "$dir" ]
98 then
99 #- no duplicate dirs
100 duplicate=`echo " $dirList " | sed -ne "s@ $dir @DUP@p"`
102 if [ ! "$duplicate" ]
103 then
104 dirList="$dirList $dir"
106 elif [ "$strip" != "true" ]
107 then
108 # Print non-existing directories if not in 'strip' mode.
109 dirList="$dirList $dir"
111 done
113 # parse on whitespace
114 IFS=' '
115 set -- $dirList
117 # join on ':'
118 IFS=':'
119 dirList="$*"
121 # restore IFS
122 IFS="$oldIFS"
124 ##DEBUG echo "output>$dirList<" 1>&2
125 echo "$dirList"
127 # -----------------------------------------------------------------------------