1#!/bin/bash
2
3# Start the CoCoAInterpreter supplying the value of "--packageDir".
4
5# This script expects to find the executable(s) in the directory bin/.
6# There are two possible arrangements:
7# (1) bin/ contains just CoCoAInterpreter (and no other files)
8# (2) bin/ contains CoCoAInterpreter-with-readline & CoCoAInterpreter-without-readline
9#     (and no other files)
10# In case (2) the script tries first with readline, and if that fails then without.
11
12
13#############################################################################
14# Find directory of this script (taken from link below)
15# http://stackoverflow.com/questions/4774054/reliable-way-for-a-bash-script-to-get-the-full-path-to-itself
16
17pushd . > /dev/null
18SCRIPT_PATH="${BASH_SOURCE[0]}"
19while [ -L "$SCRIPT_PATH" ]
20do
21  cd "`dirname "$SCRIPT_PATH"`"
22  SCRIPT_PATH="$(readlink "`basename "$SCRIPT_PATH"`")"
23done
24cd "`dirname "$SCRIPT_PATH"`" > /dev/null
25SCRIPT_DIR="`pwd`"
26popd  > /dev/null
27
28
29# At this point SCRIPT_DIR contains the path to the directory
30# where the CoCoAInterpreter executable and also packages/ subdir.
31
32##################################################################
33### executable
34
35### for released CoCoA (as in release-files/): CoCoAInterpreter is in bin/
36
37# OBSOLESCENT???
38# # Check platform is 64 bit
39# SIXTYFOUR=`uname -a | fgrep x86_64`
40# if [ -z "$SIXTYFOUR" ]
41# then
42#   echo "$0: 32-bit version has been withdrawn: please write to cocoa@dima.unige.it"
43#   exit 1
44# fi
45
46PKG_DIR="$SCRIPT_DIR/packages"
47COCOA_PATH="$SCRIPT_DIR/bin"
48COCOA_EXECUTABLE="$COCOA_PATH/CoCoAInterpreter"
49COCOA_WITH_READLINE="$COCOA_PATH/CoCoAInterpreter-with-readline"
50COCOA_WITHOUT_READLINE="$COCOA_PATH/CoCoAInterpreter-without-readline"
51
52# Check that binaries exist and are executable (sorry this is messy)
53COUNT=`(cd "$COCOA_PATH"; /bin/ls | wc -l )`
54COCOA_EXECUTABLE_CHECK=ok
55if [ $COUNT = 0 -o $COUNT -gt 2 ]
56then
57    COCOA_EXECUTABLE_CHECK=wrong-files
58elif [ $COUNT = 1 ]
59then
60    if [ -f "$COCOA_EXECUTABLE" -a \! -x "$COCOA_EXECUTABLE" ]
61    then
62	COCOA_EXECUTABLE_CHECK=not-exec
63    fi
64elif [ -f "$COCOA_WITH_READLINE" -a \! -x "$COCOA_WITH_READLINE" ]
65then
66    COCOA_EXECUTABLE_CHECK=not-exec
67elif [ -f "$COCOA_WITHOUT_READLINE" -a \! -x "$COCOA_WITHOUT_READLINE" ]
68then
69    COCOA_EXECUTABLE_CHECK=not-exec
70fi
71
72if [ "$COCOA_EXECUTABLE_CHECK" = "wrong-files" ]
73then
74    sleep 1 # Useful if running inside emacs (see redmine 1352)
75    echo ""  >/dev/stderr
76    echo "=========================================================================="  >/dev/stderr
77    echo ">>> ERROR: COCOA-5 BINARY MISSING OR UNEXPECTED FILES IN bin DIRECTORY <<<"  >/dev/stderr
78    echo "=========================================================================="  >/dev/stderr
79    sleep 1
80    if [ $COUNT -lt 2 ]
81    then
82	echo "HINT: Expected to find \`CoCoAInterpreter' but found" >/dev/stderr
83    else
84	echo "HINT: Expected to find \`CoCoAInterpreter-with-readline'" >/dev/stderr
85	echo "HINT: and \`CoCoAInterpreter-no-readline' but instead found" >/dev/stderr
86    fi
87    echo "(listing of directory $COCOA_PATH)"              >/dev/stderr
88    ( cd "$COCOA_PATH"; /bin/ls -l )                       >/dev/stderr
89    sleep 4
90    exit 1
91fi
92
93if [ "$COCOA_EXECUTABLE_CHECK" = "not-exec" ]
94then
95    sleep 1 # Useful if running inside emacs (see redmine 1352)
96    echo ""  >/dev/stderr
97    echo "============================================"  >/dev/stderr
98    echo ">>> ERROR: COCOA-5 BINARY NOT EXECUTABLE <<<"  >/dev/stderr
99    echo "============================================"  >/dev/stderr
100    sleep 1
101    echo "(listing of directory $COCOA_PATH)"              >/dev/stderr
102    ( cd "$COCOA_PATH"; /bin/ls -l )                      >/dev/stderr
103    sleep 4
104    exit 1
105fi
106
107
108#######################################################
109# Check that package dir exists
110
111if [ \! -d "$PKG_DIR" ]
112then
113    sleep 1 # Useful if running inside emacs (see redmine 1352)
114    echo ""                                           >/dev/stderr
115    echo "======================================="      >/dev/stderr
116    echo ">>> ERROR: COCOA-5 PACKAGES MISSING <<<"      >/dev/stderr
117    echo "======================================="      >/dev/stderr
118    sleep 1
119    echo "Expected CoCoA-5 package directory to be:"    >/dev/stderr
120    echo "$PKG_DIR/"                                    >/dev/stderr
121    echo                                                >/dev/stderr
122    echo "Exiting..."                                   >/dev/stderr
123    sleep 4
124    exit 1
125fi
126
127#############################################################################
128# A simple sanity check that this script has been correctly set up.
129
130if [ -f "$COCOA_EXECUTABLE" ]
131then
132    "$COCOA_EXECUTABLE" --do-nothing  > /dev/null 2>&1
133    if [ $? -ne 0 ]
134    then
135	sleep 1 # Useful if running inside emacs (see redmine 1352)
136	echo ""                                              >/dev/stderr
137	echo "============================================"    >/dev/stderr
138	echo ">>> ERROR: COCOA-5 EXECUTABLE CANNOT RUN <<<"    >/dev/stderr
139	echo "============================================"    >/dev/stderr
140	sleep 4
141	exit 1
142    fi
143    exec "$COCOA_EXECUTABLE" --packageDir "$SCRIPT_DIR/packages" "$@"
144fi
145
146# Now we are in the case where there should be two executables with and without readline
147# Try first with readline; then without; complain if both fail.
148
149"$COCOA_WITH_READLINE" --do-nothing  > /dev/null 2>&1
150if [ $? = 0 ]
151then
152    exec "$COCOA_WITH_READLINE" --packageDir "$SCRIPT_DIR/packages" "$@"
153fi
154
155"$COCOA_WITHOUT_READLINE" --do-nothing  > /dev/null 2>&1
156if [ $? = 0 ]
157then
158        exec "$COCOA_WITHOUT_READLINE" --packageDir "$SCRIPT_DIR/packages" "$@"
159fi
160
161# Neither executable worked  -->  REPORT ERROR
162sleep 1 # Useful if running inside emacs (see redmine 1352)
163echo ""                                              >/dev/stderr
164echo "============================================"    >/dev/stderr
165echo ">>> ERROR: COCOA-5 EXECUTABLE CANNOT RUN <<<"    >/dev/stderr
166echo "============================================"    >/dev/stderr
167sleep 1
168echo "Looked in directory $COCOA_PATH"                 >/dev/stderr
169echo "Neither executable worked  :-("                  >/dev/stderr
170echo                                                   >/dev/stderr
171echo "Exiting..."                                      >/dev/stderr
172sleep 4
173exit 1
174