# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=fluidsynth
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=2.5.2
pkgrel=1
pkgdesc="A real-time software synthesizer based on the SoundFont 2 specifications (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://www.fluidsynth.org/"
msys2_repository_url='https://github.com/FluidSynth/fluidsynth'
msys2_references=(
  "cpe: cpe:/a:fluidsynth:fluidsynth"
)
license=('spdx:LGPL-2.1-or-later')
depends=("${MINGW_PACKAGE_PREFIX}-cc-libs"
         "${MINGW_PACKAGE_PREFIX}-glib2"
         "${MINGW_PACKAGE_PREFIX}-libsndfile"
         "${MINGW_PACKAGE_PREFIX}-omp"
         "${MINGW_PACKAGE_PREFIX}-portaudio"
         "${MINGW_PACKAGE_PREFIX}-readline"
          $([[ "${CARCH}" == "i686" ]] || echo "${MINGW_PACKAGE_PREFIX}-sdl3"))
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ladspa-sdk"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-pkgconf")
source=("https://github.com/FluidSynth/${_realname}/archive/v${pkgver}/${_realname}-${pkgver}.tar.gz"
        "010-no-undefined.patch")
sha256sums=('1dcb13308b3aa383db658c60a7b63d73d0ff4601ccee589582ba7a816475410e'
            'e359e935d34b62c627c56b5109a336e14f208f06862141c6237af3b533b079d1')
noextract=("${_realname}-${pkgver}.tar.gz")

prepare() {
    tar -xf "${_realname}-${pkgver}.tar.gz" || true

    cd "${_realname}-${pkgver}"
    patch -p1 -i "${srcdir}"/010-no-undefined.patch
}

build() {
  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  local -a _static_flags=(
    -DGIO_STATIC_COMPILATION
    -DGLIB_STATIC_COMPILATION
    -DGMODULE_STATIC_COMPILATION
    -DGOBJECT_STATIC_COMPILATION
  )

  mkdir -p "${srcdir}"/build-${MSYSTEM}-static && cd "${srcdir}"/build-${MSYSTEM}-static

  CFLAGS+=" ${_static_flags[@]}" \
  CXXFLAGS+=" ${_static_flags[@]}" \
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    cmake \
      -G'Ninja' \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      "${extra_config[@]}" \
      -DBUILD_SHARED_LIBS=OFF \
      -Denable-portaudio=ON \
      -Denable-dbus=OFF \
      ../${_realname}-${pkgver}

  cmake --build ./

  mkdir -p "${srcdir}"/build-${MSYSTEM}-shared && cd "${srcdir}"/build-${MSYSTEM}-shared

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    cmake \
      -G'Ninja' \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      "${extra_config[@]}" \
      -DBUILD_SHARED_LIBS=ON \
      -Denable-portaudio=ON \
      -Denable-dbus=OFF \
      ../${_realname}-${pkgver}

  cmake --build ./
}

package() {
  cd "${srcdir}"/build-${MSYSTEM}-static
  DESTDIR="${pkgdir}" cmake --install ./

  cd "${srcdir}"/build-${MSYSTEM}-shared
  DESTDIR="${pkgdir}" cmake --install ./

  local MINGW_PREFIX_WIN=$(cygpath -am ${MINGW_PREFIX})
  find "${pkgdir}${MINGW_PREFIX}/lib/pkgconfig" -type f -name '*.pc' \
    -exec sed -i -e "s|${MINGW_PREFIX_WIN}|${MINGW_PREFIX}|g" \
    -e "s|${MINGW_PREFIX}/include|\${prefix}/include|g" \
    -e "s|${MINGW_PREFIX}/lib|\${prefix}/lib|g" {} \;

  install -vDm644 "${srcdir}"/${_realname}-${pkgver}/LICENSE \
    "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE
}
