# Maintainer in the AUR: Roboron <robertoms258 at gmail dot com>
 
_realname=fluidsynth
pkgname=mingw-w64-fluidsynth
pkgver=2.5.6
pkgrel=1
pkgdesc="A real-time software synthesizer based on the SoundFont 2 specifications (mingw-w64)"
url="https://www.fluidsynth.org/"
_url="https://github.com/fluidsynth/fluidsynth"
license=(LGPL-2.1-or-later)
arch=('any')
depends=('mingw-w64-sdl2' 'mingw-w64-glib2' 'mingw-w64-readline')
[[ $pkgname =~ .*-clang-.* ]] || depends+=('mingw-w64-libsndfile' 'mingw-w64-portaudio')
makedepends=('mingw-w64-cmake-static' 'mingw-w64-pkg-config' 'ninja')
options=('staticlibs' '!buildflags' '!strip')
source=(
  $_realname-$pkgver.tar.gz::$_url/archive/v$pkgver.tar.gz
)
sha512sums=('0470b190da7e3f330e4fbdf28035ff172866213e0d13f968965b82022ec5cf5c13935624fa0067bb7d9a92cfab1d8238a1ebecbbe91164b298d36a02e50464bc')
b2sums=('c87eef6142932f8f18c0bc281a6242732ea2e7d9883ec9ad9b92c7e39c8f2afcb25b5a8a17a20b3033d79e6b5aebefc06a607f7eab77dd9e5cd44d4d9175d02b')

_architectures="i686-w64-mingw32 x86_64-w64-mingw32"

build() {
  cd fluidsynth-$pkgver
  cflags_base=$CFLAGS
  for _arch in ${_architectures}; do
    mkdir -p build-${_arch} && pushd build-${_arch}
    
    [[ $pkgname =~ .*-clang-.* ]] & additional_deps=0 || additional_deps=1

    export CFLAGS="$cflags_base"
    export CXXFLAGS=$CFLAGS
    ${_arch}-cmake -G Ninja -S .. -B shared -DBUILD_SHARED_LIBS=1 \
      -Denable-libsndfile=$additional_deps -Denable-portaudio=$additional_deps  -Denable-dbus=0
    sed -i -e "s|/usr/$_arch/lib/libSDL2main.a||g" shared/build.ninja
    VERBOSE=1 cmake --build shared

    export CFLAGS="$cflags_base -DGLIB_STATIC_COMPILATION=1 -DGMODULE_STATIC_COMPILATION=1"
    export CXXFLAGS=$CFLAGS
    ${_arch}-cmake-static -G Ninja -S .. -B static -DCMAKE_INSTALL_PREFIX:PATH="/usr/$_arch/static" -DBUILD_SHARED_LIBS=0 \
      -Denable-aufile=0 -Denable-dbus=0 -Denable-ipv6=0 -Denable-jack=0 -Denable-ladspa=0 -Denable-midishare=0 \
      -Denable-opensles=0 -Denable-oboe=0 -Denable-oss=0 -Denable-readline=0 -Denable-winmidi=1 -Denable-waveout=0 \
      -Denable-network=0 -Denable-pulseaudio=0 -Denable-dsound=0 -Denable-alsa=0 -Denable-libinstpatch=0 \
      -Denable-portaudio=0 -Denable-wasapi=0 -Denable-openmp=1 -Denable-libsndfile=0 -Denable-sdl2=1
    sed -i -e "s|/usr/$_arch/lib/libSDL2main.a||g" static/build.ninja
    VERBOSE=1 cmake --build static
    popd
  done
}

package() {
  for _arch in ${_architectures}; do
    cd "$srcdir"/fluidsynth-${pkgver}/build-${_arch}
    DESTDIR="$pkgdir" cmake --install shared
    DESTDIR="$pkgdir" cmake --install static
  done
}
