BUGFIX: Uninitialised member variables
[foam-extend-3.2.git] / bin / foamEtcFile
bloba08167148e3601217a5eaa5085afe46c4e176d32
1 #!/bin/sh
2 #------------------------------------------------------------------------------
3 # ========= |
4 # \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
5 # \\ / O peration |
6 # \\ / A nd | Copyright (C) 2011 OpenFOAM Foundation
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
14 # the Free Software Foundation, either version 3 of the License, or
15 # (at your 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, see <http://www.gnu.org/licenses/>.
25 # Script
26 # foamEtcFile
28 # Description
29 # Locate user/group/shipped file with semantics similar to the
30 # ~OpenFOAM/fileName expansion.
32 # The -mode option can be used to allow chaining from
33 # personal settings to site-wide settings.
35 # For example, within the user ~/.OpenFOAM/<VER>/prefs.sh:
36 # \code
37 # foamPrefs=`$WM_PROJECT_DIR/bin/foamEtcFile -m go prefs.sh` \
38 # && _foamSource $foamPrefs
39 # \endcode
41 # Note
42 # This script must exist in <foamInstall>/OpenFOAM-<VERSION>/bin/
43 # or <foamInstall>/openfoam<VERSION>/bin/ (for the debian version)
45 #-------------------------------------------------------------------------------
46 usage() {
47 [ "${optQuiet:-$optSilent}" = true ] && exit 1
49 exec 1>&2
50 while [ "$#" -ge 1 ]; do echo "$1"; shift; done
51 cat<<USAGE
53 Usage: ${0##*/} [OPTION] fileName
54 ${0##*/} [OPTION] -list
55 options:
56 -all return all files (otherwise stop after the first match)
57 -list list the directories to be searched
58 -mode <mode> any combination of u(user), g(group), o(other)
59 -prefix <dir> specify an alternative installation prefix
60 -quiet suppress all normal output
61 -silent suppress all stderr output
62 -version <ver> specify an alternative OpenFOAM version
63 in the form Maj.Min.Rev (eg, 1.7.0)
64 -help print the usage
66 Locate user/group/shipped file with semantics similar to the
67 ~OpenFOAM/fileName expansion.
69 The options can also be specified as a single character
70 (eg, '-q' instead of '-quiet'), but must not be grouped.
72 Exit status
73 0 when the file is found. Print resolved path to stdout.
74 1 for miscellaneous errors.
75 2 when the file is not found.
77 USAGE
78 exit 1
81 #-------------------------------------------------------------------------------
83 # the bin dir:
84 binDir="${0%/*}"
86 # the project dir:
87 projectDir="${binDir%/bin}"
89 # the prefix dir (same as foamInstall):
90 prefixDir="${projectDir%/*}"
92 # the name used for the project directory
93 projectDirName="${projectDir##*/}"
95 # version number used for debian packaging
96 unset versionNum
99 # handle standard and debian naming convention
101 case "$projectDirName" in
102 OpenFOAM-*) # standard naming convention OpenFOAM-<VERSION>
103 version="${projectDirName##OpenFOAM-}"
106 openfoam[0-9]*) # debian naming convention 'openfoam<VERSION>'
107 versionNum="${projectDirName##openfoam}"
108 case "$versionNum" in
109 ??) # convert 2 digit version number to decimal delineated
110 version=$(echo "$versionNum" | sed -e 's@\(.\)\(.\)@\1.\2@')
112 ???) # convert 3 digit version number to decimal delineated
113 version=$(echo "$versionNum" | sed -e 's@\(.\)\(.\)\(.\)@\1.\2.\3@')
115 ????) # convert 4 digit version number to decimal delineated
116 version=$(echo "$versionNum" | sed -e 's@\(.\)\(.\)\(.\)\(.\)@\1.\2.\3.\4@')
118 *) # failback - use current environment setting
119 version="$WM_PROJECT_VERSION"
121 esac
125 echo "Error : unknown/unsupported naming convention"
126 exit 1
128 esac
131 # default mode is 'ugo'
132 mode=ugo
133 unset optAll optList optQuiet optSilent
135 # parse options
136 while [ "$#" -gt 0 ]
138 case "$1" in
139 -h | -help)
140 usage
142 -a | -all)
143 optAll=true
145 -l | -list)
146 optList=true
148 -m | -mode)
149 [ "$#" -ge 2 ] || usage "'$1' option requires an argument"
150 mode="$2"
152 # sanity check:
153 case "$mode" in
154 *u* | *g* | *o* )
157 usage "'$1' option with invalid mode '$mode'"
159 esac
160 shift
162 -p | -prefix)
163 [ "$#" -ge 2 ] || usage "'$1' option requires an argument"
164 prefixDir="$2"
165 shift
167 -q | -quiet)
168 optQuiet=true
170 -s | -silent)
171 optSilent=true
173 -v | -version)
174 [ "$#" -ge 2 ] || usage "'$1' option requires an argument"
175 version="$2"
176 # convert x.y.z -> xyz version (if installation looked like debian)
177 if [ -n "$versionNum" ]
178 then
179 versionNum=$(echo "$version" | sed -e 's@\.@@g')
181 shift
184 shift
185 break
188 usage "unknown option: '$*'"
191 break
193 esac
194 shift
195 done
198 # debugging:
199 # echo "Installed locations:"
200 # for i in projectDir prefixDir projectDirName version versionNum
201 # do
202 # eval echo "$i=\$$i"
203 # done
206 # Save the essential bits of information
207 # silently remove leading ~OpenFOAM/ (used in Foam::findEtcFile)
208 nArgs=$#
209 fileName="${1#~OpenFOAM/}"
211 # Define the various places to be searched:
212 unset dirList
213 case "$mode" in
214 *u*) # user
215 userDir="$HOME/.${WM_PROJECT:-OpenFOAM}"
216 dirList="$dirList $userDir/$version $userDir"
218 esac
220 case "$mode" in
221 *g*) # group (site)
222 siteDir="${WM_PROJECT_SITE:-$prefixDir/site}"
223 dirList="$dirList $siteDir/$version $siteDir"
225 esac
227 case "$mode" in
228 *o*) # other (shipped)
229 if [ -n "$versionNum" ]
230 then
231 # debian packaging
232 dirList="$dirList $prefixDir/openfoam$versionNum/etc"
233 else
234 # standard packaging
235 dirList="$dirList $prefixDir/${WM_PROJECT:-OpenFOAM}-$version/etc"
238 esac
239 set -- $dirList
243 # The main routine
246 exitCode=0
247 if [ "$optList" = true ]
248 then
250 # list directories, or potential file locations
251 [ "$nArgs" -le 1 ] || usage
253 # a silly combination, but -quiet does have precedence
254 [ "$optQuiet" = true ] && exit 0
256 for dir
258 if [ "$nArgs" -eq 1 ]
259 then
260 echo "$dir/$fileName"
261 else
262 echo "$dir"
264 done
266 else
268 [ "$nArgs" -eq 1 ] || usage
270 # general error, eg file not found
271 exitCode=2
273 for dir
275 if [ -f "$dir/$fileName" ]
276 then
277 exitCode=0
278 if [ "$optQuiet" = true ]
279 then
280 break
281 else
282 echo "$dir/$fileName"
283 [ "$optAll" = true ] || break
286 done
291 exit $exitCode
293 #------------------------------------------------------------------------------