Merge branch 'master' of ssh://opencfd@repo.or.cz/srv/git/OpenFOAM-1.5.x
[OpenFOAM-1.5.x.git] / bin / foamCleanPath
blobf59747be8d9d8f5fb494e38cf75e970951621b8e
1 #!/bin/sh
2 #------------------------------------------------------------------------------
3 # ========= |
4 # \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
5 # \\ / O peration |
6 # \\ / A nd | Copyright (C) 1991-2008 OpenCFD Ltd.
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 path [wildcard] .. [wildcard]
32 # Prints its argument (which should be a ':' separated path)
33 # without all
34 # - duplicate elements
35 # - 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##*/} path [wildcard] .. [wildcard]
47 Prints its argument (which should be a ':' separated list) cleansed from
48 - duplicate elements
49 - non-accessible directories
50 - elements whose start matches one of the wildcard(s)
51 USAGE
52 exit 1
55 dirList="$1"
56 shift
58 ##DEBUG echo "input>$dirList<" 1>&2
60 # preserve current IFS and split on whitespace
61 oldIFS="$IFS"
62 IFS=' '
64 # "wildcard1 ... wildcardN" may have been passed as a single parameter
65 set -- $*
67 # strip out wildcards via sed
68 while [ "$#" -ge 1 ]
70 wildcard=$1
71 shift
72 ##DEBUG echo "remove>$wildcard<" 1>&2
73 dirList=`echo "$dirList" | sed -e "s@${wildcard}[^:]*:@@g"`
74 done
76 # split on ':' (and on space as well to avoid any surprises)
77 IFS=': '
78 set -- $dirList
80 ##DEBUG echo "intermediate>$dirList<" 1>&2
82 # rebuild the list from scratch
83 unset dirList
84 for dir
86 ##DEBUG echo "check>$dir<" 1>&2
87 #- dirs must exist
88 if [ -e "$dir" ]
89 then
90 #- no duplicate dirs
91 duplicate=`echo " $dirList " | sed -ne "s@ $dir @DUP@p"`
93 if [ ! "$duplicate" ]
94 then
95 dirList="$dirList $dir"
98 done
100 # parse on whitespace
101 IFS=' '
102 set -- $dirList
104 # join on ':'
105 IFS=':'
106 dirList="$*"
108 # restore IFS
109 IFS="$oldIFS"
111 ##DEBUG echo "output>$dirList<" 1>&2
112 echo "$dirList"
114 # -----------------------------------------------------------------------------