aboutsummaryrefslogtreecommitdiff
path: root/Bin/kranc
blob: 0ce5f20287c6a43ed69c3d2e20411bb2e439f172 (plain)
1
2
3
4
5
6
7
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#!/bin/bash

set -e

# Assume that this script is called from the Kranc/Bin directory.
# This will not work if someone creates a symlink to the kranc script
# somewhere else
export KRANCDIR=$(dirname $0)/..

if [ ! -r "$KRANCDIR/Tools/CodeGen" ]; then
    echo "Cannot find Kranc (the kranc script must be run directly from the Kranc/Bin directory - symbolic links are not currently allowed)"
    exit 1
fi

export KRANCVERBOSE=no

echo "Using Kranc installation at $KRANCDIR"
# It would be good to find a portable way to canonicalise KRANCDIR.

while getopts "v" flag
do
  case $flag in
      "v")
	  export KRANCVERBOSE=yes
	  ;;
  esac
done

shift $(expr $OPTIND - 1)

if [ $# -eq 0 ]
then
    echo "Usage: $0 [-v] <script>"
    exit 1
fi

MMASCRIPT=$1
shift

MATH_MACOS=/Applications/Mathematica.app/Contents/MacOS/MathKernel

if which math >/dev/null; then
    MATH=math
elif [ -x $MATH_MACOS ]; then
    MATH=$MATH_MACOS
else
    echo "Cannot find math executable.  Is Mathematica on your path?"
    exit 1
fi

# If there is no licence server, Mathematica still exits with a 0 exit
# code. In order to catch this error and exit with a nonzero code, we
# first create a temporary directory, then remove it from within
# Mathematica, then check to see that this happened.  If it didn't, we
# assume there was some problem with launching Mathematica.

flagdir="kranc.tmp.$$" # mktemp is not the same on Linux and Mac OS; here we assume we can write to the current directory
mkdir $flagdir

if $MATH -run "DeleteDirectory[\"$flagdir\"]; Get[\"$KRANCDIR/Tools/MathematicaMisc/RunKranc.m\"]" $MMASCRIPT </dev/null; then
    if [ ! -d "$flagdir" ]; then
        # Mathematica kernel started and executed DeleteDirectory
        exit 0
    else
        echo "Mathematica did not start successfully"
        rmdir "$flagdir"
        exit 1
    fi
else
    code=$?
    if [ -d "$flagdir" ]; then
         rmdir "$flagdir"
         exit $code
    fi
    exit $code
fi