#!/usr/bin/env bash
# Cross compile for windows from Linux:
#
# Known to work at Ubuntu 23.04
set -e

if ! test -f ./configure || test $# != 1; then echo "Please execute from the main project directory like this:"; echo "mingw/build.sh x86_64-w64-mingw32    (or i686-w64-mingw32 for a 32bit build)"; exit 1; fi
if test "$1" != "x86_64-w64-mingw32" && test "$1" != "i686-w64-mingw32"; then echo "only supported targets are x86_64-w64-mingw32 and i686-w64-mingw32. $1 given"; exit 1; fi

. mingw/versionsconf.sh

test -f ./Makefile && make clean || true

if apt-get -s  install mingw-w64-tools mingw-w64 libz-mingw-w64-dev nasm cmake | grep -E -q "^(Inst|Conf|Remv)"; then
    sudo apt-get install mingw-w64-tools mingw-w64 libz-mingw-w64-dev nasm cmake
fi

host=$1
if test "$host" == "i686-w64-mingw32"; then
 if test `readlink -m \`which i686-w64-mingw32-g++\`|sed -e 's/\/.*\///'` != "i686-w64-mingw32-g++-posix"; then
  sudo update-alternatives --set i686-w64-mingw32-g++ /usr/bin/i686-w64-mingw32-g++-posix
 fi
 if test `readlink -m \`which i686-w64-mingw32-gcc\`|sed -e 's/\/.*\///'` != "i686-w64-mingw32-gcc-posix"; then
  sudo update-alternatives --set i686-w64-mingw32-gcc /usr/bin/i686-w64-mingw32-gcc-posix
 fi
else
 if test `readlink -m \`which x86_64-w64-mingw32-g++\`|sed -e 's/\/.*\///'` != "x86_64-w64-mingw32-g++-posix"; then
  sudo update-alternatives --set x86_64-w64-mingw32-g++ /usr/bin/x86_64-w64-mingw32-g++-posix
 fi
 if test `readlink -m \`which x86_64-w64-mingw32-gcc\`|sed -e 's/\/.*\///'` != "x86_64-w64-mingw32-gcc-posix"; then
  sudo update-alternatives --set x86_64-w64-mingw32-gcc /usr/bin/x86_64-w64-mingw32-gcc-posix
 fi
fi

install=`pwd`/$host-install
prefix=`pwd`/$host-prefix
gccversion=`$host-gcc --version|head -n 1|sed -e 's/.*\ //'`
mkdir -p $host-src
mkdir -p $host-prefix/lib
mkdir -p $host-prefix/include
cd $host-src

generate_toolchain_cmake () {
  if test "$host" == "i686-w64-mingw32"; then
    cat <<EOF > toolchain.cmake
set(CMAKE_SYSTEM_NAME Windows)
set(CMAKE_SYSTEM_PROCESSOR X86)
set(CMAKE_C_COMPILER /usr/bin/i686-w64-mingw32-gcc)
set(CMAKE_CXX_COMPILER /usr/bin/i686-w64-mingw32-g++)
set(CMAKE_RC_COMPILER /usr/bin/i686-w64-mingw32-windres)
EOF
  else
    cat <<EOF > toolchain.cmake
set(CMAKE_SYSTEM_NAME Windows)
set(CMAKE_SYSTEM_PROCESSOR AMD64)
set(CMAKE_C_COMPILER /usr/bin/x86_64-w64-mingw32-gcc)
set(CMAKE_CXX_COMPILER /usr/bin/x86_64-w64-mingw32-g++)
set(CMAKE_RC_COMPILER /usr/bin/x86_64-w64-mingw32-windres)
EOF
  fi
}

do_cmake () {
  generate_toolchain_cmake
  mkdir -p build
  cd build
  PKG_CONFIG=$host-pkg-config \
  PKG_CONFIG_PATH=$prefix/lib/pkgconfig/ \
  cmake -G"Unix Makefiles" \
        -DCMAKE_TOOLCHAIN_FILE=toolchain.cmake \
        -DCMAKE_INSTALL_PREFIX=$prefix \
        -DCMAKE_INCLUDE_PATH=$prefix/include \
        -DBUILD_SHARED_LIBS=ON \
        -DCMAKE_BUILD_TYPE=Release \
        "$@" \
        ..
  make all install
  cd ..
}

WGET="wget --tries=20 --waitretry=5 --retry-connrefused"

#test -f ax_cxx_compile_stdcxx.m4 || ( sleep 5 && $WGET 'https://gitweb.git.savannah.gnu.org/gitweb/?p=autoconf-archive.git;a=blob_plain;f=m4/ax_cxx_compile_stdcxx.m4' -O ax_cxx_compile_stdcxx.m4 )
#test -f ax_check_compile_flag.m4 || ( sleep 5 && $WGET 'https://gitweb.git.savannah.gnu.org/gitweb/?p=autoconf-archive.git;a=blob_plain;f=m4/ax_check_compile_flag.m4' -O ax_check_compile_flag.m4 )
#test -f ax_cflags_warn_all.m4    || ( sleep 5 && $WGET 'https://gitweb.git.savannah.gnu.org/gitweb/?p=autoconf-archive.git;a=blob_plain;f=m4/ax_cflags_warn_all.m4' -O ax_cflags_warn_all.m4 )
#test -f ax_compiler_vendor.m4    || ( sleep 5 && $WGET 'https://gitweb.git.savannah.gnu.org/gitweb/?p=autoconf-archive.git;a=blob_plain;f=m4/ax_compiler_vendor.m4' -O ax_compiler_vendor.m4 )
#test -f ax_prepend_flag.m4       || ( sleep 5 && $WGET 'https://gitweb.git.savannah.gnu.org/gitweb/?p=autoconf-archive.git;a=blob_plain;f=m4/ax_prepend_flag.m4' -O ax_prepend_flag.m4 )
#test -f ax_require_defined.m4    || ( sleep 5 && $WGET 'https://gitweb.git.savannah.gnu.org/gitweb/?p=autoconf-archive.git;a=blob_plain;f=m4/ax_require_defined.m4' -O ax_require_defined.m4 )
test -f ax_cxx_compile_stdcxx.m4 || ( sleep 5 && $WGET 'https://raw.githubusercontent.com/autoconf-archive/autoconf-archive/refs/heads/master/m4/ax_cxx_compile_stdcxx.m4' -O ax_cxx_compile_stdcxx.m4 )
test -f ax_check_compile_flag.m4 || ( sleep 5 && $WGET 'https://raw.githubusercontent.com/autoconf-archive/autoconf-archive/refs/heads/master/m4/ax_check_compile_flag.m4' -O ax_check_compile_flag.m4 )
test -f ax_cflags_warn_all.m4    || ( sleep 5 && $WGET 'https://raw.githubusercontent.com/autoconf-archive/autoconf-archive/refs/heads/master/m4/ax_cflags_warn_all.m4' -O ax_cflags_warn_all.m4 )
test -f ax_compiler_vendor.m4    || ( sleep 5 && $WGET 'https://raw.githubusercontent.com/autoconf-archive/autoconf-archive/refs/heads/master/m4/ax_compiler_vendor.m4' -O ax_compiler_vendor.m4 )
test -f ax_prepend_flag.m4       || ( sleep 5 && $WGET 'https://raw.githubusercontent.com/autoconf-archive/autoconf-archive/refs/heads/master/m4/ax_prepend_flag.m4' -O ax_prepend_flag.m4 )
test -f ax_require_defined.m4    || ( sleep 5 && $WGET 'https://raw.githubusercontent.com/autoconf-archive/autoconf-archive/refs/heads/master/m4/ax_require_defined.m4' -O ax_require_defined.m4 )

test -f bzip2-$BZIP2_VERSION.tar.gz                 || $WGET https://sourceware.org/pub/bzip2/bzip2-$BZIP2_VERSION.tar.gz -O bzip2-$BZIP2_VERSION.tar.gz                                                                                     || rm bzip2-$BZIP2_VERSION.tar.gz                 || false
test -f libmad-$LIBMAD_VERSION.tar.gz               || $WGET https://sourceforge.net/projects/mad/files/libmad/$LIBMAD_VERSION/libmad-$LIBMAD_VERSION.tar.gz/download -O libmad-$LIBMAD_VERSION.tar.gz                                       || rm libmad-$LIBMAD_VERSION.tar.gz               || false
test -f libjpeg-turbo-$LIBJPEGTURBO_VERSION.tar.gz  || $WGET https://github.com/libjpeg-turbo/libjpeg-turbo/releases/download/$LIBJPEGTURBO_VERSION/libjpeg-turbo-$LIBJPEGTURBO_VERSION.tar.gz -O libjpeg-turbo-$LIBJPEGTURBO_VERSION.tar.gz || rm libjpeg-turbo-$LIBJPEGTURBO_VERSION.tar.gz  || false
test -f libpng-$LIBPNG_VERSION.tar.gz               || $WGET https://download.sourceforge.net/libpng/libpng-$LIBPNG_VERSION.tar.gz -O libpng-$LIBPNG_VERSION.tar.gz                                                                          || rm libpng-$LIBPNG_VERSION.tar.gz               || false
test -f libogg-$LIBOGG_VERSION.tar.gz               || $WGET https://downloads.xiph.org/releases/ogg/libogg-$LIBOGG_VERSION.tar.gz -O libogg-$LIBOGG_VERSION.tar.gz                                                                          || rm libogg-$LIBOGG_VERSION.tar.gz               || false
test -f libvorbis-$LIBVORBIS_VERSION.tar.gz         || $WGET https://downloads.xiph.org/releases/vorbis/libvorbis-$LIBVORBIS_VERSION.tar.gz -O libvorbis-$LIBVORBIS_VERSION.tar.gz                                                           || rm libvorbis-$LIBVORBIS_VERSION.tar.gz         || false
test -f flac-$FLAC_VERSION.tar.xz                   || $WGET https://ftp.osuosl.org/pub/xiph/releases/flac/flac-$FLAC_VERSION.tar.xz -O flac-$FLAC_VERSION.tar.xz                                                                            || rm flac-$FLAC_VERSION.tar.xz                   || false
test -f opus-$OPUS_VERSION.tar.gz                   || $WGET https://downloads.xiph.org/releases/opus/opus-$OPUS_VERSION.tar.gz -O opus-$OPUS_VERSION.tar.gz                                                                                 || rm opus-$OPUS_VERSION.tar.gz                   || false
test -f speex-$SPEEX_VERSION.tar.gz                 || $WGET https://downloads.xiph.org/releases/speex/speex-$SPEEX_VERSION.tar.gz -O speex-$SPEEX_VERSION.tar.gz                                                                            || rm speex-$SPEEX_VERSION.tar.gz                 || false
test -f SDL3-devel-$SDL3_VERSION-mingw.tar.gz       || $WGET https://github.com/libsdl-org/SDL/releases/download/release-$SDL3_VERSION/SDL3-devel-$SDL3_VERSION-mingw.tar.gz -O SDL3-devel-$SDL3_VERSION-mingw.tar.gz                        || rm SDL3-devel-$SDL3_VERSION-mingw.tar.gz       || false
if test "$host" == "i686-w64-mingw32"; then
  test -f brotli-$BROTLI_VERSION.tar.gz             || $WGET https://github.com/google/brotli/archive/refs/tags/v$BROTLI_VERSION.tar.gz -O brotli-$BROTLI_VERSION.tar.gz                                                                     || rm brotli-$BROTLI_VERSION.tar.gz               || false
fi
test -f harfbuzz-$HARFBUZZ_VERSION.tar.xz           || $WGET https://github.com/harfbuzz/harfbuzz/releases/download/$HARFBUZZ_VERSION/harfbuzz-$HARFBUZZ_VERSION.tar.xz -O harfbuzz-$HARFBUZZ_VERSION.tar.xz                                 || rm harfbuzz-$HARFBUZZ_VERSION.tar.xz           || false
test -f freetype-$FREETYPE2_VERSION.tar.gz          || $WGET https://sourceforge.net/projects/freetype/files/freetype2/$FREETYPE2_VERSION/freetype-$FREETYPE2_VERSION.tar.gz/download -O freetype-$FREETYPE2_VERSION.tar.gz                  || rm freetype-$FREETYPE2_VERSION.tar.gz          || false
#$WGET http://ftp.eu.metabrainz.org/pub/musicbrainz/libdiscid/libdiscid-$LIBDISCID_VERSION-win.zip -O libdiscid-$LIBDISCID_VERSION-win.zip
test -f libdiscid-$LIBDISCID_VERSION-win.zip        || $WGET https://github.com/metabrainz/libdiscid/releases/download/v$LIBDISCID_VERSION/libdiscid-$LIBDISCID_VERSION-win.zip -O libdiscid-$LIBDISCID_VERSION-win.zip                      || rm libdiscid-$LIBDISCID_VERSION-win.zip        || false
test -f cJSON-$CJSON_VERSION.tar.gz                 || $WGET https://github.com/DaveGamble/cJSON/archive/refs/tags/v$CJSON_VERSION.tar.gz -O cJSON-$CJSON_VERSION.tar.gz                                                                     || rm cJSON-$CJSON_VERSION.tar.gz                 || false
test -f ancient-$ANCIENT_VERSION.tar.gz             || $WGET https://github.com/temisu/ancient/archive/refs/tags/v$ANCIENT_VERSION.tar.gz -O ancient-$ANCIENT_VERSION.tar.gz                                                                 || rm ancient-$ANCIENT_VERSION.tar.gz             || false
test -f libiconv-$LIBICONV_VERSION.tar.gz           || $WGET https://ftp.gnu.org/pub/gnu/libiconv/libiconv-$LIBICONV_VERSION.tar.gz -O libiconv-$LIBICONV_VERSION.tar.gz                                                                     || rm libiconv-$LIBICONV_VERSION.tar.gz           || false
test -f game-music-emu-$GAMEMUSICEMU_VERSION.tar.gz || $WGET https://github.com/libgme/game-music-emu/archive/refs/tags/$GAMEMUSICEMU_VERSION.tar.gz -O game-music-emu-$GAMEMUSICEMU_VERSION.tar.gz                                 || rm game-music-emu-$GAMEMUSICEMU_VERSION.tar.gz || false
# Future releases will be here https://github.com/libgme/game-music-emu/tags
test -f unifont-$UNIFONT_VERSION.otf                || $WGET https://unifoundry.com/pub/unifont/unifont-$UNIFONT_VERSION/font-builds/unifont-$UNIFONT_VERSION.otf       -O unifont-$UNIFONT_VERSION.otf                                      || rm unifont-$UNIFONT_VERSION.otf                || false
test -f unifont_csur-$UNIFONT_VERSION.otf           || $WGET https://unifoundry.com/pub/unifont/unifont-$UNIFONT_VERSION/font-builds/unifont_csur-$UNIFONT_VERSION.otf  -O unifont_csur-$UNIFONT_VERSION.otf                                 || rm unifont_csur-$UNIFONT_VERSION.otf           || false
test -f unifont_upper-$UNIFONT_VERSION.otf          || $WGET https://unifoundry.com/pub/unifont/unifont-$UNIFONT_VERSION/font-builds/unifont_upper-$UNIFONT_VERSION.otf -O unifont_upper-$UNIFONT_VERSION.otf                                || rmunifont_upper-$UNIFONT_VERSION.otf           || false
test -f wavpack-$WAVPACK_VERSION.tar.xz             || $WGET https://github.com/dbry/WavPack/releases/download/$WAVPACK_VERSION/wavpack-$WAVPACK_VERSION.tar.xz         -O wavpack-$WAVPACK_VERSION.tar.xz                                   || rm wavpack-$WAVPACK_VERSION.tar.xz             || false

#wget https://zlib.net/zlib-1.2.13.tar.gz
#tar xfz zlib-1.2.13.tar.gz
#cd zlib-1.2.13/
#prefix=$prefix CC=$host-gcc CFLAGS="-O2" ./configure
#make
#cd ..

########## BZIP2 ##########
if ! test -f $prefix/build-bzip2-$BZIP2_VERSION; then
	dirs=`ls -d */|cut -f1|grep 'bzip2-*'` && rm -Rf $dirs
	tar xfz bzip2-$BZIP2_VERSION.tar.gz
	cd bzip2-$BZIP2_VERSION
cat <<EOF > Makefile.mingw32
CC=$host-gcc
CFLAGS=-fpic -fPIC -DWIN32 -MD -O2 -D_FILE_OFFSET_BITS=64 -DBZ_EXPORT

OBJS= blocksort.o  \\
      huffman.o    \\
      crctable.o   \\
      randtable.o  \\
      compress.o   \\
      decompress.o \\
      bzlib.o

all: bz2.dll bzip2-shared.exe

bz2.dll: \$(OBJS)
	\$(CC) -shared -Wl,--subsystem,windows -o \$@ $^

bzip2-shared.exe: bz2.dll bzip2.c
	\$(CC) \$(CFLAGS) -o \$@ bzip2.c bz2.dll

clean:
	rm -f *.o *.a *.dll *.exe

blocksort.o: blocksort.c
	\$(CC) \$(CFLAGS) -c blocksort.c
huffman.o: huffman.c
	\$(CC) \$(CFLAGS) -c huffman.c
crctable.o: crctable.c
	\$(CC) \$(CFLAGS) -c crctable.c
randtable.o: randtable.c
	\$(CC) \$(CFLAGS) -c randtable.c
compress.o: compress.c
	\$(CC) \$(CFLAGS) -c compress.c
decompress.o: decompress.c
	\$(CC) \$(CFLAGS) -c decompress.c
bzlib.o: bzlib.c
	\$(CC) \$(CFLAGS) -c bzlib.c
EOF
		patch -p 1 <<EOF
--- bzip2-1.0.8/bzlib.h	2019-07-13 19:50:05.000000000 +0200
+++ bzip2-1.0.8-new/bzlib.h	2023-06-11 01:30:31.804371512 +0200
@@ -66,8 +66,8 @@
    bz_stream;
 
 
-#ifndef BZ_IMPORT
-#define BZ_EXPORT
+#ifndef BZ_EXPORT
+#define BZ_IMPORT
 #endif
 
 #ifndef BZ_NO_STDIO
@@ -82,12 +82,12 @@
 #      undef small
 #   endif
 #   ifdef BZ_EXPORT
-#   define BZ_API(func) WINAPI func
-#   define BZ_EXTERN extern
+#   define BZ_API(func) __cdecl func
+#   define BZ_EXTERN extern __declspec(dllexport)
 #   else
    /* import windows dll dynamically */
-#   define BZ_API(func) (WINAPI * func)
-#   define BZ_EXTERN
+#   define BZ_API(func) __cdecl func
+#   define BZ_EXTERN extern __declspec(dllimport)
 #   endif
 #else
 #   define BZ_API(func) func
EOF
		make -f Makefile.mingw32
		cp bz2.dll $prefix/lib
		cp bzlib.h $prefix/include
	cd ..
	touch $prefix/build-bzip2-$BZIP2_VERSION
fi

########## MAD ##########
if ! test -f $prefix/build-libmad-$LIBMAD_VERSION; then
	dirs=`ls -d */|cut -f1|grep 'libmad-*'` && rm -Rf $dirs
	tar xfz libmad-$LIBMAD_VERSION.tar.gz
	cd libmad-$LIBMAD_VERSION
		patch -p 1 << EOF
diff -u libmad-0.15.1b/configure.ac libmad-0.15.1b-new/configure.ac
--- libmad-0.15.1b/configure.ac	2004-01-23 10:41:32.000000000 +0100
+++ libmad-0.15.1b-new/configure.ac	2023-04-07 21:37:26.401404106 +0200
@@ -51,11 +51,11 @@
 	    esac
     esac
 
-dnl    case "\$host" in
-dnl	*-*-cygwin* | *-*-mingw*)
-dnl	    LDFLAGS="\$LDFLAGS -no-undefined -mdll"
-dnl	    ;;
-dnl    esac
+    case "\$host" in
+	*-*-cygwin* | *-*-mingw*)
+	    LDFLAGS="\$LDFLAGS -no-undefined"
+	    ;;
+    esac
 fi
 
 dnl Support for libtool.
@@ -140,7 +140,7 @@
     case "\$optimize" in
 	-O|"-O "*)
 	    optimize="-O"
-	    optimize="\$optimize -fforce-mem"
+	    #optimize="\$optimize -fforce-mem"
 	    optimize="\$optimize -fforce-addr"
 	    : #x optimize="\$optimize -finline-functions"
 	    : #- optimize="\$optimize -fstrength-reduce"
EOF
		touch AUTHORS ChangeLog NEWS
		autoreconf --install
		./configure --host $host --prefix=$prefix
		make all install
		#cp .libs/libmad-0.dll $prefix/lib/mad.dll
		#cp mad.h $prefix/include
	cd ..
	touch $prefix/build-libmad-$LIBMAD_VERSION
fi

########## LIBJPEG-TURBO ##########
if ! test -f $prefix/build-libjpeg-turbo-$LIBJPEGTURBO_VERSION; then
	dirs=`ls -d */|cut -f1|grep 'libjpeg-turbo-*'` && rm -Rf $dirs
	tar xfz libjpeg-turbo-$LIBJPEGTURBO_VERSION.tar.gz
	cd libjpeg-turbo-$LIBJPEGTURBO_VERSION
		patch -p1 <<EOF
diff -ur libjpeg-turbo-3.2.0/src/spng/spng.c libjpeg-turbo-3.2.0-patch/src/spng/spng.c
--- libjpeg-turbo-3.2.0/src/spng/spng.c 2026-06-30 19:25:43.000000000 +0200
+++ libjpeg-turbo-3.2.0-patch/src/spng/spng.c   2026-08-04 14:57:28.644856706 +0200
@@ -6199,6 +6199,11 @@
     #elif SPNG_SSE == 4
         #pragma GCC target("sse4.1")
     #else
+        #define __AVX512FP16INTRIN_H_INCLUDED     /* work-around for gcc i686 (mingw on Ubuntu) */
+        #define __AVX512FP16VLINTRIN_H_INCLUDED   /* work-around for gcc i686 (mingw on Ubuntu) */
+        #define _AVX512BF16VLINTRIN_H_INCLUDED    /* work-around for gcc i686 (mingw on Ubuntu) */
+        #define _AVX512BF16INTRIN_H_INCLUDED      /* work-around for gcc i686 (mingw on Ubuntu) */
+        #define _AVXNECONVERTINTRIN_H_INCLUDED    /* work-around for gcc i686 (mingw on Ubuntu) */
         #pragma GCC target("sse2")
     #endif
 #endif
EOF
		do_cmake -DENABLE_SHARED=TRUE
	cd ..
	touch $prefix/build-libjpeg-turbo-$LIBJPEGTURBO_VERSION
fi

########## LIBPNG ##########
if ! test -f $prefix/build-libpng-$LIBPNG_VERSION; then
	dirs=`ls -d */|cut -f1|grep 'libpng-*'` && rm -Rf $dirs
	tar xfz libpng-$LIBPNG_VERSION.tar.gz
	cd libpng-$LIBPNG_VERSION
		#cmake does not use pkg-config for zlib?????
		do_cmake -DZLIB_ROOT=/usr/$host/
	cd ..
	touch $prefix/build-libpng-$LIBPNG_VERSION
fi

########## LIBOGG ##########
if ! test -f $prefix/build-libogg-$LIBOGG_VERSION; then
	dirs=`ls -d */|cut -f1|grep 'libogg-*'` && rm -Rf $dirs
	tar xfz libogg-$LIBOGG_VERSION.tar.gz
	cd libogg-$LIBOGG_VERSION
		do_cmake
	cd ..
	# Bug in build-system, missmatch with dll filename, and internal name
	mv -f $prefix/bin/libogg.dll $prefix/bin/ogg.dll
	touch $prefix/build-libogg-$LIBOGG_VERSION
fi

########## VORBIS ##########
if ! test -f $prefix/build-libvorbis$LIBVORBIS_VERSION; then
	dirs=`ls -d */|cut -f1|grep 'libvorbis-*'` && rm -Rf $dirs
	tar xfz libvorbis-$LIBVORBIS_VERSION.tar.gz
	cd libvorbis-$LIBVORBIS_VERSION
		patch -p1 << EOF
diff -ur libvorbis-1.3.7/win32/vorbis.def libvorbis-1.3.7-new/win32/vorbis.def
--- libvorbis-1.3.7/win32/vorbis.def	2020-03-23 16:04:43.000000000 +0100
+++ libvorbis-1.3.7-new/win32/vorbis.def	2023-04-09 10:02:45.550479019 +0200
@@ -1,6 +1,6 @@
 ; vorbis.def
 ; 
-LIBRARY
+LIBRARY vorbis
 EXPORTS
 _floor_P
 _mapping_P
diff -ur libvorbis-1.3.7/win32/vorbisenc.def libvorbis-1.3.7-new/win32/vorbisenc.def
--- libvorbis-1.3.7/win32/vorbisenc.def	2020-03-23 16:04:43.000000000 +0100
+++ libvorbis-1.3.7-new/win32/vorbisenc.def	2023-04-09 10:01:52.862206991 +0200
@@ -1,6 +1,6 @@
 ; vorbisenc.def
 ;
-LIBRARY
+LIBRARY vorbisenc
 
 EXPORTS
 vorbis_encode_init
diff -ur libvorbis-1.3.7/win32/vorbisfile.def libvorbis-1.3.7-new/win32/vorbisfile.def
--- libvorbis-1.3.7/win32/vorbisfile.def	2020-03-23 16:04:43.000000000 +0100
+++ libvorbis-1.3.7-new/win32/vorbisfile.def	2023-04-09 10:01:52.862206991 +0200
@@ -1,6 +1,6 @@
 ; vorbisfile.def
 ;
-LIBRARY
+LIBRARY vorbisfile
 EXPORTS
 ov_clear
 ov_open
EOF
	patch -p2 << EOF
diff --git a/libvorbis-1.3.7/CMakeLists.txt b/libvorbis-1.3.7-22ac96/CMakeLists.txt
index f377c42..b42380b 100644
--- a/libvorbis-1.3.7/CMakeLists.txt
+++ b/libvorbis-1.3.7-22ac96/CMakeLists.txt
@@ -1,4 +1,4 @@
-cmake_minimum_required(VERSION 2.8.12)
+cmake_minimum_required(VERSION 3.6)
 project(vorbis)

 list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
EOF
		do_cmake
	cd ..
# Bug in build-system, missmatch with dll filename, and internal name
	mv -f $prefix/bin/libvorbisfile.dll $prefix/bin/vorbisfile.dll
	mv -f $prefix/bin/libvorbis.dll $prefix/bin/vorbis.dll
	touch $prefix/build-libvorbis$LIBVORBIS_VERSION
fi

########## FLAC ##########
if ! test -f $prefix/build-flac-$FLAC_VERSION; then
	dirs=`ls -d */|cut -f1|grep 'flac-*'` && rm -Rf $dirs
	tar xfJ flac-$FLAC_VERSION.tar.xz
	cd flac-$FLAC_VERSION
		do_cmake -D_OGG_LIBRARY_DIRS=$prefix/lib
	cd ..
	touch $prefix/build-flac-$FLAC_VERSION
fi

########## OPUS ##########
if ! test -f $prefix/build-opus-$OPUS_VERSION; then
	dirs=`ls -d */|cut -f1|grep 'opus-*'` && rm -Rf $dirs
	tar xfz opus-$OPUS_VERSION.tar.gz
	cd opus-$OPUS_VERSION
		autoreconf --install --force
		./configure --host $host --prefix=$prefix
		make all install
	cd ..
	touch $prefix/build-opus-$OPUS_VERSION
fi

########## SPEEX ##########
if ! test -f $prefix/build-speex-$SPEEX_VERSION; then
	dirs=`ls -d */|cut -f1|grep 'speex-*'` && rm -Rf $dirs
	tar xfz speex-$SPEEX_VERSION.tar.gz
	cd speex-$SPEEX_VERSION
		autoreconf --install --force
		./configure --host $host --prefix=$prefix
		make all install
	cd ..
	touch $prefix/build-speex-$SPEEX_VERSION
fi

########## SDL3 ##########
if ! test -f $prefix/build-SDL3-$SDL3_VERSION; then
	dirs=`ls -d */|cut -f1|grep 'SDL3-*'` && rm -Rf $dirs
	tar xfz SDL3-devel-$SDL3_VERSION-mingw.tar.gz
	cd SDL3-$SDL3_VERSION
		cp $host/* $prefix -R
	cd ..
	touch $prefix/build-SDL3-$SDL3_VERSION
fi

########## BROTLI ##########, only needed for 32bit, unsure why
if ! test -f $prefix/build-brotli-$BROTLI_VERSION; then
	dirs=`ls -d */|cut -f1|grep 'brotli-*'` && rm -Rf $dirs
	if test "$host" == "i686-w64-mingw32"; then
		$WGET https://github.com/google/brotli/archive/refs/tags/v$BROTLI_VERSION.tar.gz -O brotli-$BROTLI_VERSION.tar.gz
		tar xfz brotli-$BROTLI_VERSION.tar.gz
		cd brotli-$BROTLI_VERSION
			do_cmake -DCMAKE_CXX_FLAGS=-Wa,-mbig-obj
		cd ..
	fi
	touch $prefix/build-brotli-$BROTLI_VERSION
fi

########## HARFBUZZ #########
if ! test -f $prefix/build-harfbuzz-$HARFBUZZ_VERSION; then
	dirs=`ls -d */|cut -f1|grep 'harfbuzz-*'` && rm -Rf $dirs
	tar xfJ harfbuzz-$HARFBUZZ_VERSION.tar.xz
	cd harfbuzz-$HARFBUZZ_VERSION
		do_cmake -DCMAKE_CXX_FLAGS=-Wa,-mbig-obj
	cd ..
	touch $prefix/build-harfbuzz-$HARFBUZZ_VERSION
fi

########## FREETYPE2 ##########
if ! test -f $prefix/build-freetype2-$FREETYPE2_VERSION; then
	dirs=`ls -d */|cut -f1|grep 'freetype-*'` && rm -Rf $dirs
	tar xfz freetype-$FREETYPE2_VERSION.tar.gz
		cd freetype-$FREETYPE2_VERSION
		do_cmake -DZLIB_ROOT=/usr/$host/ -DFT_DISABLE_BZIP2=TRUE
	cd ..
	touch $prefix/build-freetype2-$FREETYPE2_VERSION
fi

########## LIBDISCID ##########
if ! test -f $prefix/build-libdiscid-$LIBDISCID_VERSION; then
	dirs=`ls -d */|cut -f1|grep 'libdiscid-*'` && rm -Rf $dirs
	unzip libdiscid-$LIBDISCID_VERSION-win.zip
	if test "$host" == "i686-w64-mingw32"; then
		cp -r libdiscid-$LIBDISCID_VERSION-win/Win32/* $prefix/lib
	else
		cp -r libdiscid-$LIBDISCID_VERSION-win/x64/* $prefix/lib
	fi
	cp -r libdiscid-$LIBDISCID_VERSION-win/include/discid $prefix/include
	cat <<EOF > $prefix/lib/pkgconfig/libdiscid.pc
prefix=
exec_prefix=
libdir=
includedir=

Name: libdiscid
Description: The MusicBrainz DiscID Library.
URL: http://musicbrainz.org/products/libdiscid/
Version: 0.6.5
Requires:
Libs: -ldiscid
Cflags:
EOF
	touch $prefix/build-libdiscid-$LIBDISCID_VERSION
fi

########## cJSON ##########
if ! test -f $prefix/build-cjson-$CJSON_VERSION; then
	dirs=`ls -d */|cut -f1|grep 'cJSON-*'` && rm -Rf $dirs
	tar xfz cJSON-$CJSON_VERSION.tar.gz
	cd cJSON-$CJSON_VERSION
		patch -p 1 <<EOF
diff -u cJSON-1.7.15-orig/cJSON.c cJSON-1.7.15/cJSON.c
--- cJSON-1.7.15-orig/cJSON.c	2021-08-25 13:15:09.000000000 +0200
+++ cJSON-1.7.15/cJSON.c	2023-04-08 15:46:56.341804545 +0200
@@ -69,21 +69,7 @@
 #endif
 #define false ((cJSON_bool)0)
 
-/* define isnan and isinf for ANSI C, if in C99 or above, isnan and isinf has been defined in math.h */
-#ifndef isinf
-#define isinf(d) (isnan((d - d)) && !isnan(d))
-#endif
-#ifndef isnan
-#define isnan(d) (d != d)
-#endif
-
-#ifndef NAN
-#ifdef _WIN32
-#define NAN sqrt(-1.0)
-#else
-#define NAN 0.0/0.0
-#endif
-#endif
+#include <math.h>
 
 typedef struct {
     const unsigned char *json;
diff -u cJSON-1.7.15-orig/CMakeLists.txt cJSON-1.7.15/CMakeLists.txt
--- cJSON-1.7.15-orig/CMakeLists.txt	2021-08-25 13:15:09.000000000 +0200
+++ cJSON-1.7.15/CMakeLists.txt	2023-04-08 15:47:22.540175535 +0200
@@ -20,11 +20,9 @@
 if (ENABLE_CUSTOM_COMPILER_FLAGS)
     if (("\${CMAKE_C_COMPILER_ID}" STREQUAL "Clang") OR ("\${CMAKE_C_COMPILER_ID}" STREQUAL "GNU"))
         list(APPEND custom_compiler_flags
-            -std=c89
             -pedantic
             -Wall
             -Wextra
-            -Werror
             -Wstrict-prototypes
             -Wwrite-strings
             -Wshadow
EOF
		patch -p1 <<EOF
diff -u cJSON-1.7.18/CMakeLists.txt cJSON-1.7.18-a29814f/CMakeLists.txt
--- cJSON-1.7.18/CMakeLists.txt 2026-08-04 09:23:39.918735622 +0200
+++ cJSON-1.7.18-a29814f/CMakeLists.txt 2026-08-04 09:30:10.797313010 +0200
@@ -1,5 +1,5 @@
 set(CMAKE_LEGACY_CYGWIN_WIN32 0)
-cmake_minimum_required(VERSION 3.0)
+cmake_minimum_required(VERSION 3.5)

 project(cJSON
     VERSION 1.7.18
EOF
		do_cmake
	cd ..
	touch $prefix/build-cjson-$CJSON_VERSION
fi

########## ancient ##########
if ! test -f $prefix/build-ancient-$ANCIENT_VERSION; then
	dirs=`ls -d */|cut -f1|grep 'ancient-*'` && rm -Rf $dirs
	tar xfz ancient-$ANCIENT_VERSION.tar.gz
	cd ancient-$ANCIENT_VERSION
		mkdir -p m4
		cp ../ax_cxx_compile_stdcxx.m4 m4/
		cp ../ax_cxx_compile_stdcxx.m4 m4/
		cp ../ax_check_compile_flag.m4 m4/
		cp ../ax_cflags_warn_all.m4    m4/
		cp ../ax_compiler_vendor.m4    m4/
		cp ../ax_prepend_flag.m4       m4/
		cp ../ax_require_defined.m4    m4/
		aclocal -I m4
		autoreconf --install
		./configure --host $host --prefix=$prefix
		make all install
	cd ..
	touch $prefix/build-ancient-$ANCIENT_VERSION
fi

########## libiconv ##########
if ! test -f $prefix/build-libiconv-$LIBICONV_VERSION; then
	dirs=`ls -d */|cut -f1|grep 'libiconv-*'` && rm -Rf $dirs
	tar xfz libiconv-$LIBICONV_VERSION.tar.gz
	cd libiconv-$LIBICONV_VERSION
		./configure --host $host --prefix=$prefix
		make all install
	cd ..
	touch $prefix/build-libiconv-$LIBICONV_VERSION
fi

########## game-music-emulator ##########
if ! test -f $prefix/build-game-music-emu-$GAMEMUSICEMU_VERSION; then
	dirs=`ls -d */|cut -f1|grep 'game-music-emu-*'` && rm -Rf $dirs
	tar xfz game-music-emu-$GAMEMUSICEMU_VERSION.tar.gz
	cd game-music-emu-$GAMEMUSICEMU_VERSION
		do_cmake -DENABLE_UBSAN=off
	cd ..
	touch $prefix/build-game-music-emu-$GAMEMUSICEMU_VERSION
fi

########## WavPack ##########
if ! test -f $prefix/build-wavpack-$WAVPACK_VERSION; then
	dirs=`ls -d */|cut -f1|grep 'wavpack-*'` && rm -Rf $dirs
	tar xfJ wavpack-$WAVPACK_VERSION.tar.xz
	cd wavpack-$WAVPACK_VERSION
		./configure --host $host --prefix=$prefix --enable-legacy
		make all install
	cd ..
	touch $prefix/build-wavpack-$WAVPACK_VERSION
fi

######### compile and install ##########

cd ..
./configure \
  --host $host \
  CFLAGS="-I$prefix/include/ -O2" \
  CXXFLAGS="-I$prefix/include/ -O2" \
  LDFLAGS=-L$prefix/lib/ \
  PKG_CONFIG_PATH=$prefix/lib/pkgconfig/ \
  --prefix="$install" \
  --libdir="$install" \
  --docdir="$install" \
  --bindir="$install" \
  --sysconfdir="$install" \
  --datadir="$install" \
  --datarootdir="$install" \
  --mandir="$install" \
  --without-info \
  --without-update-mime-database \
  --without-desktop_file_install \
  --without-update-desktop-database \
  --with-unifont-relative
make all
rm -Rf $install
make install
cp $prefix/lib/bz2.dll              \
   $prefix/lib/discid.dll           \
   $prefix/bin/libancient-2.dll     \
   $prefix/bin/libcharset-1.dll     \
   $prefix/bin/libcjson.dll         \
   $prefix/bin/libFLAC.dll          \
   $prefix/bin/libfreetype.dll      \
   $prefix/bin/libgme.dll           \
   $prefix/bin/libharfbuzz.dll      \
   $prefix/bin/libiconv-2.dll       \
   $prefix/bin/libjpeg-62.dll       \
   $prefix/bin/libmad-0.dll         \
   $prefix/bin/libopus-0.dll        \
   $prefix/bin/libpng16.dll         \
   $prefix/bin/libspeex-1.dll       \
   $prefix/bin/libturbojpeg.dll     \
   $prefix/bin/ogg.dll              \
   $prefix/bin/SDL3.dll             \
   $prefix/bin/vorbis.dll           \
   $prefix/bin/vorbisfile.dll       \
   $prefix/bin/libwavpack-1.dll     \
   /usr/lib/gcc/$host/$gccversion/libssp-0.dll       \
   /usr/lib/gcc/$host/$gccversion/libstdc++-6.dll    \
   /usr/$host/lib/libwinpthread-1.dll             \
   /usr/$host/lib/zlib1.dll                       \
   $install
if test "$host" == "i686-w64-mingw32"; then
  cp $prefix/bin/libbrotlicommon.dll  \
     $prefix/bin/libbrotlidec.dll     \
     /usr/lib/gcc/$host/$gccversion/libgcc_s_dw2-1.dll \
   $install
else
  cp /usr/lib/gcc/$host/$gccversion/libgcc_s_seh-1.dll \
     $install
fi
mkdir -p $install/data
cp $host-src/unifont-$UNIFONT_VERSION.otf       $install/data/unifont.otf
cp $host-src/unifont_csur-$UNIFONT_VERSION.otf  $install/data/unifont_csur.otf
cp $host-src/unifont_upper-$UNIFONT_VERSION.otf $install/data/unifont_upper.otf
$host-strip $install/*.dll $install/*.exe $install/autoload/*.dll
wget https://stian.cubic.org/mirror/ftp.cubic.org/pub/player/gfx/opencp25image1.zip -O $install/data/opencp25image1.zip
wget https://stian.cubic.org/mirror/ftp.cubic.org/pub/player/gfx/opencp25ani1.zip -O $install/data/opencp25ani1.zip
