foamEtcFile 7.25 KB
Newer Older
Henry's avatar
Henry committed
1
2
3
4
5
#!/bin/sh
#------------------------------------------------------------------------------
# =========                 |
# \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
#  \\    /   O peration     |
Henry Weller's avatar
Henry Weller committed
6
#   \\  /    A nd           | Copyright (C) 2011-2016 OpenFOAM Foundation
mark's avatar
mark committed
7
#    \\/     M anipulation  | Copyright (C) 2017 OpenCFD Ltd.
Henry's avatar
Henry committed
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#-------------------------------------------------------------------------------
# License
#     This file is part of OpenFOAM.
#
#     OpenFOAM is free software: you can redistribute it and/or modify it
#     under the terms of the GNU General Public License as published by
#     the Free Software Foundation, either version 3 of the License, or
#     (at your option) any later version.
#
#     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
#     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
#     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
#     for more details.
#
#     You should have received a copy of the GNU General Public License
#     along with OpenFOAM.  If not, see <http://www.gnu.org/licenses/>.
#
# Script
#     foamEtcFile
#
# Description
#     Locate user/group/shipped file with semantics similar to the
#     ~OpenFOAM/fileName expansion.
#
#     The -mode option can be used to allow chaining from
#     personal settings to site-wide settings.
#
#     For example, within the user ~/.OpenFOAM/<VER>/prefs.sh:
#     \code
mark's avatar
mark committed
37
#        foamFile=$(foamEtcFile -mode go prefs.sh) && . $foamFile
Henry's avatar
Henry committed
38
39
40
#     \endcode
#
# Note
41
42
#     This script must exist in $FOAM_INST_DIR/OpenFOAM-<VERSION>/bin/
#     or $FOAM_INST_DIR/openfoam<VERSION>/bin/ (for the debian version)
Henry's avatar
Henry committed
43
44
#
#-------------------------------------------------------------------------------
mark's avatar
mark committed
45
unset optQuiet optSilent
Henry's avatar
Henry committed
46
47
48
49
50
51
52
53
54
55
usage() {
    [ "${optQuiet:-$optSilent}" = true ] && exit 1

    exec 1>&2
    while [ "$#" -ge 1 ]; do echo "$1"; shift; done
    cat<<USAGE

Usage: ${0##*/} [OPTION] fileName
       ${0##*/} [OPTION] -list
options:
mark's avatar
mark committed
56
57
58
59
60
61
62
  -a, -all          return all files (otherwise stop after the first match)
  -l, -list         list the directories to be searched
  -m, -mode MODE    any combination of u(user), g(group), o(other)
  -p, -prefix DIR   specify an alternative installation prefix
  -q, -quiet        suppress all normal output
  -s, -silent       suppress all stderr output
  -v, -version VER  specify an alternative OpenFOAM version (eg, 3.0, 1612, ...)
Henry's avatar
Henry committed
63
64
65
66
67
  -help             print the usage

  Locate user/group/shipped file with semantics similar to the
  ~OpenFOAM/fileName expansion.

mark's avatar
mark committed
68
  Many options can be specified as a single character, but must not be grouped.
Henry's avatar
Henry committed
69
70
71
72
73
74
75
76
77
78
79

  Exit status
      0  when the file is found. Print resolved path to stdout.
      1  for miscellaneous errors.
      2  when the file is not found.

USAGE
    exit 1
}
#-------------------------------------------------------------------------------

mark's avatar
mark committed
80
# The bin dir:
Henry's avatar
Henry committed
81
82
binDir="${0%/*}"

mark's avatar
mark committed
83
# The project dir:
Henry's avatar
Henry committed
84
85
projectDir="${binDir%/bin}"

mark's avatar
mark committed
86
# The prefix dir (same as $FOAM_INST_DIR):
Henry's avatar
Henry committed
87
88
prefixDir="${projectDir%/*}"

mark's avatar
mark committed
89
# The name used for the project directory
Henry's avatar
Henry committed
90
91
projectDirName="${projectDir##*/}"

mark's avatar
mark committed
92
93
# versionNum used for debian packaging
unset version versionNum
Henry's avatar
Henry committed
94
95

#
mark's avatar
mark committed
96
97
# Handle standard and debian naming conventions
# - set version (always) and versionNum (debian only)
Henry's avatar
Henry committed
98
99
#
case "$projectDirName" in
mark's avatar
mark committed
100
OpenFOAM-*)         # standard naming: OpenFOAM-<VERSION>
Henry's avatar
Henry committed
101
102
103
    version="${projectDirName##OpenFOAM-}"
    ;;

mark's avatar
mark committed
104
openfoam[0-9]* | openfoam-dev)     # debian naming: openfoam<VERSION>
Henry's avatar
Henry committed
105
    versionNum="${projectDirName##openfoam}"
mark's avatar
mark committed
106
107
108
109
    case "${#versionNum}" in
    (2|3|4) # Convert digits version number to decimal delineated
        version=$(echo "$versionNum" | sed -e 's@\([0-9]\)@\1.@g')
        version="${version%.}"
Henry's avatar
Henry committed
110
        ;;
mark's avatar
mark committed
111
112

    (*) # Fallback - use current environment setting
Henry's avatar
Henry committed
113
114
115
116
117
118
        version="$WM_PROJECT_VERSION"
        ;;
    esac
    ;;

*)
mark's avatar
mark committed
119
    echo "${0##*/} Error : unknown/unsupported naming convention" 1>&2
Henry's avatar
Henry committed
120
121
122
123
124
    exit 1
    ;;
esac


mark's avatar
mark committed
125
# Default mode is always 'ugo'
Henry's avatar
Henry committed
126
mode=ugo
mark's avatar
mark committed
127
unset optAll optList
Henry's avatar
Henry committed
128
129
130
131
132
133
134
135
136
137
138
139
140
141

# parse options
while [ "$#" -gt 0 ]
do
    case "$1" in
    -h | -help)
        usage
        ;;
    -a | -all)
        optAll=true
        ;;
    -l | -list)
        optList=true
        ;;
mark's avatar
mark committed
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
    -mode=[ugo]*)
        mode="${1#-mode=}"
        ;;
    -prefix=/*)
        prefixDir="${1#-prefix=}"
        prefixDir="${prefixDir%/}"
        ;;
    -version=*)
        version="${1#-version=}"
        # convert x.y.z -> xyz version (if installation looked like debian)
        if [ -n "$versionNum" ]
        then
            versionNum=$(echo "$version" | sed -e 's@\.@@g')
        fi
        ;;
Henry's avatar
Henry committed
157
158
    -m | -mode)
        mode="$2"
mark's avatar
mark committed
159
        # Sanity check. Handles missing argument too.
Henry's avatar
Henry committed
160
        case "$mode" in
mark's avatar
mark committed
161
162
        [ugo]*)
            ;;
Henry's avatar
Henry committed
163
        *)
mark's avatar
mark committed
164
165
            usage "invalid mode '$mode'"
            ;;
Henry's avatar
Henry committed
166
167
168
169
170
        esac
        shift
        ;;
    -p | -prefix)
        [ "$#" -ge 2 ] || usage "'$1' option requires an argument"
mark's avatar
mark committed
171
        prefixDir="${2%/}"
Henry's avatar
Henry committed
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
        shift
        ;;
    -q | -quiet)
        optQuiet=true
        ;;
    -s | -silent)
        optSilent=true
        ;;
    -v | -version)
        [ "$#" -ge 2 ] || usage "'$1' option requires an argument"
        version="$2"
        # convert x.y.z -> xyz version (if installation looked like debian)
        if [ -n "$versionNum" ]
        then
            versionNum=$(echo "$version" | sed -e 's@\.@@g')
        fi
        shift
        ;;
    --)
        shift
        break
        ;;
    -*)
mark's avatar
mark committed
195
        usage "unknown option: '$1'"
Henry's avatar
Henry committed
196
197
198
199
200
201
202
203
        ;;
    *)
        break
        ;;
    esac
    shift
done

mark's avatar
mark committed
204
205
206
207
208
209
210
211
# Update projectDir accordingly
if [ -n "$versionNum" ]
then
    projectDir="$prefixDir/openfoam$versionNum"                 # debian
else
    projectDir="$prefixDir/${WM_PROJECT:-OpenFOAM}-$version"    # standard
fi

Henry's avatar
Henry committed
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227

# debugging:
# echo "Installed locations:"
# for i in projectDir prefixDir projectDirName version versionNum
# do
#     eval echo "$i=\$$i"
# done


# Save the essential bits of information
# silently remove leading ~OpenFOAM/ (used in Foam::findEtcFile)
nArgs=$#
fileName="${1#~OpenFOAM/}"

# Define the various places to be searched:
unset dirList
mark's avatar
mark committed
228
229
230
case "$mode" in (*u*)   # user
    dir="$HOME/.${WM_PROJECT:-OpenFOAM}"
    dirList="$dirList $dir/$version $dir"
Henry's avatar
Henry committed
231
232
233
    ;;
esac

mark's avatar
mark committed
234
235
236
case "$mode" in (*g*)   # group (site)
    dir="${WM_PROJECT_SITE:-$prefixDir/site}"
    dirList="$dirList $dir/$version $dir"
Henry's avatar
Henry committed
237
238
239
    ;;
esac

mark's avatar
mark committed
240
241
case "$mode" in (*o*)   # other (shipped)
    dirList="$dirList $projectDir/etc"
Henry's avatar
Henry committed
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
    ;;
esac
set -- $dirList


#
# The main routine
#

exitCode=0
if [ "$optList" = true ]
then

    # list directories, or potential file locations
    [ "$nArgs" -le 1 ] || usage

    # a silly combination, but -quiet does have precedence
    [ "$optQuiet" = true ] && exit 0

    for dir
    do
        if [ "$nArgs" -eq 1 ]
        then
            echo "$dir/$fileName"
        else
            echo "$dir"
        fi
    done

else

    [ "$nArgs" -eq 1 ] || usage

    # general error, eg file not found
    exitCode=2

    for dir
    do
        if [ -f "$dir/$fileName" ]
        then
            exitCode=0
mark's avatar
mark committed
283
284
285
286
            [ "$optQuiet" = true ] && break

            echo "$dir/$fileName"
            [ "$optAll" = true ] || break
Henry's avatar
Henry committed
287
288
289
290
291
292
293
294
295
        fi
    done

fi


exit $exitCode

#------------------------------------------------------------------------------