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

_realname=id3lib
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=3.8.3
pkgrel=2
pkgdesc='Library for reading, writing, and manipulating ID3v1 and ID3v2 tags (mingw-w64)'
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
license=('LGPL')
url="https://id3lib.sourceforge.io/"
options=('staticlibs')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-zlib" )
source=(https://downloads.sourceforge.net/sourceforge/${_realname}/${_realname}-${pkgver}.tar.gz
        00-add-libz-to-compilation.patch
        05-create-m4-directory.patch
        10-fix-compilation-with-cpp-headers.patch
        15-fix-headers-of-main-functions.patch
        20-create-manpages.patch
        30-fix-utf16.patch
        40-deal-with-mkstemp.patch
        50-remove-outdated-check.patch
        60-add-c-wrapper-functions.patch
        60-fix_make_check.patch
        60-id3lib-missing-nullpointer-check.patch
        60-spelling.patch
        61-fix_vbr_stack_smash.patch
        70-id3lib-3.8.3-autoreconf.patch
        100-id3lib-3.8.3-mingw.patch)

sha256sums=('2749cc3c0cd7280b299518b1ddf5a5bcfe2d1100614519b68702230e26c7d079'
            'e884395cf0bd7b44a550fc8785d79c0992d749be1edc45b00d289cbbd179c6d5'
            'c41ec6889b775119f71c25fa09bb5110bcf550a14e6aac80597479df453de5fb'
            '5fb24595095596ff55d876fb3e5277000f04c4f7a3e07aff5c05e11639dc2165'
            '042deb7c2df1afa0851622b6086e8eb4b007cc6cfcd70210852d5c25e39b08b1'
            '97442e14a5147df36f35d6a396e2b3fb6e155060ec672d91a68bd1a742234b4e'
            'a7cce4176df773677bfc6a527a0465d460fe60a269402af9bdfc9dd6104ea6a4'
            'd7999cdb425ba8013dd11da7fb570e875d368e5b1236c2b094abee8446f04218'
            'f908af7715fb164081d191c766b040cefa0a94f1414e98f60bc38b3e0b1aa309'
            '1b06c5d2c36e6a1f2236c4603a934d8cd675414dcfd52b997cb584404c0bc77d'
            '88aded7423f266d2fedb0eef9dbb272b53cc776ae98bd71dd5afb30b6ac48515'
            '85f20c4d3467f48df1bcaa4150bae55e95270cabb542589f167a914b2f553d04'
            'b59be8d58a0f7d84220cb7cecce907f1db5eaf3dcc45cf70c1451ab7c9ab9ceb'
            '5a24d460c9dd1e643ac57b817ea9cec87f44c1d49381065f83879b2eea36adf7'
            'b06cecc541ea20e2bb5495da31c668cf41fb055fa045b1de4eade0294ba70e73'
            'f6b3c120eb4b34fd45f67dce6130747b76ae0d945d37dc14b7c01f930e772237')

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

  patch -p1 -i ../00-add-libz-to-compilation.patch
  patch -p1 -i ../05-create-m4-directory.patch
  patch -p1 -i ../10-fix-compilation-with-cpp-headers.patch
  patch -p1 -i ../15-fix-headers-of-main-functions.patch
  patch -p1 -i ../20-create-manpages.patch
  patch -p1 -i ../30-fix-utf16.patch
  patch -p1 -i ../40-deal-with-mkstemp.patch
  patch -p1 -i ../60-add-c-wrapper-functions.patch
  patch -p1 -i ../60-fix_make_check.patch
  patch -p1 -i ../60-id3lib-missing-nullpointer-check.patch
  patch -p1 -i ../60-spelling.patch
  patch -p1 -i ../61-fix_vbr_stack_smash.patch
  patch -p1 -i ../50-remove-outdated-check.patch
  patch -p1 -i ../70-id3lib-3.8.3-autoreconf.patch
  patch -p1 -i ../100-id3lib-3.8.3-mingw.patch

  libtoolize -fc
  WANT_AUTOMAKE=latest aclocal
  WANT_AUTOMAKE=latest autoconf
  WANT_AUTOMAKE=latest automake --add-missing --copy
}

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

  ../${_realname}-${pkgver}/configure \
    --prefix=${MINGW_PREFIX} \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --enable-shared \
    --enable-static

  LC_ALL=C make
}

check() {
  cd "${srcdir}/${_realname}-${pkgver}"
  make check
}

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

  install -d "${pkgdir}${MINGW_PREFIX}"/share/man/man1
  cp ${srcdir}/${_realname}-${pkgver}/doc/man/* "${pkgdir}${MINGW_PREFIX}"/share/man/man1
}
