# Maintainer: David Macek <david.macek.0@gmail.com>

_realname=portmidi
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=217
pkgrel=2
pkgdesc="Platform independent library for real-time MIDI input/output (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
url="https://portmedia.sourceforge.io/"
license=("custom")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-pkg-config")
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs")
source=("https://downloads.sourceforge.net/portmedia/portmidi/${pkgver}/portmidi-src-${pkgver}.zip"
        enable-install-on-windows.patch)
sha256sums=('08e9a892bd80bdb1115213fb72dc29a7bf2ff108b378180586aa65f3cfd42e0f'
            'fabd4660901f16d4c97ab1a8d1346b946e9dc06b3158cb237dbf14264895c53e')

prepare() {
  cd portmidi
  patch -p1 -i "${srcdir}/enable-install-on-windows.patch"
}

build() {
  # we don't have Java yet, so we fake it

  [[ -d "build-${MINGW_CHOST}" ]] && rm -rf "build-${MINGW_CHOST}"
  mkdir -p "build-${MINGW_CHOST}"
  cd "build-${MINGW_CHOST}"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -Wno-dev \
    -G "MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DCMAKE_BUILD_TYPE=Release \
    -DJAVA_INCLUDE_PATH= \
    -DJAVA_INCLUDE_PATH2= \
    -DJAVA_JVM_LIBRARY= \
    ../portmidi

  make -t pmjni
  make
}

package() {
  cd "build-${MINGW_CHOST}"
  make DESTDIR=${pkgdir} install

  # install executables
  for exe in latency.exe midiclock.exe midithread.exe midithru.exe mm.exe qtest.exe sysex.exe test.exe; do
    install -Dm0755 "pm_test/${exe}" "${pkgdir}${MINGW_PREFIX}/bin/pm-${exe}"
  done

  # install license
  install -Dm0755 "${srcdir}/portmidi/license.txt" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/license.txt"
}
