
_realname=portsmf
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=0.1.r264.9e44200
pkgrel=1
pkgdesc="Portable library for reading/writing Standard MIDI Files (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://github.com/rbdannenberg/portsmf"
license=("spdx:MIT")
makedepends=(
  "${MINGW_PACKAGE_PREFIX}-autotools"
  "${MINGW_PACKAGE_PREFIX}-cc"
  "git"
)
_commit="9e44200027078b876064cf9556086d1a538aff61"
depends=("${MINGW_PACKAGE_PREFIX}-cc-libs")
source=("${_realname}::git+https://github.com/rbdannenberg/portsmf#commit=$_commit"
         001-portsmf-shared.patch
         002-ptr-arithm-64bits.patch)
sha256sums=('SKIP'
            '0c16d53892daf7d9ae8c78aa9339b9cf59ee4977e2b9914c11c7bc0dcd15afd2'
            '1efd5624143770b5ae8beee80e9ac8dc39c2d1bf3f2398308ba7ffdf2a6b15fa')

pkgver() {
  cd "${srcdir}/${_realname}"

  printf "0.1.r%s.%s" "$(git rev-list --count $_commit)" "$(git rev-parse --short $_commit)"
}

prepare() {
  cd "${srcdir}/${_realname}"

  patch -Np1 -i "${srcdir}/001-portsmf-shared.patch"
  patch -Np1 -i "${srcdir}/002-ptr-arithm-64bits.patch"

  autoreconf -vfi
}

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

  ../${_realname}/configure \
      --prefix=${MINGW_PREFIX} \
      --libdir=${MINGW_PREFIX}/lib \
      --includedir=${MINGW_PREFIX}/include/portsmf

  make
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"

  make DESTDIR="${pkgdir}" includedir=${MINGW_PREFIX}/include/portsmf install

  install -Dm644 "${srcdir}/build-${MSYSTEM}"/portSMF.pc -t "${pkgdir}${MINGW_PREFIX}/lib/pkgconfig/"
  install -Dm644 "${srcdir}/${_realname}"/license.txt "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
  install -Dm644 "${srcdir}/${_realname}"/README.md "${pkgdir}${MINGW_PREFIX}/share/doc/${_realname}/README"

  # Make install adds the license to the doc folder
  cd "${pkgdir}${MINGW_PREFIX}/share/doc/${_realname}"
  rm *.txt *.md
}
