foamEtcFile 8.02 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
  -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
61
62
63
64
65
66
  -s, -silent       suppress stderr output, except for things that are emitted
                    by -csh-verbose, -sh-verbose.
  -v, -version VER  specify alternative OpenFOAM version (eg, 3.0, 1612, ...)
  -csh | -sh        produce output suitable for a csh or sh 'eval'
  -csh-verbose,
  -sh-verbose       with additional verbosity
Henry's avatar
Henry committed
67
68
69
70
71
  -help             print the usage

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

mark's avatar
mark committed
72
  Many options can be specified as a single character, but must not be grouped.
Henry's avatar
Henry committed
73
74
75
76
77
78
79
80
81
82
83

  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
84
# The bin dir:
Henry's avatar
Henry committed
85
86
binDir="${0%/*}"

mark's avatar
mark committed
87
# The project dir:
Henry's avatar
Henry committed
88
89
projectDir="${binDir%/bin}"

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

mark's avatar
mark committed
93
# The name used for the project directory
Henry's avatar
Henry committed
94
95
projectDirName="${projectDir##*/}"

mark's avatar
mark committed
96
97
# versionNum used for debian packaging
unset version versionNum
Henry's avatar
Henry committed
98
99

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

mark's avatar
mark committed
108
openfoam[0-9]* | openfoam-dev)     # debian naming: openfoam<VERSION>
Henry's avatar
Henry committed
109
    versionNum="${projectDirName##openfoam}"
mark's avatar
mark committed
110
111
112
113
    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
114
        ;;
mark's avatar
mark committed
115
116

    (*) # Fallback - use current environment setting
Henry's avatar
Henry committed
117
118
119
120
121
122
        version="$WM_PROJECT_VERSION"
        ;;
    esac
    ;;

*)
mark's avatar
mark committed
123
    echo "${0##*/} Error : unknown/unsupported naming convention" 1>&2
Henry's avatar
Henry committed
124
125
126
127
128
    exit 1
    ;;
esac


mark's avatar
mark committed
129
# Default mode is always 'ugo'
Henry's avatar
Henry committed
130
mode=ugo
131
unset optAll optList optShell
Henry's avatar
Henry committed
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
142
        unset optShell
Henry's avatar
Henry committed
143
144
145
        ;;
    -l | -list)
        optList=true
146
147
148
149
150
        unset optShell
        ;;
    -csh | -sh | -csh-verbose | -sh-verbose)
        optShell="${1#-}"
        unset optAll
Henry's avatar
Henry committed
151
        ;;
mark's avatar
mark committed
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
    -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
167
168
    -m | -mode)
        mode="$2"
mark's avatar
mark committed
169
        # Sanity check. Handles missing argument too.
Henry's avatar
Henry committed
170
        case "$mode" in
mark's avatar
mark committed
171
172
        [ugo]*)
            ;;
Henry's avatar
Henry committed
173
        *)
mark's avatar
mark committed
174
175
            usage "invalid mode '$mode'"
            ;;
Henry's avatar
Henry committed
176
177
178
179
180
        esac
        shift
        ;;
    -p | -prefix)
        [ "$#" -ge 2 ] || usage "'$1' option requires an argument"
mark's avatar
mark committed
181
        prefixDir="${2%/}"
Henry's avatar
Henry committed
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
        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
205
        usage "unknown option: '$1'"
Henry's avatar
Henry committed
206
207
208
209
210
211
212
213
        ;;
    *)
        break
        ;;
    esac
    shift
done

mark's avatar
mark committed
214
215
216
217
218
219
220
221
# 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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
# 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
237
238
239
case "$mode" in (*u*)   # user
    dir="$HOME/.${WM_PROJECT:-OpenFOAM}"
    dirList="$dirList $dir/$version $dir"
Henry's avatar
Henry committed
240
241
242
    ;;
esac

mark's avatar
mark committed
243
244
245
case "$mode" in (*g*)   # group (site)
    dir="${WM_PROJECT_SITE:-$prefixDir/site}"
    dirList="$dirList $dir/$version $dir"
Henry's avatar
Henry committed
246
247
248
    ;;
esac

mark's avatar
mark committed
249
250
case "$mode" in (*o*)   # other (shipped)
    dirList="$dirList $projectDir/etc"
Henry's avatar
Henry committed
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
    ;;
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
268
    [ -n "$optQuiet" ] && exit 0
Henry's avatar
Henry committed
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291

    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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
            [ -n "$optQuiet" ] && break

            case "$optShell" in
            (*verbose)
                echo "Using: $dir/$fileName" 1>&2
                ;;
            esac

            case "$optShell" in
            csh*)
                echo "source $dir/$fileName"
                break
                ;;
            sh*)
                echo ". $dir/$fileName"
                break
                ;;
            *)
                echo "$dir/$fileName"
                [ -n "$optAll" ] || break
                ;;
            esac
Henry's avatar
Henry committed
314
315
316
317
318
319
320
321
322
        fi
    done

fi


exit $exitCode

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