Initial commit for version 2.0.x patch release
[OpenFOAM-2.0.x.git] / bin / foamEtcFile
blob8a1f5d97e627a1eed729cfb6d725f54a11f8cbf0
1 #!/bin/sh
2 #------------------------------------------------------------------------------
3 # ========= |
4 # \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
5 # \\ / O peration |
6 # \\ / A nd | Copyright (C) 2008-2011 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
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 [ "${quietOpt:-$silentOpt}" = 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 -list list the directories to be searched
57 -mode <mode> any combination of u(user), g(group), o(other)
58 -prefix <dir> specify an alternative installation prefix
59 -quiet suppress all normal output
60 -silent suppress all stderr output
61 -version <ver> specify an alternative OpenFOAM version
62 in the form Maj.Min.Rev (eg, 1.7.0)
63 -help print the usage
65 Locate user/group/shipped file with semantics similar to the
66 ~OpenFOAM/fileName expansion.
68 The options can also be specified as a single character
69 (eg, '-q' instead of '-quiet'), but must not be grouped.
71 Exit status
72 0 when the file is found. Print resolved path to stdout.
73 1 for miscellaneous errors.
74 2 when the file is not found.
76 USAGE
77 exit 1
80 #-------------------------------------------------------------------------------
82 # the bin dir:
83 binDir="${0%/*}"
85 # the project dir:
86 projectDir="${binDir%/bin}"
88 # the prefix dir (same as foamInstall):
89 prefixDir="${projectDir%/*}"
91 # the name used for the project directory
92 projectDirName="${projectDir##*/}"
94 # version number used for debian packaging
95 unset versionNum
98 # handle standard and debian naming convention
100 case "$projectDirName" in
101 OpenFOAM-*) # standard naming convention OpenFOAM-<VERSION>
102 version="${projectDirName##OpenFOAM-}"
105 openfoam[0-9]*) # debian naming convention 'openfoam<VERSION>'
106 versionNum="${projectDirName##openfoam}"
107 case "$versionNum" in
108 ??) # convert 2 digit version number to decimal delineated
109 version=$(echo "$versionNum" | sed -e 's@\(.\)\(.\)@\1.\2@')
111 ???) # convert 3 digit version number to decimal delineated
112 version=$(echo "$versionNum" | sed -e 's@\(.\)\(.\)\(.\)@\1.\2.\3@')
114 ????) # convert 4 digit version number to decimal delineated
115 version=$(echo "$versionNum" | sed -e 's@\(.\)\(.\)\(.\)\(.\)@\1.\2.\3.\4@')
117 *) # failback - use current environment setting
118 version="$WM_PROJECT_VERSION"
120 esac
124 echo "Error : unknown/unsupported naming convention"
125 exit 1
127 esac
130 # default mode is 'ugo'
131 mode=ugo
132 unset listOpt quietOpt silentOpt
134 # parse options
135 while [ "$#" -gt 0 ]
137 case "$1" in
138 -h | -help)
139 usage
141 -l | -list)
142 listOpt=true
144 -m | -mode)
145 [ "$#" -ge 2 ] || usage "'$1' option requires an argument"
146 mode="$2"
148 # sanity check:
149 case "$mode" in
150 *u* | *g* | *o* )
153 usage "'$1' option with invalid mode '$mode'"
155 esac
156 shift
158 -p | -prefix)
159 [ "$#" -ge 2 ] || usage "'$1' option requires an argument"
160 prefixDir="$2"
161 shift
163 -q | -quiet)
164 quietOpt=true
166 -s | -silent)
167 silentOpt=true
169 -v | -version)
170 [ "$#" -ge 2 ] || usage "'$1' option requires an argument"
171 version="$2"
172 # convert x.y.z -> xyz version (if installation looked like debian)
173 if [ -n "$versionNum" ]
174 then
175 versionNum=$(echo "$version" | sed -e 's@\.@@g')
177 shift
180 shift
181 break
184 usage "unknown option: '$*'"
187 break
189 esac
190 shift
191 done
194 # debugging:
195 # echo "Installed locations:"
196 # for i in projectDir prefixDir projectDirName version versionNum
197 # do
198 # eval echo "$i=\$$i"
199 # done
202 # Save the essential bits of information
203 # silently remove leading ~OpenFOAM/ (used in Foam::findEtcFile)
204 nArgs=$#
205 fileName="${1#~OpenFOAM/}"
207 # Define the various places to be searched:
208 unset dirList
209 case "$mode" in
210 *u*) # user
211 userDir="$HOME/.${WM_PROJECT:-OpenFOAM}"
212 dirList="$dirList $userDir/$version $userDir"
214 esac
216 case "$mode" in
217 *g*) # group (site)
218 siteDir="${WM_PROJECT_SITE:-$prefixDir/site}"
219 dirList="$dirList $siteDir/$version $siteDir"
221 esac
223 case "$mode" in
224 *o*) # other (shipped)
225 if [ -n "$versionNum" ]
226 then
227 # debian packaging
228 dirList="$dirList $prefixDir/openfoam$versionNum/etc"
229 else
230 # standard packaging
231 dirList="$dirList $prefixDir/${WM_PROJECT:-OpenFOAM}-$version/etc"
234 esac
235 set -- $dirList
239 # The main routine
242 if [ "$listOpt" = true ]
243 then
245 # list directories, or potential file locations
246 [ "$nArgs" -le 1 ] || usage
248 # a silly combination, but -quiet does have precedence
249 [ "$quietOpt" = true ] && exit 0
251 for dir
253 if [ "$nArgs" -eq 1 ]
254 then
255 echo "$dir/$fileName"
256 else
257 echo "$dir"
259 done
260 exit 0
262 else
264 [ "$nArgs" -eq 1 ] || usage
266 for dir
268 if [ -f "$dir/$fileName" ]
269 then
270 [ "$quietOpt" = true ] || echo "$dir/$fileName"
271 exit 0
273 done
278 # general error, eg file not found
279 exit 2
281 #------------------------------------------------------------------------------