#!/bin/bash

set -e

if [[ "$MSYSTEM" == "MINGW32" ]]; then
    export ARTIFACTS_SUFFIX="-w32"
    export MSYS2_ARCH="i686"
    export MSYS_PREFIX="/c/msys64/mingw32/"
    export PATH="/mingw32/bin:$PATH"
    export PIKA_DISTRIB=`realpath ./pika-w32`
elif [[ "$MSYSTEM" == "MINGW64" ]]; then
    export ARTIFACTS_SUFFIX="-w64"
    export MSYS2_ARCH="x86_64"
    export MSYS_PREFIX="/c/msys64/mingw64/"
    export PATH="/mingw64/bin:$PATH"
    export PIKA_DISTRIB=`realpath ./pika-w64`
else # [[ "$MSYSTEM" == "CLANGARM64" ]];
    export ARTIFACTS_SUFFIX="-arm64"
    export MSYS2_ARCH="clang-aarch64"
    export MSYS_PREFIX="/c/msys64/clangarm64/"
    export PATH="/clangarm64/bin:$PATH"
    export PIKA_DISTRIB=`realpath ./pika-arm64`
fi

export OPTIONAL_PACKAGES=""
if [[ "$MSYSTEM" == "CLANGARM64" ]]; then
  export OPTIONAL_PACKAGES="mingw-w64-$MSYS2_ARCH-lua51"
else
  export OPTIONAL_PACKAGES="mingw-w64-$MSYS2_ARCH-luajit"
fi

# Update everything
pacman --noconfirm -Suy

# Install the required packages
pacman --noconfirm -S --needed \
    base-devel \
    mingw-w64-$MSYS2_ARCH-binutils \
    mingw-w64-$MSYS2_ARCH-toolchain \
    mingw-w64-$MSYS2_ARCH-ccache \
    \
    $OPTIONAL_PACKAGES \
    mingw-w64-$MSYS2_ARCH-lua51-lgi \
    \
    mingw-w64-$MSYS2_ARCH-aalib \
    mingw-w64-$MSYS2_ARCH-appstream-glib \
    mingw-w64-$MSYS2_ARCH-atk \
    mingw-w64-$MSYS2_ARCH-brotli \
    mingw-w64-$MSYS2_ARCH-cairo \
    mingw-w64-$MSYS2_ARCH-cfitsio \
    mingw-w64-$MSYS2_ARCH-drmingw \
    mingw-w64-$MSYS2_ARCH-gexiv2 \
    mingw-w64-$MSYS2_ARCH-ghostscript \
    mingw-w64-$MSYS2_ARCH-glib2 \
    mingw-w64-$MSYS2_ARCH-glib-networking \
    mingw-w64-$MSYS2_ARCH-gobject-introspection \
    mingw-w64-$MSYS2_ARCH-gobject-introspection-runtime \
    mingw-w64-$MSYS2_ARCH-graphviz \
    mingw-w64-$MSYS2_ARCH-gtk3 \
    mingw-w64-$MSYS2_ARCH-iso-codes \
    mingw-w64-$MSYS2_ARCH-json-c \
    mingw-w64-$MSYS2_ARCH-json-glib \
    mingw-w64-$MSYS2_ARCH-lcms2 \
    mingw-w64-$MSYS2_ARCH-lensfun \
    mingw-w64-$MSYS2_ARCH-libarchive \
    mingw-w64-$MSYS2_ARCH-libheif \
    mingw-w64-$MSYS2_ARCH-libiff \
    mingw-w64-$MSYS2_ARCH-libilbm \
    mingw-w64-$MSYS2_ARCH-libjxl \
    mingw-w64-$MSYS2_ARCH-libmypaint \
    mingw-w64-$MSYS2_ARCH-libspiro \
    mingw-w64-$MSYS2_ARCH-libwebp \
    mingw-w64-$MSYS2_ARCH-libwmf \
    mingw-w64-$MSYS2_ARCH-maxflow \
    mingw-w64-$MSYS2_ARCH-mypaint-brushes \
    mingw-w64-$MSYS2_ARCH-openexr \
    mingw-w64-$MSYS2_ARCH-pango \
    mingw-w64-$MSYS2_ARCH-poppler \
    mingw-w64-$MSYS2_ARCH-poppler-data \
    mingw-w64-$MSYS2_ARCH-python \
    mingw-w64-$MSYS2_ARCH-python3-gobject \
    mingw-w64-$MSYS2_ARCH-qoi \
    mingw-w64-$MSYS2_ARCH-shared-mime-info \
    mingw-w64-$MSYS2_ARCH-suitesparse \
    mingw-w64-$MSYS2_ARCH-vala \
    mingw-w64-$MSYS2_ARCH-xpm-nox

export PIKA_PREFIX="`realpath ./_install`${ARTIFACTS_SUFFIX}"
export PATH="$PIKA_PREFIX/bin:$PATH"

# Package ressources.
mkdir -p ${PIKA_DISTRIB}
cp -fr ${PIKA_PREFIX}/etc ${PIKA_DISTRIB}
cp -fr ${PIKA_PREFIX}/include ${PIKA_DISTRIB}
#cp -fr ${PIKA_PREFIX}/ssl ${PIKA_DISTRIB}
cp -fr ${PIKA_PREFIX}/share ${PIKA_DISTRIB}

# Package executables.
mkdir ${PIKA_DISTRIB}/bin
cp -fr ${PIKA_PREFIX}/bin/pika*.exe ${PIKA_DISTRIB}/bin/

# With the native Windows build, it's directly in bin/
#mkdir ${PIKA_DISTRIB}/libexec
#cp -fr ${PIKA_PREFIX}/libexec/pika*.exe ${PIKA_DISTRIB}/libexec/

# Add a wrapper at tree root, less messy than having to look for the
# binary inside bin/, in the middle of all the DLLs.
echo "bin\pika-2.99.exe" > ${PIKA_DISTRIB}/pika.cmd

# Package library data and modules.
cp -fr ${MSYS_PREFIX}/etc/fonts ${PIKA_DISTRIB}/etc/
cp -fr ${MSYS_PREFIX}/etc/gtk-3.0 ${PIKA_DISTRIB}/etc/

mkdir ${PIKA_DISTRIB}/lib/
cp -fr ${PIKA_PREFIX}/lib/pika ${PIKA_DISTRIB}/lib/
cp -fr ${PIKA_PREFIX}/lib/gegl-0.4 ${PIKA_DISTRIB}/lib/
cp -fr ${PIKA_PREFIX}/lib/babl-0.1 ${PIKA_DISTRIB}/lib/

cp -fr ${MSYS_PREFIX}/lib/girepository-1.0 ${PIKA_DISTRIB}/lib/
cp -fr ${PIKA_PREFIX}/lib/girepository-1.0/* ${PIKA_DISTRIB}/lib/girepository-1.0/

cp -fr ${MSYS_PREFIX}/lib/gio ${PIKA_DISTRIB}/lib/
cp -fr ${MSYS_PREFIX}/lib/gdk-pixbuf-2.0 ${PIKA_DISTRIB}/lib/
cp -fr ${MSYS_PREFIX}/lib/gtk-3.0 ${PIKA_DISTRIB}/lib/

cp -fr ${MSYS_PREFIX}/lib/python3.11 ${PIKA_DISTRIB}/lib/

cp -fr ${MSYS_PREFIX}/share/ghostscript ${PIKA_DISTRIB}/share/
cp -fr ${MSYS_PREFIX}/share/glib-2.0 ${PIKA_DISTRIB}/share/
cp -fr ${MSYS_PREFIX}/share/libthai ${PIKA_DISTRIB}/share/
cp -fr ${MSYS_PREFIX}/share/libwmf ${PIKA_DISTRIB}/share/
cp -fr ${MSYS_PREFIX}/share/mypaint-data ${PIKA_DISTRIB}/share/
cp -fr ${MSYS_PREFIX}/share/poppler ${PIKA_DISTRIB}/share/

cp -fr ${MSYS_PREFIX}/share/lua/ ${PIKA_DISTRIB}/share/
cp -fr ${MSYS_PREFIX}/lib/lua/ ${PIKA_DISTRIB}/lib/

# XXX Are these themes really needed?
cp -fr ${MSYS_PREFIX}/share/themes ${PIKA_DISTRIB}/share/

# Only copy from langs supported in PIKA.
for dir in ${PIKA_DISTRIB}/share/locale/*/; do
  lang=`basename "$dir"`;
  # TODO: ideally we could be a bit more accurate and copy only the
  # language files from our dependencies and iso_639.mo. But let's go
  # with this for now, especially as each lang may have different
  # translation availability.
  if [ -d "${MSYS_PREFIX}/share/locale/${lang}/LC_MESSAGES/" ]; then
    cp -fr "${MSYS_PREFIX}/share/locale/${lang}/LC_MESSAGES/"*.mo "${PIKA_DISTRIB}/share/locale/${lang}/LC_MESSAGES/"
  fi
done;

# Only one iso-codes file is useful.
mkdir -p ${PIKA_DISTRIB}/share/xml/iso-codes
cp -fr ${MSYS_PREFIX}/share/xml/iso-codes/iso_639.xml ${PIKA_DISTRIB}/share/xml/iso-codes/

# Adwaita can be used as the base icon set.
cp -fr ${MSYS_PREFIX}/share/icons/Adwaita ${PIKA_DISTRIB}/share/icons/

# XXX Why are these for exactly?
cp -fr ${MSYS_PREFIX}/bin/gspawn*.exe ${PIKA_DISTRIB}/bin/

# We save the list of already copied DLLs to keep a state between dll_link runs.
rm -f done-dll.list

python3 build/windows/gitlab-ci/dll_link.py ${PIKA_DISTRIB}/bin/gspawn-win*-helper.exe ${PIKA_PREFIX}/ ${MSYS_PREFIX} ${PIKA_DISTRIB} --output-dll-list done-dll.list
python3 build/windows/gitlab-ci/dll_link.py ${PIKA_DISTRIB}/bin/gspawn-win*-helper-console.exe ${PIKA_PREFIX}/ ${MSYS_PREFIX}/ ${PIKA_DISTRIB} --output-dll-list done-dll.list

# XXX Does not look like it's needed anymore. Check?
cp -fr ${MSYS_PREFIX}/bin/gdk-pixbuf-query-loaders.exe ${PIKA_DISTRIB}/bin/
python3 build/windows/gitlab-ci/dll_link.py ${PIKA_DISTRIB}/bin/gdk-pixbuf-query-loaders.exe ${PIKA_PREFIX}/ ${MSYS_PREFIX}/ ${PIKA_DISTRIB} --output-dll-list done-dll.list

# XXX Why is bzip2.exe needed?
cp -fr ${MSYS_PREFIX}/bin/bzip2.exe ${PIKA_DISTRIB}/bin/
python3 build/windows/gitlab-ci/dll_link.py ${PIKA_DISTRIB}/bin/bzip2.exe ${PIKA_PREFIX}/ ${MSYS_PREFIX}/ ${PIKA_DISTRIB} --output-dll-list done-dll.list

# Executables for supported interpreters.
cp -fr ${MSYS_PREFIX}/bin/pythonw.exe ${PIKA_DISTRIB}/bin/
python3 build/windows/gitlab-ci/dll_link.py ${PIKA_DISTRIB}/bin/pythonw.exe ${PIKA_PREFIX}/ ${MSYS_PREFIX}/ ${PIKA_DISTRIB} --output-dll-list done-dll.list
cp -fr ${MSYS_PREFIX}/bin/python3w.exe ${PIKA_DISTRIB}/bin/
python3 build/windows/gitlab-ci/dll_link.py ${PIKA_DISTRIB}/bin/python3w.exe ${PIKA_PREFIX}/ ${MSYS_PREFIX}/ ${PIKA_DISTRIB} --output-dll-list done-dll.list
cp -fr ${MSYS_PREFIX}/bin/python3.exe ${PIKA_DISTRIB}/bin/
python3 build/windows/gitlab-ci/dll_link.py ${PIKA_DISTRIB}/bin/python3.exe ${PIKA_PREFIX}/ ${MSYS_PREFIX}/ ${PIKA_DISTRIB} --output-dll-list done-dll.list

if [[ "$MSYSTEM" == "CLANGARM64" ]]; then
  cp -fr ${MSYS_PREFIX}/bin/lua5.1.exe ${PIKA_DISTRIB}/bin/
  python3 build/windows/gitlab-ci/dll_link.py ${PIKA_DISTRIB}/bin/lua5.1.exe ${PIKA_PREFIX}/ ${MSYS_PREFIX}/ ${PIKA_DISTRIB} --output-dll-list done-dll.list
else
  cp -fr ${MSYS_PREFIX}/bin/luajit.exe ${PIKA_DISTRIB}/bin/
  python3 build/windows/gitlab-ci/dll_link.py ${PIKA_DISTRIB}/bin/luajit.exe ${PIKA_PREFIX}/ ${MSYS_PREFIX}/ ${PIKA_DISTRIB} --output-dll-list done-dll.list
fi

# Executable for "gegl:introspect" from graphviz package.
cp -fr ${MSYS_PREFIX}/bin/dot.exe ${PIKA_DISTRIB}/bin/
python3 build/windows/gitlab-ci/dll_link.py ${PIKA_DISTRIB}/bin/dot.exe ${PIKA_PREFIX}/ ${MSYS_PREFIX}/ ${PIKA_DISTRIB} --output-dll-list done-dll.list

# Generate share/glib-2.0/schemas/gschemas.compiled
glib-compile-schemas --targetdir=${PIKA_DISTRIB}/share/glib-2.0/schemas ${PIKA_DISTRIB}/share/glib-2.0/schemas

# Package needed DLLs only
python3 build/windows/gitlab-ci/dll_link.py ${PIKA_DISTRIB}/bin/pika-2.99.exe ${PIKA_PREFIX}/ ${MSYS_PREFIX}/ ${PIKA_DISTRIB} --output-dll-list done-dll.list

python3 build/windows/gitlab-ci/dll_link.py ${PIKA_DISTRIB}/bin/pika-console-2.99.exe ${PIKA_PREFIX}/ ${MSYS_PREFIX}/ ${PIKA_DISTRIB} --output-dll-list done-dll.list

python3 build/windows/gitlab-ci/dll_link.py ${PIKA_DISTRIB}/bin/pika-debug-resume.exe ${PIKA_PREFIX}/ ${MSYS_PREFIX}/ ${PIKA_DISTRIB} --output-dll-list done-dll.list

python3 build/windows/gitlab-ci/dll_link.py ${PIKA_DISTRIB}/bin/pika-debug-tool-2.99.exe ${PIKA_PREFIX}/ ${MSYS_PREFIX}/ ${PIKA_DISTRIB} --output-dll-list done-dll.list

python3 build/windows/gitlab-ci/dll_link.py ${PIKA_DISTRIB}/bin/pika-test-clipboard-2.99.exe ${PIKA_PREFIX}/ ${MSYS_PREFIX}/ ${PIKA_DISTRIB} --output-dll-list done-dll.list

python3 build/windows/gitlab-ci/dll_link.py ${PIKA_DISTRIB}/bin/pikatool-2.99.exe ${PIKA_PREFIX}/ ${MSYS_PREFIX}/ ${PIKA_DISTRIB} --output-dll-list done-dll.list

for dll in ${PIKA_DISTRIB}/lib/babl-0.1/*.dll; do
  python3 build/windows/gitlab-ci/dll_link.py $dll ${PIKA_PREFIX}/ ${MSYS_PREFIX}/ ${PIKA_DISTRIB} --output-dll-list done-dll.list;
done
for dll in ${PIKA_DISTRIB}/lib/gegl-0.4/*.dll; do
  python3 build/windows/gitlab-ci/dll_link.py $dll ${PIKA_PREFIX}/ ${MSYS_PREFIX}/ ${PIKA_DISTRIB} --output-dll-list done-dll.list;
done
for dll in ${PIKA_DISTRIB}/lib/gio/modules/*.dll; do
  python3 build/windows/gitlab-ci/dll_link.py $dll ${PIKA_PREFIX}/ ${MSYS_PREFIX}/ ${PIKA_DISTRIB} --output-dll-list done-dll.list;
done
for dll in ${PIKA_DISTRIB}/lib/gdk-pixbuf-2.0/2.10.0/loaders/*.dll; do
  python3 build/windows/gitlab-ci/dll_link.py $dll ${PIKA_PREFIX}/ ${MSYS_PREFIX}/ ${PIKA_DISTRIB} --output-dll-list done-dll.list;
done
for dll in ${PIKA_DISTRIB}/lib/pika/2.99/modules/*.dll; do
  python3 build/windows/gitlab-ci/dll_link.py $dll ${PIKA_PREFIX}/ ${MSYS_PREFIX}/ ${PIKA_DISTRIB} --output-dll-list done-dll.list;
done
for dll in ${PIKA_DISTRIB}/lib/pika/2.99/plug-ins/*/*.exe; do
  python3 build/windows/gitlab-ci/dll_link.py $dll ${PIKA_PREFIX}/ ${MSYS_PREFIX}/ ${PIKA_DISTRIB} --output-dll-list done-dll.list;
done

# Libraries for GObject Introspection.

cp -fr ${MSYS_PREFIX}/bin/libgirepository-1.0-1.dll ${PIKA_DISTRIB}/bin/
python3 build/windows/gitlab-ci/dll_link.py ${PIKA_DISTRIB}/bin/libgirepository-1.0-1.dll ${PIKA_PREFIX}/ ${MSYS_PREFIX}/ ${PIKA_DISTRIB} --output-dll-list done-dll.list

for dll in ${PIKA_DISTRIB}/lib/python3.11/site-packages/*/*.dll; do
  python3 build/windows/gitlab-ci/dll_link.py $dll ${PIKA_PREFIX}/ ${MSYS_PREFIX}/ ${PIKA_DISTRIB} --output-dll-list done-dll.list;
done
