#!/usr/bin/env 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; releases already present will not be downloaded again.
#
# The files in the folder CoCoALib-0.99800_gui will afterwards be patched. To
# compile with the Qt5 GUI pass the option --Qt-gui to the configure script.

VER1=0.99718
VER2=0.99800

COCOALIB_VER1_DIR=CoCoALib-$VER1
COCOALIB_VER2_DIR=CoCoALib-$VER2

COCOALIB_VER1_SHA256=1b822387c4482c1e81b1d060abca726a27507702c7c642c10c094527fc0e7bbc
#COCOALIB_VER2_SHA256=6f38dbf9156d5598e67c05c9557f03281880527afd579d71cd4076be535ec523
COCOALIB_VER2_SHA256=f8bb227e2e1729e171cf7ac2008af71df25914607712c35db7bcb5a044a928c6

# Check if directory is clean enough:

echo -n "Checking directory... "
if [ -e $COCOALIB_VER1_DIR ]; then
    echo "Error: $COCOALIB_VER1_DIR already exists, exiting"
    exit 1
fi
if [ -e $COCOALIB_VER2_DIR ]; then
    echo "Error: $COCOALIB_VER2_DIR already exists, exiting"
    exit 1
fi
if [ -e ${COCOALIB_VER2_DIR}_gui ]; then
    echo "Error: ${COCOALIB_VER2_DIR}_gui already exists, exiting"
    exit 1
fi
echo "done"

# Download CoCoALib and check SHA256 sums:

for i in $VER1 $VER2; do
    if [ $i = $VER1 ]; then
        SHA256SUM=$COCOALIB_VER1_SHA256
    elif [ $i = $VER2 ]; then
        SHA256SUM=$COCOALIB_VER2_SHA256
    else
        echo "An error occured: \$i = $i" 2>&1
        exit 1
    fi
    if [ ! -e CoCoALib-$i.tgz ]; then
        echo -n "Downloading CoCoALib-$i.tgz... "
        wget -q https://cocoa.dima.unige.it/cocoa/cocoalib/tgz/CoCoALib-$i.tgz
        echo "done"
    fi
    echo -n "Verifying CoCoALib-$i.tgz... "
    sha256sum --status -c <<-EOF
	$SHA256SUM  CoCoALib-$i.tgz
	EOF
    if [ $? -ne 0 ]; then
        echo "\nCoCoALib-$i.tgz invalid" 1>&2
        exit 1
    fi
    echo "done"
done

# Unpack CoCoALib:

for i in $VER1 $VER2; do
    echo -n "Unpacking CoCoALib-$i... "
    tar xf CoCoALib-$i.tgz
    echo "done"
done

# Copy the missing files from the old release to the new one:

echo -n "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
echo "done"

# Apply Qt5 patches to the copied files:

echo -n "Applying Qt5 patches to copied files... "
patch -p0 -s << 'EOF'
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;
EOF
echo "done"

# Clean up:

echo -n "Cleaning up... "
rm -r $COCOALIB_VER1_DIR
mv $COCOALIB_VER2_DIR ${COCOALIB_VER2_DIR}_gui
echo "done"
