#!/usr/local/bin/bash
set -ex

autoreconf -i
mkdir build-freebsd
cd build-freebsd
../configure --with-interface=ncurses --enable-debug --enable-html-docs
gmake

# NOTE: The test suite must be run in verbose mode because if it fails
# we won't be able to analyze testsuite.log.
gmake check TESTSUITEFLAGS="--verbose --color=never --valgrind"
# Includes a second test suite run, but without Valgrind.
# This is good since we had to exclude several test cases when running
# under CI with --valgrind.
gmake distcheck

# Test building Doxygen documentation
gmake -C doc devdoc

gmake install

# TODO: Also automatically rebuild the cheat sheet.

# Build and deploy website
cd ../www
sciteco -m build.tes ../build-freebsd
cp *.html /opt/htdocs/
cd ..
cp ico/sciteco.ico /opt/htdocs/graphics
cp ico/sciteco-48.png /opt/htdocs/graphics

# TODO: Should we also distribute FreeBSD binaries?

# FIXME: This should be a separate job but currently we need the
# boostrapping with the FreeBSD version of SciTECO.
# We can run with bootstrapping using --with-wine=wine64,
# but it doesn't even generate all files correctly.
# The test suite also doesn't fully work under Wine yet.
# Activate MSYS environment
. /opt/quasi-msys2/activate
cd /opt/build

# NOTE: Sometimes the PDCursesMod MSYS upstream package is too old,
# so we also installed v4.5.4 into the container.
#export CURSES_CFLAGS=-I/mingw64/include/pdcurses/
export CURSES_CFLAGS=-I/opt/PDCursesMod

# FIXME: glib on MinGW supports static linking but the gspawn
# helper binaries are still linked dynamically, forcing us to ship
# all DLLs anyway. Therefore it makes little sense to link SciTECO
# itself statically - it only wastes a few MB.
# You would also have to add --enable-static-executables.
# FIXME: Once there is an --enable-lto, we should use that.
# NOTE: LTO is broken with libstdc++.
# https://github.com/HolyBlackCat/quasi-msys2/issues/44
export CFLAGS="-O3 -flto=thin"
export CXXFLAGS="-O3 -flto=thin -stdlib=libc++"
export LDFLAGS="-flto=thin -stdlib=libc++"

# We cannot run Windows binaries automatically through Wine,
# so we must still force cross-compilation with --host.
# There is a --with-launcher=wine64, but SciTECO is currently
# simply broken under Wine.

#autoreconf -i
mkdir build-wingui build-wincon
cd build-wingui
# See above, we use a manually built PDCursesMod v4.5.4
#export CURSES_LIBS="-lpdcurses_wingui -lgdi32 -lcomdlg32 -lwinmm"
export CURSES_LIBS="/opt/PDCursesMod/wingui/pdcurses.a -lgdi32 -lcomdlg32 -lwinmm"
../configure --host=x86_64-w64-mingw32 \
             --with-interface=pdcurses-gui --enable-html-docs --program-prefix=g \
             --with-scitecodatadir=. \
             --disable-bootstrap
make
make install-strip
#make check TESTSUITEFLAGS="--verbose --color=never"

cd ../build-wincon
# See above, we use a manually built PDCursesMod v4.5.4
#export CURSES_LIBS="-lpdcurses_wincon -lgdi32 -lwinmm"
export CURSES_LIBS="/opt/PDCursesMod/wincon/pdcurses.a -lgdi32 -lwinmm"
../configure --host=x86_64-w64-mingw32 \
             --with-interface=pdcurses --enable-html-docs \
             --with-scitecodatadir=. \
             --disable-bootstrap
make
make install-strip
#make check TESTSUITEFLAGS="--verbose --color=never"

export MINGW_BUNDLEDLLS_SEARCH_PATH=/mingw64/bin

cd ..
mkdir -p temp-bin-pdcurses/
cd temp-bin-pdcurses/
cp -r /mingw64/bin/{gsciteco.exe,sciteco.exe,grosciteco.tes,tedoc.tes} ./
# datadir is relative to bindir
cp -r /mingw64/bin/{lib,*.tmac} ./
cp /mingw64/bin/fallback.teco_ini .teco_ini
cp -r /mingw64/share/doc/sciteco/* ./
cp ../COPYING ../ChangeLog ./
cp /mingw64/bin/gspawn-win64-helper*.exe ./
# Collect DLLs for all included binaries
for f in *.exe; do ../contrib/mingw-bundledlls --copy $f; done
zip -9 -r ../sciteco-pdcurses_nightly_win64.zip .
cd ..

mkdir -p /opt/htdocs/downloads/nightly/
cp sciteco-pdcurses_nightly_win64.zip /opt/htdocs/downloads/nightly/

# FIXME: If we had a working bootstrapping build (where SciTECO
# is run under wine64), this should also be in a separate job
# so we don't have to install into the same root as for the PDCurses versions.
mkdir build-gtk
cd build-gtk
../configure --host=x86_64-w64-mingw32 \
             --with-interface=gtk --enable-html-docs \
             --with-scitecodatadir=. \
             --disable-bootstrap
make
make install-strip
#make check TESTSUITEFLAGS="--verbose --color=never"

export MINGW_BUNDLEDLLS_SEARCH_PATH=/mingw64/bin

cd ..
mkdir -p temp-bin-gtk/
cd temp-bin-gtk/
cp /mingw64/bin/{sciteco.exe,grosciteco.tes,tedoc.tes} ./
# datadir is relative to bindir
cp -r /mingw64/bin/{lib,*.tmac} ./
cp /mingw64/bin/fallback.teco_ini .teco_ini
cp /mingw64/bin/fallback.css ../win32/.teco_css .
cp -r /mingw64/share/doc/sciteco/* ./
cp ../COPYING ../ChangeLog ./
cp /mingw64/bin/gspawn-win64-helper*.exe ./
# Collect DLLs for all included binaries
for f in *.exe; do ../contrib/mingw-bundledlls --copy $f; done
#mkdir share
#cp /mingw64/share/loader.cache share/
#glib-compile-schemas /mingw64/share/glib-2.0/schemas
#mkdir -p share/glib-2.0
#cp /mingw64/share/glib-2.0/gschemas.compiled share/glib-2.0/
mkdir -p share/icons/Adwaita
# FIXME: It should be sufficient to package the SVG icons,
# but I cannot get it to work. Perhaps index.theme would have to be tweaked.
# We could also try to include a pure scalable icon theme.
#cp -r /mingw64/share/icons/Adwaita/{scalable*,index.theme} share/icons/Adwaita/
cp -r /mingw64/share/icons/Adwaita/* share/icons/Adwaita/
wine64 /mingw64/bin/gtk-update-icon-cache-3.0.exe share/icons/Adwaita/
# FIXME: It's possible to change the location of loaders.cache via $GDK_PIXBUF_MODULE_FILE.
# If we did that, we could avoid "reusing" the lib/ directory.
# This is important when somebody changes $SCITECOPATH.
cp /mingw64/lib/gdk-pixbuf-2.0/2.10.0/loaders/{pixbufloader_svg.dll,libpixbufloader-png.dll} .
# Collect DLLs for all pixbuf loaders into the root directory
for f in *pixbufloader*.dll; do ../contrib/mingw-bundledlls --copy $f; done
mkdir -p lib/gdk-pixbuf-2.0/2.10.0/loaders/
mv *pixbufloader*.dll lib/gdk-pixbuf-2.0/2.10.0/loaders/
cp ../win32/loaders.cache lib/gdk-pixbuf-2.0/2.10.0/
zip -9 -r ../sciteco-gtk3_nightly_win64.zip .
cd ..

mkdir -p /opt/htdocs/downloads/nightly/
cp sciteco-gtk3_nightly_win64.zip /opt/htdocs/downloads/nightly/
