#!/usr/local/bin/bash
set -ex
export ASSUME_ALWAYS_YES=yes

# Already in freebsd14-sciteco
# TODO: Build this with buildah.
# Start with --network=host
#pkg update
#pkg install FreeBSD-clang FreeBSD-clibs-dev \
#            gmake pkgconf autoconf automake libtool \
#            glib gtk3 groff doxygen lowdown valgrind
#
#pkg install llvm21 gnugrep gmake coreutils gsed gawk git wget gnupg bash groff zip autoconf automake libtool python3
#pkg remove FreeBSD-clang
#git clone https://github.com/HolyBlackCat/quasi-msys2.git /opt/quasi-msys2
#cd /opt/quasi-msys2
#ln -s /usr/local/bin/gpgv2 /usr/local/bin/gpgv
#ln -s /usr/local/bin/bash /bin/bash
#mkdir -p gnu-overrides
#ln -s /usr/local/bin/ggrep gnu-overrides/grep
#ln -s /usr/local/bin/gmake gnu-overrides/make
#ln -s /usr/local/bin/gsed gnu-overrides/sed
#ln -s /usr/local/bin/greadlink gnu-overrides/readlink
#ln -s /usr/local/bin/wine64 gnu-overrides/wine
#echo MINGW64 >msystem.txt
#cat >activate << EOF
#cd /opt/quasi-msys2
#export PATH=`pwd`/gnu-overrides:$PATH
#export PKG_CONFIG=pkg-config
#set +ex
#. env/all.src
#set -ex
#EOF
#gmake install _autotools _gcc _libc++ _glib2 _pdcurses _gtk3 _librsvg
#ln -nfs "/opt/quasi-msys2/root/mingw64" /mingw64
#pkg clean -a

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

export CURSES_CFLAGS=-I/mingw64/include/pdcurses/
# 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.

#autoreconf -i
mkdir build-wingui build-wincon
cd build-wingui
../configure --host=x86_64-w64-mingw32 \
             --with-interface=pdcurses-gui --enable-html-docs --program-prefix=g \
             --with-scitecodatadir=. \
             --disable-bootstrap \
             CURSES_LIBS="-lpdcurses_wingui -lgdi32 -lcomdlg32 -lwinmm"
make
make install-strip
#make check TESTSUITEFLAGS="--verbose --color=never"

cd ../build-wincon
../configure --host=x86_64-w64-mingw32 \
             --with-interface=pdcurses --enable-html-docs \
             --with-scitecodatadir=. \
             --disable-bootstrap \
             CURSES_LIBS="-lpdcurses_wincon -lgdi32 -lwinmm"
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/
