#!/bin/bash
# -*- coding: utf-8 -*-
# This file is part of Xpra.
# Copyright (C) 2017 Antoine Martin <antoine@xpra.org>
# Xpra is released under the terms of the GNU GPL v2, or, at your option, any
# later version. See the file COPYING for details.

set -e

export XPKG="${MINGW_PACKAGE_PREFIX}-"
PACMAN=${PACMAN:-"pacman --noconfirm --needed -S"}
#PACMAN="echo pacman"

#most packages get installed here: (python, gtk, etc):
$PACMAN ${XPKG}python ${XPKG}libnotify ${XPKG}gtk3
#media libraries (more than we actually need):
$PACMAN ${XPKG}libavif ${XPKG}libyuv-git ${XPKG}gst-plugins-good ${XPKG}gst-plugins-bad ${XPKG}gst-plugins-ugly
#more codecs:
$PACMAN ${XPKG}libx264 ${XPKG}libvpx ${XPKG}openh264 ${XPKG}dav1d ${XPKG}aom ${XPKG}libwebp
#network layer libraries:
$PACMAN ${XPKG}lz4 ${XPKG}xxhash heimdal-libs openssh sshpass ${XPKG}libsodium
#pinentry is not available for aarch64 yet:
$PACMAN ${XPKG}pinentry
#make qr codes:
$PACMAN ${XPKG}qrencode
#not strictly needed:
$PACMAN ${XPKG}dbus-glib
#python GStreamer bindings:
$PACMAN ${XPKG}gst-python
#development tools and libs for building extra packages:
$PACMAN base-devel ${XPKG}yasm ${XPKG}nasm gcc groff subversion rsync zip gtk-doc ${XPKG}cmake ${XPKG}gcc ${XPKG}pkgconf ${XPKG}libffi ${XPKG}python-pandocfilters
#python extensions:
for x in cryptography cffi pycparser numpy pillow appdirs paramiko comtypes netifaces setproctitle pyu2f fido2 ldap ldap3 bcrypt pynacl pyopengl pyopengl-accelerate nvidia-ml zeroconf certifi yaml py-cpuinfo winkerberos coverage psutil oauthlib pysocks pyopenssl importlib_resources pylsqpack aioquic service_identity pyvda watchdog pyqt6 wmi winloop pyglet; do
	$PACMAN ${XPKG}python-${x}
done
#not yet available for aarch64?:
for x in cx-freeze gssapi; do
	$PACMAN ${XPKG}python-${x}
done
$PACMAN ${XPKG}amf-headers

#dependencies of browser_cookie3 and pycuda,
#best to manage them via pacman rather than have them installed via pip,
#so we get automatic updates:
#(pycryptodome* is not yet available for aarch64?)
for x in mako markupsafe typing_extensions platformdirs pip pycryptodome pycryptodomex keyring idna; do
	$PACMAN ${XPKG}python-${x}
done
$PACMAN ${XPKG}cython
$PACMAN openssl-devel
#these need to be converted to PKGBUILD:
for x in browser-cookie3 pyaes pbkdf2 pytools; do
	pip3 install --break-system-packages $x
done
# to keep these libraries updated, you may need:
# SETUPTOOLS_USE_DISTUTILS=stdlib pip install --upgrade $PACKAGE

curl -sL "https://api.nuget.org/v3-flatcontainer/verpatch/1.0.14/verpatch.1.0.14.nupkg" -o "verpatch.nupkg"
python -c "import zipfile; zipfile.ZipFile('verpatch.nupkg').extract('lib/win/verpatch.exe', '.')"
mv lib/win/verpatch.exe "$MINGW_PREFIX/bin/"
rmdir lib/win && rmdir lib
rm "verpatch.nupkg"

pushd mingw-w64-pdfium-bin
rm ./mingw-*-pdfium-bin-*.pkg.tar.*
makepkg -sCLf
pacman --noconfirm -U ./mingw-*-pdfium-bin-*.pkg.tar.*
popd

# InnoSetup:
INNO_VERSION="6.7.1"
INNO_TAG="is-$(echo "${INNO_VERSION}" | tr '.' '_')"
INSTALLER="innosetup-${INNO_VERSION}.exe"
DOWNLOAD_URL="https://github.com/jrsoftware/issrc/releases/download/${INNO_TAG}/${INSTALLER}"
INSTALL_DIR="C:\\Program Files (x86)\\Inno Setup 6"
if [[ ! -f "$INSTALLER" ]]; then
    echo "Downloading Inno Setup ${INNO_VERSION}..."
    curl -fL -o "$INSTALLER" "$DOWNLOAD_URL"
fi
echo "Installing Inno Setup ${INNO_VERSION}..."
./"$INSTALLER" \
    //VERYSILENT \
    //NORESTART \
    //SUPPRESSMSGBOXES \
    //DIR="${INSTALL_DIR}"

# pandoc:
PANDOC_VERSION="3.9"
BASE_URL="https://github.com/jgm/pandoc/releases/download/${PANDOC_VERSION}"
ARCHIVE="pandoc-${PANDOC_VERSION}-windows-x86_64.zip"
DOWNLOAD_URL="${BASE_URL}/${ARCHIVE}"
if [[ ! -f "$ARCHIVE" ]]; then
    echo "Downloading Pandoc ${PANDOC_VERSION}..."
    curl -fL -o "$ARCHIVE" "$DOWNLOAD_URL"
fi
echo "Installing Pandoc ${PANDOC_VERSION} to ${INSTALL_DIR}..."
$PACMAN ${XPKG}unzip
unzip -o "$ARCHIVE" "pandoc-${PANDOC_VERSION}/pandoc.exe" -d pandoc_tmp
mv "pandoc_tmp/pandoc-${PANDOC_VERSION}/pandoc.exe" "${MINGW_PREFIX}/bin/"
rm -rf pandoc_tmp

echo "to generate the MSI, install MSIWrapper:"
echo "https://www.exemsi.com/"
echo
echo "to support NVIDIA hardware accelerated encoders NVENC, NVJPEG"
echo "and NVFBC screen capture:"
echo "* install CUDA in its default location"
echo "* create symbolic links so the build system can find CUDA more easily:"
echo " 'pushd /c/Program\ Files/NVIDIA\ GPU\ Computing\ Toolkit/CUDA/;ln -sf v12.0 current;popd'"
echo " 'ln -sf /c/Program\ Files/NVIDIA\ GPU\ Computing\ Toolkit/CUDA/current ./cuda'"
echo "* install 'NVidia_Capture' into '$MINGW_PREFIX/lib/nvenc'"
echo "* add the pkg-config files:"
echo " 'cp pkgconfig/*.pc $MINGW_PREFIX/lib/pkgconfig/'"
echo "* install python-setuptools python-numpy python-pip"
echo
echo "for SBOM, run SETUP_SBOM.sh"
echo
