#!/bin/sh

# In the CoCoALib-0.99800 release some files necessary for building the GUI
# are missing. See https://cocoa.dima.unige.it/redmine/issues/1393#note-13
# This script copies these files from the previous release 0.99718 and patches
# them for Qt5 support.
#
# Executing this script in an empty folder will download both releases of
# CoCoALib (0.99718 and 0.99800); releases already present will not be
# downloaded again.
#
# Additionally, various patches to the build system are applied, also
# described in https://cocoa.dima.unige.it/redmine/issues/1393
#
# The files in the folder CoCoALib-0.99800_patched will afterwards be patched.
#
# The patched configure script automatically determines if the Qt5 GUI can be
# built. For building without the GUI pass the option --no-qt-gui to the
# configure script.

VER1='0.99718'
VER2='0.99800'
VERS="$VER1 $VER2"

COCOALIB_VER1_DIR="CoCoALib-$VER1"
COCOALIB_VER2_DIR="CoCoALib-$VER2"
COCOALIB_PATCHED_DIR="${COCOALIB_VER2_DIR}_patched"
DIRS="$COCOALIB_VER1_DIR $COCOALIB_VER2_DIR $COCOALIB_PATCHED_DIR"

COCOALIB_VER1_SHA256=\
'1b822387c4482c1e81b1d060abca726a27507702c7c642c10c094527fc0e7bbc'
COCOALIB_VER2_SHA256=\
'f8bb227e2e1729e171cf7ac2008af71df25914607712c35db7bcb5a044a928c6'

# Check if directory is clean enough:
printf '%s' "Checking directory... "
for dir in $DIRS; do
    if [ -e "$dir" ]; then
        printf '\n%s\n' "Error: $dir already exists, exiting" > /dev/stderr
        exit 1
    fi
done
printf '%s\n' "done"

SHA256PRG="$(command -v sha256sum || command -v sha256)"

# Download CoCoALib and check SHA256 sums:
for i in $VERS; do
    if [ "$i" = "$VER1" ]; then
        SHA256SUM="$COCOALIB_VER1_SHA256"
    elif [ "$i" = "$VER2" ]; then
        SHA256SUM="$COCOALIB_VER2_SHA256"
    else
        printf '%s\n' "An error occured: \$i = $i" > /dev/stderr
        exit 1
    fi
    if [ ! -e "CoCoALib-$i.tgz" ]; then
        printf '%s' "Downloading CoCoALib-$i.tgz... "
        wget -q "https://cocoa.dima.unige.it/cocoa/cocoalib/tgz/CoCoALib-$i.tgz"
        printf '%s\n' "done"
    fi
    printf '%s' "Verifying CoCoALib-$i.tgz... "
    if ! { "$SHA256PRG" -c <<- EOF
	$SHA256SUM  CoCoALib-$i.tgz
	EOF
        } > /dev/null; then
        printf '\n%s\n' "Error: CoCoALib-$i.tgz invalid" > /dev/stderr
        exit 1
    fi
    printf '%s\n' "done"
done

# Unpack CoCoALib:
for i in $VERS; do
    printf '%s' "Unpacking CoCoALib-$i... "
    tar xzf "CoCoALib-$i.tgz"
    printf '%s\n' "done"
done

# Copy the missing files from the old release to the new one:
printf '%s' "Copying files... "
SRC="$COCOALIB_VER1_DIR"
DST="$COCOALIB_VER2_DIR"

mkdir -p "$DST/src/CoCoA-5"
cp -p \
"$SRC/src/CoCoA-5/C5.pro.in" \
"$SRC/src/CoCoA-5/C5.qrc" \
"$SRC/src/CoCoA-5/Console.ui" \
"$SRC/src/CoCoA-5/Debugger.ui" \
"$SRC/src/CoCoA-5/MainWindow.ui" \
"$SRC/src/CoCoA-5/make-c5makefile.sh" \
"$SRC/src/CoCoA-5/make-gui-finish.sh" \
"$SRC/src/CoCoA-5/make-qcodeeditmakefile.sh" \
"$SRC/src/CoCoA-5/SourceEditor.ui" \
"$DST/src/CoCoA-5"

mkdir -p "$DST/src/CoCoA-5/images"
cp -p \
"$SRC/src/CoCoA-5/images/applications-graphics.png" \
"$SRC/src/CoCoA-5/images/CoCoALogo-icon.png" \
"$SRC/src/CoCoA-5/images/dialog-information.png" \
"$SRC/src/CoCoA-5/images/document-new.png" \
"$SRC/src/CoCoA-5/images/document-open.png" \
"$SRC/src/CoCoA-5/images/document-save-as.png" \
"$SRC/src/CoCoA-5/images/document-save.png" \
"$SRC/src/CoCoA-5/images/emblem-system.png" \
"$SRC/src/CoCoA-5/images/go-jump.png" \
"$SRC/src/CoCoA-5/images/go-last.png" \
"$SRC/src/CoCoA-5/images/go-next.png" \
"$SRC/src/CoCoA-5/images/go-top.png" \
"$SRC/src/CoCoA-5/images/go-up.png" \
"$SRC/src/CoCoA-5/images/help-browser.png" \
"$SRC/src/CoCoA-5/images/preferences-desktop-font.png" \
"$SRC/src/CoCoA-5/images/preferences-desktop-keyboard-shortcuts.png" \
"$SRC/src/CoCoA-5/images/preferences-system.png" \
"$SRC/src/CoCoA-5/images/preferences-system-windows.png" \
"$SRC/src/CoCoA-5/images/process-stop.png" \
"$SRC/src/CoCoA-5/images/system-log-out.png" \
"$SRC/src/CoCoA-5/images/utilities-system-monitor.png" \
"$SRC/src/CoCoA-5/images/utilities-terminal.png" \
"$DST/src/CoCoA-5/images"

mkdir -p "$DST/src/CoCoA-5/QCodeEdit/document"
cp -p \
"$SRC/src/CoCoA-5/QCodeEdit/document/qdocumentbuffer.cpp" \
"$SRC/src/CoCoA-5/QCodeEdit/document/qdocumentbuffer.h" \
"$SRC/src/CoCoA-5/QCodeEdit/document/qdocumentcommand.cpp" \
"$SRC/src/CoCoA-5/QCodeEdit/document/qdocumentcommand.h" \
"$SRC/src/CoCoA-5/QCodeEdit/document/qdocument.cpp" \
"$SRC/src/CoCoA-5/QCodeEdit/document/qdocumentcursor.cpp" \
"$SRC/src/CoCoA-5/QCodeEdit/document/qdocumentcursor.h" \
"$SRC/src/CoCoA-5/QCodeEdit/document/qdocumentcursor_p.h" \
"$SRC/src/CoCoA-5/QCodeEdit/document/qdocument.h" \
"$SRC/src/CoCoA-5/QCodeEdit/document/qdocumentline.cpp" \
"$SRC/src/CoCoA-5/QCodeEdit/document/qdocumentline.h" \
"$SRC/src/CoCoA-5/QCodeEdit/document/qdocumentline_p.h" \
"$SRC/src/CoCoA-5/QCodeEdit/document/qdocument_p.h" \
"$SRC/src/CoCoA-5/QCodeEdit/document/qdocumentsearch.cpp" \
"$SRC/src/CoCoA-5/QCodeEdit/document/qdocumentsearch.h" \
"$DST/src/CoCoA-5/QCodeEdit/document"

mkdir -p "$DST/src/CoCoA-5/QCodeEdit/images"
cp -p \
"$SRC/src/CoCoA-5/QCodeEdit/images/bold.png" \
"$SRC/src/CoCoA-5/QCodeEdit/images/closeall.png" \
"$SRC/src/CoCoA-5/QCodeEdit/images/comment.png" \
"$SRC/src/CoCoA-5/QCodeEdit/images/copy.png" \
"$SRC/src/CoCoA-5/QCodeEdit/images/cut.png" \
"$SRC/src/CoCoA-5/QCodeEdit/images/down.png" \
"$SRC/src/CoCoA-5/QCodeEdit/images/editor.png" \
"$SRC/src/CoCoA-5/QCodeEdit/images/Edyuk.qrc" \
"$SRC/src/CoCoA-5/QCodeEdit/images/exit.png" \
"$SRC/src/CoCoA-5/QCodeEdit/images/fillcolor.png" \
"$SRC/src/CoCoA-5/QCodeEdit/images/find.png" \
"$SRC/src/CoCoA-5/QCodeEdit/images/formats.png" \
"$SRC/src/CoCoA-5/QCodeEdit/images/goto.png" \
"$SRC/src/CoCoA-5/QCodeEdit/images/indent.png" \
"$SRC/src/CoCoA-5/QCodeEdit/images/italic.png" \
"$SRC/src/CoCoA-5/QCodeEdit/images/new.png" \
"$SRC/src/CoCoA-5/QCodeEdit/images/open.png" \
"$SRC/src/CoCoA-5/QCodeEdit/images/package_settings.png" \
"$SRC/src/CoCoA-5/QCodeEdit/images/paste.png" \
"$SRC/src/CoCoA-5/QCodeEdit/images/print.png" \
"$SRC/src/CoCoA-5/QCodeEdit/images/redo.png" \
"$SRC/src/CoCoA-5/QCodeEdit/images/reload.png" \
"$SRC/src/CoCoA-5/QCodeEdit/images/replace.png" \
"$SRC/src/CoCoA-5/QCodeEdit/images/saveas.png" \
"$SRC/src/CoCoA-5/QCodeEdit/images/save.png" \
"$SRC/src/CoCoA-5/QCodeEdit/images/settings.png" \
"$SRC/src/CoCoA-5/QCodeEdit/images/strikeout.png" \
"$SRC/src/CoCoA-5/QCodeEdit/images/strokecolor.png" \
"$SRC/src/CoCoA-5/QCodeEdit/images/textcolor.png" \
"$SRC/src/CoCoA-5/QCodeEdit/images/uncomment.png" \
"$SRC/src/CoCoA-5/QCodeEdit/images/underline.png" \
"$SRC/src/CoCoA-5/QCodeEdit/images/undo.png" \
"$SRC/src/CoCoA-5/QCodeEdit/images/unindent.png" \
"$SRC/src/CoCoA-5/QCodeEdit/images/up.png" \
"$DST/src/CoCoA-5/QCodeEdit/images"

mkdir -p "$DST/src/CoCoA-5/QCodeEdit/qnfa"
cp -p \
"$SRC/src/CoCoA-5/QCodeEdit/qnfa/light_vector.h" \
"$SRC/src/CoCoA-5/QCodeEdit/qnfa/qnfa.cpp" \
"$SRC/src/CoCoA-5/QCodeEdit/qnfa/qnfadefinition.cpp" \
"$SRC/src/CoCoA-5/QCodeEdit/qnfa/qnfadefinition.h" \
"$SRC/src/CoCoA-5/QCodeEdit/qnfa/qnfa.h" \
"$SRC/src/CoCoA-5/QCodeEdit/qnfa/xml2qnfa.cpp" \
"$DST/src/CoCoA-5/QCodeEdit/qnfa"

mkdir -p "$DST/src/CoCoA-5/QCodeEdit/snippets"
cp -p \
"$SRC/src/CoCoA-5/QCodeEdit/snippets/qsnippetbinding.cpp" \
"$SRC/src/CoCoA-5/QCodeEdit/snippets/qsnippetbinding.h" \
"$SRC/src/CoCoA-5/QCodeEdit/snippets/qsnippet.cpp" \
"$SRC/src/CoCoA-5/QCodeEdit/snippets/qsnippetedit.cpp" \
"$SRC/src/CoCoA-5/QCodeEdit/snippets/qsnippetedit.h" \
"$SRC/src/CoCoA-5/QCodeEdit/snippets/qsnippet.h" \
"$SRC/src/CoCoA-5/QCodeEdit/snippets/qsnippetmanager.cpp" \
"$SRC/src/CoCoA-5/QCodeEdit/snippets/qsnippetmanager.h" \
"$SRC/src/CoCoA-5/QCodeEdit/snippets/qsnippetpatternloader.h" \
"$SRC/src/CoCoA-5/QCodeEdit/snippets/qsnippet_p.h" \
"$SRC/src/CoCoA-5/QCodeEdit/snippets/snippetedit.ui" \
"$DST/src/CoCoA-5/QCodeEdit/snippets"

mkdir -p "$DST/src/CoCoA-5/QCodeEdit/widgets"
cp -p \
"$SRC/src/CoCoA-5/QCodeEdit/widgets/editconfig.ui" \
"$SRC/src/CoCoA-5/QCodeEdit/widgets/formatconfig.ui" \
"$SRC/src/CoCoA-5/QCodeEdit/widgets/gotolinedialog.ui" \
"$SRC/src/CoCoA-5/QCodeEdit/widgets/gotoline.ui" \
"$SRC/src/CoCoA-5/QCodeEdit/widgets/qcalltip.cpp" \
"$SRC/src/CoCoA-5/QCodeEdit/widgets/qcalltip.h" \
"$SRC/src/CoCoA-5/QCodeEdit/widgets/qeditconfig.cpp" \
"$SRC/src/CoCoA-5/QCodeEdit/widgets/qeditconfig.h" \
"$SRC/src/CoCoA-5/QCodeEdit/widgets/qfoldpanel.cpp" \
"$SRC/src/CoCoA-5/QCodeEdit/widgets/qfoldpanel.h" \
"$SRC/src/CoCoA-5/QCodeEdit/widgets/qformatconfig.cpp" \
"$SRC/src/CoCoA-5/QCodeEdit/widgets/qformatconfig.h" \
"$SRC/src/CoCoA-5/QCodeEdit/widgets/qgotolinedialog.cpp" \
"$SRC/src/CoCoA-5/QCodeEdit/widgets/qgotolinedialog.h" \
"$SRC/src/CoCoA-5/QCodeEdit/widgets/qgotolinepanel.cpp" \
"$SRC/src/CoCoA-5/QCodeEdit/widgets/qgotolinepanel.h" \
"$SRC/src/CoCoA-5/QCodeEdit/widgets/qlinechangepanel.cpp" \
"$SRC/src/CoCoA-5/QCodeEdit/widgets/qlinechangepanel.h" \
"$SRC/src/CoCoA-5/QCodeEdit/widgets/qlinemarkpanel.cpp" \
"$SRC/src/CoCoA-5/QCodeEdit/widgets/qlinemarkpanel.h" \
"$SRC/src/CoCoA-5/QCodeEdit/widgets/qlinenumberpanel.cpp" \
"$SRC/src/CoCoA-5/QCodeEdit/widgets/qlinenumberpanel.h" \
"$SRC/src/CoCoA-5/QCodeEdit/widgets/qpanel.cpp" \
"$SRC/src/CoCoA-5/QCodeEdit/widgets/qpanel.h" \
"$SRC/src/CoCoA-5/QCodeEdit/widgets/qsearchreplacepanel.cpp" \
"$SRC/src/CoCoA-5/QCodeEdit/widgets/qsearchreplacepanel.h" \
"$SRC/src/CoCoA-5/QCodeEdit/widgets/qsimplecolorpicker.cpp" \
"$SRC/src/CoCoA-5/QCodeEdit/widgets/qsimplecolorpicker.h" \
"$SRC/src/CoCoA-5/QCodeEdit/widgets/qstatuspanel.cpp" \
"$SRC/src/CoCoA-5/QCodeEdit/widgets/qstatuspanel.h" \
"$SRC/src/CoCoA-5/QCodeEdit/widgets/searchreplace.ui" \
"$DST/src/CoCoA-5/QCodeEdit/widgets"

mkdir -p "$DST/src/CoCoA-5/qxs"
cp -p \
"$SRC/src/CoCoA-5/qxs/cocoa5.qnfa" \
"$DST/src/CoCoA-5/qxs"
printf '%s\n' "done"

# Apply Qt5 patches to the copied files:
printf '%s' "Applying Qt5 patches to copied files... "
patch -d "$COCOALIB_VER2_DIR" -p1 -s << 'EOFQT5PATCH'
diff -Naur CoCoALib-0.99800/src/CoCoA-5/C5.pro.in CoCoALib-0.99800_gui/src/CoCoA-5/C5.pro.in
--- CoCoALib-0.99800/src/CoCoA-5/C5.pro.in	2019-12-18 09:25:25.000000000 +0100
+++ CoCoALib-0.99800_gui/src/CoCoA-5/C5.pro.in	2022-03-03 20:32:17.711894478 +0100
@@ -1,10 +1,10 @@
 
 #QMAKE_CXXFLAGS += -ansi -Wall -Wextra -pedantic -O0 -g3
 #QMAKE_CXXFLAGS += -ansi -Wall -Wextra -pedantic # for bug in gcc-4.5.2
-QMAKE_CXXFLAGS += -std=c++11 -ansi -Wall -Wextra -pedantic -O2
+QMAKE_CXXFLAGS += -std=c++14 -Wall -pedantic -O2
 QMAKE_MAKEFILE = C5Makefile
 DESTDIR = .
-CONFIG += c++11 debug qt thread
+CONFIG += c++14 debug qt thread
 TEMPLATE = app
 TARGET = C5
 DEPENDPATH += .
@@ -16,7 +16,7 @@
 MOC_DIR = 
 UI_DIR =
 OBJECTS_DIR = Debug
-QT += xml
+QT += xml webkitwidgets printsupport
 HEADERS += AST.H \
            C5.H \
 	   CoCoALibSupplement.H \
@@ -34,6 +34,7 @@
 SOURCES += AST.C \
            BuiltInFunctions.C \
            BuiltInFunctions-CoCoALib.C \
+           BuiltInFunctionsVarArgs-CoCoALib.C \
            BuiltInOneLiners-CoCoALib.C \
            BuiltInFunctions-Frobby.C \
            BuiltInFunctions-GFan.C \
@@ -49,6 +50,7 @@
            Banner.C \
 	   OnlineHelp.C \
            VersionInfo.C \
-           Parser.C 
+           Parser.C \
+           globals.C 
 RESOURCES += C5.qrc
 
diff -Naur CoCoALib-0.99800/src/CoCoA-5/QCodeEdit/document/qdocument.cpp CoCoALib-0.99800_gui/src/CoCoA-5/QCodeEdit/document/qdocument.cpp
--- CoCoALib-0.99800/src/CoCoA-5/QCodeEdit/document/qdocument.cpp	2010-08-23 21:57:39.000000000 +0200
+++ CoCoALib-0.99800_gui/src/CoCoA-5/QCodeEdit/document/qdocument.cpp	2022-02-25 15:35:53.956652017 +0100
@@ -6157,7 +6157,8 @@
 			
 		it = areas.insert(m.line, n);
 		
-		if ( it != areas.end() && (it - 1) != areas.end() )
+		// if ( it != areas.end() && (it - 1) != areas.end() )
+		if ( it != areas.end() && it != areas.begin() )
 		{
 			tmp = it - 1;
 			int off = tmp.key() + *tmp - l;
diff -Naur CoCoALib-0.99800/src/CoCoA-5/QCodeEdit/document/qdocumentsearch.cpp CoCoALib-0.99800_gui/src/CoCoA-5/QCodeEdit/document/qdocumentsearch.cpp
--- CoCoALib-0.99800/src/CoCoA-5/QCodeEdit/document/qdocumentsearch.cpp	2010-08-23 21:57:39.000000000 +0200
+++ CoCoALib-0.99800_gui/src/CoCoA-5/QCodeEdit/document/qdocumentsearch.cpp	2022-02-25 15:35:53.956652017 +0100
@@ -566,7 +566,7 @@
 				{
 					QString replacement = m_replace;
 					
-					for ( int i = m_regexp.numCaptures(); i >= 0; --i )
+					for ( int i = m_regexp.captureCount(); i >= 0; --i )
 						replacement.replace(QString("\\") + QString::number(i),
 											m_regexp.cap(i));
 					
diff -Naur CoCoALib-0.99800/src/CoCoA-5/QCodeEdit/snippets/qsnippetmanager.h CoCoALib-0.99800_gui/src/CoCoA-5/QCodeEdit/snippets/qsnippetmanager.h
--- CoCoALib-0.99800/src/CoCoA-5/QCodeEdit/snippets/qsnippetmanager.h	2010-08-23 21:57:39.000000000 +0200
+++ CoCoALib-0.99800_gui/src/CoCoA-5/QCodeEdit/snippets/qsnippetmanager.h	2022-02-25 15:35:53.960652065 +0100
@@ -23,6 +23,7 @@
 	\brief Definition of the QSnippetManager class
 */
 
+#include <QObject>
 #include <QStringList>
 
 class QSnippet;
EOFQT5PATCH
printf '%s\n' "done"

# Apply patches to the build scripts:
printf '%s' "Applying patches to build scripts... "
patch -d "$COCOALIB_VER2_DIR" -p1 -s << 'EOFBUILDPATCH'
diff -Naur CoCoALib-0.99800_gui/configuration/boost-check-arch.sh CoCoALib-0.99800_patched/configuration/boost-check-arch.sh
--- CoCoALib-0.99800_gui/configuration/boost-check-arch.sh	2022-03-25 16:29:32.000000000 +0000
+++ CoCoALib-0.99800_patched/configuration/boost-check-arch.sh	2022-10-24 06:06:19.074135623 +0000
@@ -9,9 +9,9 @@
 SCRIPT_NAME=[[`basename "$0"`]]
 SCRIPT_DIR=`dirname "$0"`
 
-if [ $# -ne 1 ]
+if [ $# -ne 3 ]
 then
-  echo "ERROR: expecting 1 arg (LIBS for linker)   $SCRIPT_NAME"  > /dev/stderr
+  echo "ERROR: expecting 3 args (INC_DIR, LIB_DIR, and LIBS for linker)   $SCRIPT_NAME"  > /dev/stderr
   exit 1
 fi
 
@@ -21,7 +21,9 @@
   exit 1
 fi
 
-BOOST_LDLIBS="$1"
+BOOST_INC_DIR="$1"
+BOOST_LIB_DIR="$2"
+BOOST_LDLIBS="$3"
 
 # We create a temp dir and work in there.
 umask 22
@@ -42,8 +44,8 @@
 }
 EOF
 
-echo "$CXX $CXXFLAGS  -I\"$COCOA_EXTLIB_DIR/include\" test-boost-arch.C -o test-boost-arch -L\"$COCOA_EXTLIB_DIR/lib\" $BOOST_LDLIBS" > LogFile
-$CXX $CXXFLAGS  -I"$COCOA_EXTLIB_DIR/include" test-boost-arch.C -o test-boost-arch -L"$COCOA_EXTLIB_DIR/lib" $BOOST_LDLIBS  >> LogFile  2>&1
+echo "$CXX $CXXFLAGS  -I\"$COCOA_EXTLIB_DIR/include\" -I\"$BOOST_INC_DIR\" test-boost-arch.C -o test-boost-arch -L\"$COCOA_EXTLIB_DIR/lib\" -L\"$BOOST_LIB_DIR\" $BOOST_LDLIBS" > LogFile
+$CXX $CXXFLAGS  -I"$COCOA_EXTLIB_DIR/include" -I"$BOOST_INC_DIR" test-boost-arch.C -o test-boost-arch -L"$COCOA_EXTLIB_DIR/lib" -L"$BOOST_LIB_DIR" $BOOST_LDLIBS  >> LogFile  2>&1
 if [ $? -ne 0 ]
 then
   echo "ERROR: compilation failed --> see LogFile   $SCRIPT_NAME"   > /dev/stderr
diff -Naur CoCoALib-0.99800_gui/configuration/fixed_part2 CoCoALib-0.99800_patched/configuration/fixed_part2
--- CoCoALib-0.99800_gui/configuration/fixed_part2	2022-03-25 16:29:32.000000000 +0000
+++ CoCoALib-0.99800_patched/configuration/fixed_part2	2022-10-24 06:06:19.074135623 +0000
@@ -7,7 +7,8 @@
 
 COCOA_HDR=$(COCOA_ROOT)/include/CoCoA/library.H
 
-INCLUDE_PATHS=-I$(COCOA_ROOT)/include  -$(ISYSTEM) $(EXTLIBS)/include
+INCLUDE_PATHS=-I$(COCOA_ROOT)/include  -$(ISYSTEM) $(EXTLIBS)/include \
+$(ISYSTEM_BOOST_INCLUDE) $(ISYSTEM_READLINE_INCLUDE)
 COMPILE=$(CXX)  $(CXXFLAGS)  $(INCLUDE_PATHS)
 COCOA_LIB=$(COCOA_ROOT)/lib/libcocoa.a
 
diff -Naur CoCoALib-0.99800_gui/configuration/fpic-ldflag.sh CoCoALib-0.99800_patched/configuration/fpic-ldflag.sh
--- CoCoALib-0.99800_gui/configuration/fpic-ldflag.sh	2022-03-25 16:29:32.000000000 +0000
+++ CoCoALib-0.99800_patched/configuration/fpic-ldflag.sh	1970-01-01 00:00:00.000000000 +0000
@@ -1,54 +0,0 @@
-#!/bin/bash
-
-SCRIPT_NAME=[[`basename "$0"`]]
-SCRIPT_DIR=`dirname "$0"`
-
-# Auxiliary script for CoCoALib configuration process.
-# Expects env variable CXX to be set (to compiler's name).
-
-# Script to see whether compiler is clang, and then link with special flags.
-# If no warning is produced, the script prints -fPIC; otherwise it prints nothing.
-
-if [ $# -ne 0 ]
-then
-  echo "ERROR: expected no args   $SCRIPT_NAME"  > /dev/stderr
-  exit 1
-fi
-
-# Check environment variable CXX
-if [ -z "$CXX" ]
-then
-  echo "ERROR: environment variable CXX not set.   $SCRIPT_NAME"  > /dev/stderr
-  exit 1
-fi
-
-
-# Create tmp directory, put test prog in it, compile and run.
-umask 22
-source "$SCRIPT_DIR/shell-fns.sh"
-TMP_DIR=`mktempdir fpic-ldflag`
-
-pushd "$TMP_DIR"  > /dev/null
-
-# test if it is clang:  .... a bit harsh, maybe...
-/bin/cat > test-fpic-ldflag.C <<EOF
-int main()
-{
-#ifdef __clang__
-  exit(1);
-#endif
-}
-EOF
-
-FPIC_FLAG=-fPIC
-
-"$CXX" -o test-fpic-ldflag  test-fpic-ldflag.C  > LogFile  2>& 1  &&  ./test-fpic-ldflag  >> LogFile  2>&1
-if [ $? -ne 0 ]
-then
-  FPIC_LDFLAG="-Wl,-no_pie";
-fi
-
-# Clean up TMP_DIR
-popd  > /dev/null
-/bin/rm -rf "$TMP_DIR"
-echo $FPIC_LDFLAG
diff -Naur CoCoALib-0.99800_gui/configuration/qt5-check.sh CoCoALib-0.99800_patched/configuration/qt5-check.sh
--- CoCoALib-0.99800_gui/configuration/qt5-check.sh	1970-01-01 00:00:00.000000000 +0000
+++ CoCoALib-0.99800_patched/configuration/qt5-check.sh	2022-10-24 06:06:19.074135623 +0000
@@ -0,0 +1,108 @@
+#!/bin/bash
+
+# This script checks sufficiency of installed Qt5 system for the CoCoA5 GUI.
+# Exit code is 0 if sufficient, o/w non-zero.
+
+# This script expects no arguments.
+
+SCRIPT_NAME="[[$(basename "$0")]]"
+SCRIPT_DIR="$(dirname "$0")"
+
+if [ $# -ne 0 ]; then
+    echo "ERROR: expected no args   $SCRIPT_NAME" > /dev/stderr
+    exit 1
+fi
+
+# Create tmp directory, put test prog in it, compile and run.
+umask 22
+. "$SCRIPT_DIR/shell-fns.sh"
+TMP_DIR="$(mktempdir qt5-check)"
+
+pushd "$TMP_DIR" > /dev/null || exit 255
+
+# Create project file for qmake
+/bin/cat > qt5-check.pro << 'EOF'
+QMAKE_MAKEFILE = qt5-check-makefile
+
+TEMPLATE = app
+TARGET = qt5-check
+QT += xml webkitwidgets printsupport # crucial line with dependencies
+SOURCES += qt5-check.cpp
+EOF
+
+# Create C++ file
+/bin/cat > qt5-check.cpp << 'EOF'
+#include <QtWidgets/QApplication> // a header file necessary for QCodeEdit
+#include <iostream>
+
+int main() {
+    std::cout << "Qt5 version " << qVersion() << " found" << std::endl;
+    return 0;
+}
+EOF
+
+# Set qmake flag for macOS (see src/CoCoA-5/make-c5makefile.sh
+#                           and src/CoCoA-5/make-qcodeeditmakefile.sh)
+if [ "$(uname)" = "Darwin" ]; then
+    DARWIN_OPTS="-spec macx-g++"
+fi
+
+# Check for qmake
+if ! QMAKE=$(   ( command -v qmake-qt5 2>  LogFile ) \
+             || ( command -v qmake     2>> LogFile ) ); then
+    # Deliberately leave $TMP_DIR to assist debugging.
+    echo "ERROR: qmake (and qmake-qt5) not found   $SCRIPT_NAME" > /dev/stderr
+    exit 3
+fi
+
+# Basic check of $QMAKE
+echo "$QMAKE -v" > LogFile
+if ! "$QMAKE" -v >> LogFile 2>&1; then
+    # Deliberately leave $TMP_DIR to assist debugging.
+    echo "ERROR: $QMAKE not working properly   $SCRIPT_NAME" > /dev/stderr
+    exit 4
+fi
+
+# Use $QMAKE to create makefile
+echo "$QMAKE $DARWIN_OPTS qt5-check.pro" > LogFile
+if ! "$QMAKE" $DARWIN_OPTS qt5-check.pro >> LogFile 2>&1; then
+    # Deliberately leave $TMP_DIR to assist debugging.
+    echo "ERROR: $QMAKE exited abnormally --> see LogFile.   $SCRIPT_NAME" \
+    > /dev/stderr
+    exit 5
+fi
+
+# If MAKE is unset, try to set it reasonably
+if [ -z "$MAKE" ]; then
+    echo "Warning: MAKE not set, trying to find it   $SCRIPT_NAME" > LogFile
+    if ! MAKE="$(command -v make 2>> LogFile)"; then
+        # Deliberately leave $TMP_DIR to assist debugging.
+        echo "ERROR: make not found   $SCRIPT_NAME" > /dev/stderr
+        exit 6
+    fi
+fi
+
+# Use $MAKE to compile cpp file
+echo "Using MAKE = $MAKE   $SCRIPT_NAME" > LogFile
+echo "$MAKE -f qt5-check-makefile" >> LogFile
+if ! "$MAKE" -f qt5-check-makefile >> LogFile 2>&1; then
+    # Deliberately leave $TMP_DIR to assist debugging.
+    echo "ERROR: $MAKE exited abnormally --> see LogFile.   $SCRIPT_NAME" \
+    > /dev/stderr
+    exit 7
+fi
+
+# Run Qt5 program
+echo "./qt5-check" > LogFile
+if ! ./qt5-check >> LogFile 2>&1; then
+    # Deliberately leave $TMP_DIR to assist debugging.
+    echo "ERROR: qt5-check exited abnormally --> see LogFile.   $SCRIPT_NAME" \
+     > /dev/stderr
+    exit 8
+fi
+
+# Clean up $TMP_DIR
+popd > /dev/null || exit 255
+/bin/rm -rf "$TMP_DIR"
+
+echo "$QMAKE"
diff -Naur CoCoALib-0.99800_gui/configuration/readline-find-hdr.sh CoCoALib-0.99800_patched/configuration/readline-find-hdr.sh
--- CoCoALib-0.99800_gui/configuration/readline-find-hdr.sh	2022-03-25 16:29:32.000000000 +0000
+++ CoCoALib-0.99800_patched/configuration/readline-find-hdr.sh	2022-10-24 06:06:19.074135623 +0000
@@ -54,9 +54,16 @@
 READLINE_LIB_DIR_DIR=`dirname "$READLINE_LIB_DIR"`
 READLINE_LIB_DIR_DIR_DIR=`dirname "$READLINE_LIB_DIR_DIR"`
 READLINE_HDR_DIR1="$READLINE_LIB_DIR_DIR/include/readline"
-READLINE_HDR_DIR2="$READLINE_LIB_DIR_DIR_DIR/include/readline"
+### On some systems there is an "intermediate" directory:
+INTERMEDIATE_DIR="$(basename "$READLINE_LIB")"
+INTERMEDIATE_DIR="${INTERMEDIATE_DIR%.*}"
+INTERMEDIATE_DIR="${INTERMEDIATE_DIR#lib}"
+READLINE_HDR_DIR2="$READLINE_LIB_DIR_DIR/include/$INTERMEDIATE_DIR/readline"
+###
+READLINE_HDR_DIR3="$READLINE_LIB_DIR_DIR_DIR/include/readline"
 READLINE_HDR1="$READLINE_HDR_DIR1/readline.h"
 READLINE_HDR2="$READLINE_HDR_DIR2/readline.h"
+READLINE_HDR3="$READLINE_HDR_DIR3/readline.h"
 
 if [ -f "$READLINE_HDR1" -a -r "$READLINE_HDR1" ]
 then
@@ -72,8 +79,16 @@
   exit 0
 fi
 
+if [ -f "$READLINE_HDR3" -a -r "$READLINE_HDR3" ]
+then
+  # We've found a plausible readline.h.
+  echo "$READLINE_HDR3"
+  exit 0
+fi
+
 echo "ERROR: Trouble finding READLINE header file:    $SCRIPT_NAME"  > /dev/stderr
 echo "ERROR: + tried  $READLINE_HDR1   $SCRIPT_NAME"                 > /dev/stderr
 echo "ERROR: + tried  $READLINE_HDR2   $SCRIPT_NAME"                 > /dev/stderr
+echo "ERROR: + tried  $READLINE_HDR3   $SCRIPT_NAME"                 > /dev/stderr
 
 exit 2
diff -Naur CoCoALib-0.99800_gui/configure CoCoALib-0.99800_patched/configure
--- CoCoALib-0.99800_gui/configure	2022-03-25 16:29:32.346970365 +0000
+++ CoCoALib-0.99800_patched/configure	2022-10-24 06:06:19.074135623 +0000
@@ -84,7 +84,7 @@
 
 COCOA_DEBUG="no"
 
-BUILD_QT_GUI="no"
+BUILD_QT_GUI="yes"
 
 # Below, is there a neater way to do tilde expansion for paths to libraries???
 for option in "$@"
@@ -114,8 +114,8 @@
     ( --with-libgmp=* )
        GMP_LIB=`echo "$option" | cut -f 2- -d=` ;;
 
-    ( --Qt-gui )
-      BUILD_QT_GUI=yes ;;
+    ( --no-qt-gui )
+      BUILD_QT_GUI=no ;;
 
     (--no-boost )
 	AVOID_BOOST=yes ;;
@@ -257,8 +257,8 @@
        echo -e "  --no-readline\t\tdo not look for libreadline.a"
        echo -e "  --with-libreadline=ARG\tspecify location of the file libreadline.a"
        echo -e "\t\t\t[default is to search for libreadline.a]"
-       echo -e "  --Qt-gui\t\tbuild also the Qt GUI for CoCoA-5"
-       echo -e "\t\t\t>>> REQUIRES qmake & Qt webkit libraries <<<"
+       echo -e "  --no-qt-gui\t\tdisable building the Qt5 GUI for CoCoA-5"
+       echo -e "\t\t\t[default is to determine automatically]"
        exit;;
 
     ( * )
@@ -363,7 +363,7 @@
 if [ "$CXX_TYPE" = "gnu" -a -z "$CXXFLAGS" ]
 then
     FPIC=`$SCRIPT_DIR/fpic-flag.sh`
-    LDFPIC=`$SCRIPT_DIR/fpic-ldflag.sh`
+    LDFPIC='-fPIC'
 fi
 
 CXXFLAGS="$CXXFLAGS  $CXX14_FLAG  -Wall  -pedantic  $FPIC"
@@ -616,6 +616,7 @@
       HAVE_READLINE=yes
       READLINE_LIB="$READLINE_LIB  $LIBTERMCAP"
     fi
+    READLINE_INC_DIR="$(dirname "$(dirname "$READLINE_HDR")")"
   fi
 fi
 
@@ -991,11 +992,11 @@
     eval "$BOOST_MESG"  # sets BOOST_LIB_DIR and BOOST_LDLIBS
     BOOST_LIB_NOT_FOUND=
     # Check that the BOOST libs are compatible with CXX and CXXFLAGS
-    BOOST_MESG=`$SCRIPT_DIR/boost-check-arch.sh  "$BOOST_LDLIBS" 2>&1`
+    BOOST_MESG=`$SCRIPT_DIR/boost-check-arch.sh "$BOOST_INC_DIR" "$BOOST_LIB_DIR" "$BOOST_LDLIBS" 2>&1`
     if [ $? -ne 0 ]
     then
       BOOST_LDLIBS_STATIC="-Wl,-Bstatic $BOOST_LDLIBS -Wl,-Bdynamic"
-      BOOST_MESG2=`$SCRIPT_DIR/boost-check-arch.sh  "$BOOST_LDLIBS_STATIC" 2>&1`
+      BOOST_MESG2=`$SCRIPT_DIR/boost-check-arch.sh "$BOOST_INC_DIR" "$BOOST_LIB_DIR" "$BOOST_LDLIBS_STATIC" 2>&1`
       if [ $? -eq 0 ]
       then
 	BOOST_LDLIBS=$BOOST_LDLIBS_STATIC
@@ -1038,43 +1039,29 @@
 
 
 ##################################################################
-# If user requested building the Qt GUI...
-# (1) check whether "qmake" is available -- needed for building the GUI
-# (2) assume that if "qmake" is available then all of QT is too.
-# (3) also check that "qmake" actually runs (Ubuntu 14.04 can have a broken symlink)
-# (4) also check that BOOST is available (o/w cannot build CoCoA-5)
+# Determine if Qt5 GUI is to be built
 
-if [ "$BUILD_QT_GUI" = "yes" ]
+if [ "$HAVE_BOOST" = 'no' ]
 then
-#HAVE_QMAKE=no
-    QMAKE=`which qmake 2>/dev/null`
-    if [ $? -ne 0 ]
-    then
-	QMAKE_ERR="qmake (Qt4) not found";
-    else
-	JUNK=`[ -x "$QMAKE" ] && qmake -help 2>&1 >/dev/null`
-	if [ $? -ne 0 ]
-	then
-	    QMAKE_ERR="qmake not working properly"
-	fi
-    fi
-    if [ -n "$QMAKE_ERR" ]
-    then
-	echo "$0: ERROR:  $QMAKE_ERR"   > /dev/stderr
-	exit 1;
-    fi
+  echo "Cannot build CoCoA-5 or its GUI without BOOST"
+  BUILD_QT_GUI='no'
+fi
 
-    if [ "$HAVE_BOOST" = "no" ]
-    then
-	echo "$0: ERROR:  cannot build CoCoA-5 or its GUI without BOOST"  > /dev/stderr
-	exit 1;
-    fi
+if [ "$BUILD_QT_GUI" = 'no' ]
+then
+  echo "Not building Qt5 GUI"
+  echo
+else
+  if ! QMAKE=$("$SCRIPT_DIR/qt5-check.sh" 2> "ERR_MESGS")
+  then
+    echo "Qt5 system insufficient, not building Qt5 GUI"
+    echo
+  else
+    echo "Building Qt5 GUI"
+    echo
+    BUILD_QT_GUI='yes'
+  fi
 fi
-#if [ "$HAVE_BOOST" = "yes" -a "$HAVE_QMAKE" = "no" ]
-#then
-#  echo "Note: we will build CoCoA5 ***without*** GUI because \"qmake\" (from Qt4) is absent"
-#  echo
-#fi
 
 
 ##################################################################
@@ -1317,6 +1304,15 @@
 # BOOST
 echo "# BOOST library:"                                       >> "$CONFIG_TMP"
 echo "HAVE_BOOST=$HAVE_BOOST"                                 >> "$CONFIG_TMP"
+if ! [ -z "$BOOST_INC_DIR" ]
+then
+#  echo "ISYSTEM_BOOST_INCLUDE=-\$(ISYSTEM) $BOOST_INC_DIR"    >> "$CONFIG_TMP"
+  echo "ISYSTEM_BOOST_INCLUDE=-I $BOOST_INC_DIR"              >> "$CONFIG_TMP"
+fi
+if ! [ -z "$BOOST_LIB_DIR" ]
+then
+  echo "L_BOOST_LIB_DIR=-L$BOOST_LIB_DIR"                     >> "$CONFIG_TMP"
+fi
 echo "BOOST_LDLIBS=$BOOST_LDLIBS"                             >> "$CONFIG_TMP"
 echo                                                          >> "$CONFIG_TMP"
 
@@ -1327,6 +1323,10 @@
 echo "HAVE_READLINE = $HAVE_READLINE   $READLINE_LIB"
 if [ $HAVE_READLINE = "yes" ]
 then
+  if ! [ -z "$READLINE_INC_DIR" ]
+  then
+    echo "ISYSTEM_READLINE_INCLUDE=-\$(ISYSTEM) $READLINE_INC_DIR" >> "$CONFIG_TMP"
+  fi
   echo "READLINE_LDLIBS=$READLINE_LIB"                        >> "$CONFIG_TMP"
 fi
 echo                                                          >> "$CONFIG_TMP"
@@ -1343,6 +1343,7 @@
 
 echo "# Flag saying whether to build the Qt GUI"              >> "$CONFIG_TMP"
 echo "BUILD_QT_GUI=$BUILD_QT_GUI"                             >> "$CONFIG_TMP"
+echo "QMAKE=$QMAKE"                                           >> "$CONFIG_TMP"
 echo                                                          >> "$CONFIG_TMP"
 
 
@@ -1431,20 +1432,6 @@
   echo
   sleep 3
 fi
-if [ "$BUILD_QT_GUI" = "yes" ]
-then
-    echo
-    echo "QT-QT-QT-QT-QT-QT-QT-QT-QT-QT-QT-QT-QT-QT-QT-QT-QT-QT-QT-QT-QT-QT-QT-QT-QT-QT-QT"
-    echo "QT                                                                            QT"
-    echo "QT  To build the QT-gui, ensure that you have qmake and Qt webkit installed   QT"
-    echo "QT  It probably suffices to install: 'qt5-qmake' and 'libqt5webkit5-dev'      QT"
-    echo "QT                                                                            QT"
-    echo "QT-QT-QT-QT-QT-QT-QT-QT-QT-QT-QT-QT-QT-QT-QT-QT-QT-QT-QT-QT-QT-QT-QT-QT-QT-QT-QT"
-    echo
-    echo -ne "\a"; sleep 1; echo -ne "\a"; sleep 1; echo -e "\a"  # 3 alert beeps!!!
-    sleep 5
-    # Ulrich also needed: libqt5scxml5-dev
-fi
 echo "======================================================="
 echo "CoCoALib configuration process complete."
 echo "Configuration info saved in the following files:"
diff -Naur CoCoALib-0.99800_gui/doc/CoCoALib-tasks/Makefile CoCoALib-0.99800_patched/doc/CoCoALib-tasks/Makefile
--- CoCoALib-0.99800_gui/doc/CoCoALib-tasks/Makefile	2022-03-25 16:29:32.000000000 +0000
+++ CoCoALib-0.99800_patched/doc/CoCoALib-tasks/Makefile	2022-10-24 06:06:19.074135623 +0000
@@ -1,7 +1,7 @@
 # Makefile for CoCoALib/doc/CoCoALib-tasks/ directory.
 
-# COCOA_ROOT=../..
-# include $(COCOA_ROOT)/configuration/autoconf.mk
+COCOA_ROOT=../..
+include $(COCOA_ROOT)/configuration/autoconf.mk # sets CXX
 CWD=doc/CoCoALib-tasks
 
 EXEC = HTMLTasks
@@ -22,7 +22,7 @@
 
 $(EXEC): $(C_SRCS)
 	@echo "Generating executable" $(EXEC) "..."
-	@g++ -g -Wextra  HTMLTasks.C  -o $(EXEC)
+	@$(CXX) -g -Wextra  HTMLTasks.C  -o $(EXEC)
 	/bin/rm -rf  $(EXEC).dSYM
 
 .PHONY: clean clean-local
diff -Naur CoCoALib-0.99800_gui/doc/Makefile CoCoALib-0.99800_patched/doc/Makefile
--- CoCoALib-0.99800_gui/doc/Makefile	2022-03-25 16:29:32.000000000 +0000
+++ CoCoALib-0.99800_patched/doc/Makefile	2022-10-24 06:06:19.074135623 +0000
@@ -12,9 +12,9 @@
 DOC_SRCS = $(wildcard $(SRC_DIR)/*.txt)
 DOC_HTML = $(patsubst $(SRC_DIR)/%.txt,$(HTML_DIR)/%.html,$(DOC_SRCS))
 TASKS_OUT = CoCoALib-tasks.html  CoCoALib-CompletedTasks.html
-##TXT2TAGS = txt2tags   # if installed version is new enough
+TXT2TAGS = txt2tags   # if installed version is new enough
 ##TXT2TAGS = /usr/local/Cellar/txt2tags/3.7_3/bin/txt2tags
-TXT2TAGS = $(AUX_DIR)/txt2tags
+##TXT2TAGS = $(AUX_DIR)/txt2tags
 
 .SUFFIXES:
 
@@ -42,7 +42,7 @@
 CoCoALib.pdf:  $(COCOA_ROOT)/configuration/version  $(DOC_SRCS)  $(AUX_DIR)/DocTeX.t2t  $(AUX_DIR)/cocoalib-doc.sty
 	@if [ "$(TXT2TAGS)" = "txt2tags" ]; \
 	 then \
-	   which txt2tags >/dev/null 2>&1 ; \
+	   command -v txt2tags > /dev/null 2>&1; \
 	   if [ $$? -ne 0 ]; \
 	   then \
 	     echo; \
@@ -51,14 +51,6 @@
 	     exit; \
 	   fi; \
 	 fi; \
-	 [ -x "$(TXT2TAGS)" ] && [ -r "$(TXT2TAGS)" ]; \
-         if [ $$? -ne 0 ]; \
-	 then \
-	   echo; \
-	   echo "***WARNING***  $(TXT2TAGS) missing ==> not creating/updating HTML documentation"; \
-	   echo; \
-	   exit; \
-	 fi; \
 	 $(MAKE)  $(TEX_DIR)/CoCoALib.tex  $(TEX_DIR)/cocoalib-doc.sty; \
 	 echo "Doing LaTeX-->PDF"; \
 	 cd $(TEX_DIR); \
@@ -86,7 +78,7 @@
 	 then \
 	   if [ "$(TXT2TAGS)" = "txt2tags" ]; \
 	   then \
-	     which txt2tags >/dev/null 2>&1; \
+	     command -v txt2tags > /dev/null 2>&1; \
 	     if [ $$? -ne 0 ]; \
 	     then \
 	       echo; \
@@ -95,14 +87,6 @@
 	       exit; \
 	     fi; \
 	   fi; \
-	   [ -x "$(TXT2TAGS)" ] && [ -r "$(TXT2TAGS)" ]; \
-           if [ $$? -ne 0 ]; \
-	   then \
-	     echo; \
-	     echo "***WARNING***  $(TXT2TAGS) missing or not executable ==> not creating/updating HTML documentation"; \
-	     echo; \
-	     exit; \
-	   fi; \
 	   $(MAKE) -s $(DOC_HTML); \
 	 fi
 
diff -Naur CoCoALib-0.99800_gui/src/CoCoA-5/Makefile CoCoALib-0.99800_patched/src/CoCoA-5/Makefile
--- CoCoALib-0.99800_gui/src/CoCoA-5/Makefile	2022-03-25 16:30:55.000000000 +0000
+++ CoCoALib-0.99800_patched/src/CoCoA-5/Makefile	2022-10-24 06:06:19.074135623 +0000
@@ -62,7 +62,7 @@
 
 CoCoAInterpreter: VersionInfo.o  CompilationDate.o  $(COCOA_LIB)
 	@echo "CoCoAInterpreter: linking everything together"
-	$(COMPILE5) $(OBJS) CompilationDate.o -o CoCoAInterpreter $(LDLIBS)  $(COCOA5_LDLIBS)  -lpthread
+	$(COMPILE5) $(OBJS) CompilationDate.o -o CoCoAInterpreter $(LDLIBS)  $(COCOA5_LDLIBS)  $(L_BOOST_LIB_DIR)  -lpthread
 	@#Next lines remove the *.dSYM directory which the Apple compiler creates
 	@#but we remove it only if the compilation flag "-g" is absent.
 	@AppleDir=CoCoAInterpreter.dSYM ; \
@@ -112,8 +112,8 @@
 	@echo "-----------------------------------"
 	@echo ">>>>  Making the CoCoA Qt GUI  <<<<"
 	@echo "-----------------------------------"
-	@echo "[[Qt-GUI: 1/3]]  Building QCodeEdit";  cd QCodeEdit;  $(MAKE)  -s
-	@echo "[[Qt-GUI: 2/3]]  Buiding GUI"; $(MAKE) -s -f C5Makefile
+	@echo "[[Qt-GUI: 1/3]]  Building QCodeEdit";  cd QCodeEdit;  CXX=$(CXX) $(MAKE) -s
+	@echo "[[Qt-GUI: 2/3]]  Buiding GUI"; CXX=$(CXX) $(MAKE) -s -f C5Makefile
 	@echo "[[Qt-GUI: 3/3]]  Finishing GUI"; ./make-gui-finish.sh
 	@echo
 	@echo "---------------------------------------------------------"
@@ -122,11 +122,11 @@
 
 QCodeEdit/Makefile: $(COCOA_LIB)  QCodeEdit/QCodeEdit.pro.in  make-qcodeeditmakefile.sh
 	if [ -f QCodeEdit/Makefile ]; then cd QCodeEdit; $(MAKE) -s clean; fi
-	./make-qcodeeditmakefile.sh
+	QMAKE=$(QMAKE) ./make-qcodeeditmakefile.sh
 
 C5Makefile: $(COCOA_LIB)  C5.pro.in  make-c5makefile.sh
 	if [ -f C5Makefile ]; then $(MAKE) -s -f C5Makefile distclean; fi
-	./make-c5makefile.sh  $(CXXFLAGS_DEFINES)  $(COCOA5_VERSION_DEFINES)  $(LDLIBS)  $(COCOA5_LDLIBS)
+	QMAKE=$(QMAKE) ./make-c5makefile.sh $(CXXFLAGS_DEFINES) $(COCOA5_VERSION_DEFINES) $(LDLIBS) $(COCOA5_LDLIBS)
 
 .PHONY: check
 check: cocoa5
@@ -193,7 +193,7 @@
 
 .PHONY: doc
 doc:
-	@(cd $(COCOA5_DOC_DIR); make)
+	@(cd $(COCOA5_DOC_DIR); $(MAKE))
 
 # .PHONY: htmldoc
 # htmldoc:
diff -Naur CoCoALib-0.99800_gui/src/CoCoA-5/make-c5makefile.sh CoCoALib-0.99800_patched/src/CoCoA-5/make-c5makefile.sh
--- CoCoALib-0.99800_gui/src/CoCoA-5/make-c5makefile.sh	2016-03-09 10:18:29.000000000 +0000
+++ CoCoALib-0.99800_patched/src/CoCoA-5/make-c5makefile.sh	2022-10-24 06:06:19.074135623 +0000
@@ -2,7 +2,7 @@
 
 # This script builds the CoCoA-5 GUI; it assumes that CoCoALib
 # has already been built, and that the Qt libraries have been installed
-# (and that the qmake command is available).
+# (and that the $QMAKE command is available).
 
 # if [ $# -ne 5 ]
 # then
@@ -10,12 +10,17 @@
 #   exit 1
 # fi
 
+##################################################################
+# Check $QMAKE
 
-which qmake >/dev/null
-if [ $? -ne 0 ]
-then
-  echo "ERROR: $0 failed because it cannot find qmake" >/dev/stderr
-  exit 1
+if [ -z "$QMAKE" ]; then
+  echo "ERROR: QMAKE not set" > /dev/stderr
+  exit 2
+fi
+
+if [ ! -r "$QMAKE" -o ! -x "$QMAKE" ]; then
+  echo "ERROR: $QMAKE not executable" > /dev/stderr
+  exit 3
 fi
 
 ##################################################################
@@ -104,9 +109,9 @@
 then
   DARWIN_OPTS="-spec macx-g++"
 fi
-qmake $DARWIN_OPTS C5.pro -o C5Makefile
+"$QMAKE" $DARWIN_OPTS C5.pro -o C5Makefile
 if [ "$?" -ne 0 ]
 then
-  echo "ERROR: $0 failed because qmake failed for C5.pro"  >/dev/stderr
+  echo "ERROR: $0 failed because $QMAKE failed for C5.pro" > /dev/stderr
   exit 6
 fi
diff -Naur CoCoALib-0.99800_gui/src/CoCoA-5/make-qcodeeditmakefile.sh CoCoALib-0.99800_patched/src/CoCoA-5/make-qcodeeditmakefile.sh
--- CoCoALib-0.99800_gui/src/CoCoA-5/make-qcodeeditmakefile.sh	2014-03-07 14:18:27.000000000 +0000
+++ CoCoALib-0.99800_patched/src/CoCoA-5/make-qcodeeditmakefile.sh	2022-10-24 06:06:19.074135623 +0000
@@ -2,7 +2,7 @@
 
 # This script builds the CoCoA-5 GUI; it assumes that CoCoALib
 # has already been built, and that the Qt libraries have been installed
-# (and that the qmake command is available).
+# (and that the $QMAKE command is available).
 
 if [ $# -ne 0 ]
 then
@@ -11,13 +11,16 @@
 fi
 
 ##################################################################
-# Check for qmake program
+# Check $QMAKE
 
-which qmake >/dev/null
-if [ $? -ne 0 ]
-then
-  echo "ERROR: $0 failed because it cannot find qmake" >/dev/stderr
-  exit 1
+if [ -z "$QMAKE" ]; then
+  echo "ERROR: QMAKE not set" > /dev/stderr
+  exit 2
+fi
+
+if [ ! -r "$QMAKE" -o ! -x "$QMAKE" ]; then
+  echo "ERROR: $QMAKE not executable" > /dev/stderr
+  exit 3
 fi
 
 ##################################################################
@@ -27,7 +30,7 @@
 if [ \! -d "$QCE" ]
 then
   echo "ERROR: $0 cannot find subdirectory $QCE/"
-  exit 2
+  exit 4
 fi
 
 
@@ -83,9 +86,9 @@
   DARWIN="-spec macx-g++"
 fi
 cd $QCE
-qmake $DARWIN QCodeEdit.pro
+"$QMAKE" $DARWIN QCodeEdit.pro
 if [ "$?" -ne 0 ]
 then
-  echo "ERROR: $0 failed because qmake failed for QCodeEdit"  >/dev/stderr
-  exit 4
+  echo "ERROR: $0 failed because $QMAKE failed for QCodeEdit" > /dev/stderr
+  exit 5
 fi
diff -Naur CoCoALib-0.99800_gui/src/CoCoA-5/tests/Makefile CoCoALib-0.99800_patched/src/CoCoA-5/tests/Makefile
--- CoCoALib-0.99800_gui/src/CoCoA-5/tests/Makefile	2022-03-25 16:31:02.000000000 +0000
+++ CoCoALib-0.99800_patched/src/CoCoA-5/tests/Makefile	2022-10-24 06:06:19.074135623 +0000
@@ -59,7 +59,7 @@
 # directory.  Most of the work is done by the RunTests.sh script.
 .PHONY: check
 check: ../CoCoAInterpreter
-	@./RunTests.sh $(TESTS) 2> /dev/null  # discard err mesgs from the shell script
+	@./RunTests.sh $(TESTS)
 
 .PHONY: clean clean-local
 clean: clean-local
diff -Naur CoCoALib-0.99800_gui/src/CoCoA-5/tests/RunTests.sh CoCoALib-0.99800_patched/src/CoCoA-5/tests/RunTests.sh
--- CoCoALib-0.99800_gui/src/CoCoA-5/tests/RunTests.sh	2022-03-25 16:31:02.000000000 +0000
+++ CoCoALib-0.99800_patched/src/CoCoA-5/tests/RunTests.sh	2022-10-24 06:06:19.074135623 +0000
@@ -30,9 +30,17 @@
 fi
 
 source ../../../configuration/shell-fns.sh
-source ../../../configuration/autoconf.mk
+#source ../../../configuration/autoconf.mk
 
-NUM_TESTS=$#-CoCoA-5
+unset progs
+for arg in "$@"; do
+  prog=$(basename "$arg" .cocoa5)
+  HAVE_EXTLIB=HAVE_"${prog:6:100}"
+  if [ "ExtLib" != "${prog:0:6}" ] || [ "${!HAVE_EXTLIB}" = yes ]; then
+    progs=("${progs[@]}" "$arg")
+  fi
+done
+NUM_TESTS=${#progs[@]}-CoCoA-5
 
 # Before running the tests check that empty input does not trigger an error (e.g. package error)
 ../CoCoAInterpreter --no-readline --no-preamble --packageDir ../packages /dev/null > NULL.found 2> NULL.cerr
@@ -54,24 +62,19 @@
 /bin/rm  NULL.found  NULL.cerr
 
 echo
-echounderline "Running the CoCoA-5 tests ($# tests altogether)"
+echounderline "Running the CoCoA-5 tests (${#progs[@]} tests altogether)"
 
 # Keep track of which tests failed, to print a summary at the end.
 failures=""
 
-# This loop iterates through the names supplied as arguments to the script.
+# This loop iterates through the names of tests to run.
 COUNTER=0
-while [ $# -ne 0 ]
-do
+for p in "${progs[@]}"; do
   COUNTER=`expr 1 + $COUNTER`
-  prog=`basename "$1" .cocoa5`
-  if [ $? -ne 0 -o ! -f "$1" ] ; then echo "!!!!! Bad test source file \`$1' !!!!!"; exit 1; fi
-  shift
+  prog=`basename "$p" .cocoa5`
+  if [ $? -ne 0 -o ! -f "$p" ] ; then echo "!!!!! Bad test source file \`$p' !!!!!"; exit 1; fi
   /bin/rm -f $prog.found $prog.cerr
-  HAVE_EXTLIB=HAVE_${prog:6:100};
-  if [ "ExtLib" != "${prog:0:6}" ] || [ ${!HAVE_EXTLIB} = yes ]
-  then
-    ../CoCoAInterpreter --no-readline --no-preamble --packageDir ../packages $prog.cocoa5 > $prog.found 2> $prog.cerr
+  ../CoCoAInterpreter --no-readline --no-preamble --packageDir ../packages $prog.cocoa5 > $prog.found 2> $prog.cerr
   if [ $? -ne 0 ]
   then
     echo "[$COUNTER/$NUM_TESTS] *****  $prog FAILED  ***** (non-zero exit status)"
@@ -103,7 +106,6 @@
         echo "[$COUNTER/$NUM_TESTS] $prog.cocoa5 ..... OK"
       fi
     fi
-    fi
   fi
 done
 if [ -z "$failures" ]
EOFBUILDPATCH
printf '%s\n' "done"

# Delete configuration/fpic-ldflag.sh (empty and unused after the patch):
rm -f "$COCOALIB_VER2_DIR/configuration/fpic-ldflag.sh"
# Set execute permissions on newly created file:
chmod 755 "$COCOALIB_VER2_DIR/configuration/qt5-check.sh"

# Clean up:
printf '%s' "Cleaning up... "
rm -r "$COCOALIB_VER1_DIR"
mv "$COCOALIB_VER2_DIR" "$COCOALIB_PATCHED_DIR"
printf '%s\n' "done"

printf '\n%s\n' "Qt5 GUI and build scripts patching finished"
