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

_realname=lame
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=3.100
pkgrel=3
pkgdesc="A high quality MPEG Audio Layer III (MP3) encoder (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://lame.sourceforge.io/"
msys2_references=(
  "cpe: cpe:/a:lame_project:lame"
)
license=("LGPL")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-mpg123"
             "${MINGW_PACKAGE_PREFIX}-ncurses"
             "${MINGW_PACKAGE_PREFIX}-autotools"
              $( [[ "${CARCH}" != "i686" \
                && "${CARCH}" != "x86_64" ]] \
                || echo "${MINGW_PACKAGE_PREFIX}-nasm" ))
depends=("${MINGW_PACKAGE_PREFIX}-libiconv")
source=("https://downloads.sourceforge.net/${_realname}/${_realname}-${pkgver}.tar.gz"
        0002-07-field-width-fix.all.patch
        0005-no-gtk.all.patch
        0006-dont-use-outdated-symbol-list.patch
        0007-revert-posix-code.patch
        0008-skip-termcap.patch
        0009-lame-add-pkgconfig.patch)
sha256sums=('ddfe36cab873794038ae2c1210557ad34857a4b6bdc515785d1da9e175b1da1e'
            'd3ba595816761706d1e5dcf432f0b3ad8c9c6b37584032e21bf24c89e4fd0fc8'
            'cd3c8807de5b435b8e97200e369e7e8f93f7fc0eba7d44b393ede925e42d3b00'
            '21c7ee85854be00a3e8e6790911292837676f083a5c1085718bed899096d3679'
            '1e7f61456b15ceea613de8fd982b3f892340b851393d4ca4aff44395ae237ae9'
            '8fddeebd0ccdec62ede2932ce3bbe767af6ed6f06018f7f13113401d4ce06179'
            '65edcb5090417f822227b4a43f2f0c0224ceca7486fb3fd01aa9a6cf153ad714')

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

  patch -p1 -i "${srcdir}"/0002-07-field-width-fix.all.patch
  patch -p1 -i "${srcdir}"/0005-no-gtk.all.patch
  patch -p1 -i "${srcdir}"/0006-dont-use-outdated-symbol-list.patch
  patch -p1 -i "${srcdir}"/0007-revert-posix-code.patch
  # tgetent() crashes under mingw64, not sure why
  patch -p1 -i "${srcdir}"/0008-skip-termcap.patch

  # https://salsa.debian.org/multimedia-team/lame
  # https://sourceforge.net/p/lame/mailman/message/37616009/
  patch -p1 -i "${srcdir}"/0009-lame-add-pkgconfig.patch

  autoreconf -fi
}

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

  ../${_realname}-${pkgver}/configure \
    --prefix=${MINGW_PREFIX} \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST} \
    --enable-shared \
    --enable-static \
    $( [[ "${CARCH}" != "i686" \
      && "${CARCH}" != "x86_64" ]] \
      || echo "--enable-nasm")
  make
}

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