# Maintainer: Biswapriyo Nath <nathbappai@gmail.com>

_realname=handbrake
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=1.9.2
pkgrel=8
pkgdesc="The open source video transcoder (mingw-w64)"
arch=('any')
mingw_arch=('ucrt64' 'clang64' 'clangarm64')
url='https://handbrake.fr/'
msys2_repository_url='https://github.com/HandBrake/HandBrake/'
license=('GPL-2.0-or-later')
depends=(
  "${MINGW_PACKAGE_PREFIX}-ffmpeg7.1"
  "${MINGW_PACKAGE_PREFIX}-jansson"
  "${MINGW_PACKAGE_PREFIX}-libass"
  "${MINGW_PACKAGE_PREFIX}-libbluray"
  "${MINGW_PACKAGE_PREFIX}-libdvdnav"
  "${MINGW_PACKAGE_PREFIX}-libdvdread"
  "${MINGW_PACKAGE_PREFIX}-libiconv"
  "${MINGW_PACKAGE_PREFIX}-libjpeg-turbo"
  "${MINGW_PACKAGE_PREFIX}-libsystre"
  "${MINGW_PACKAGE_PREFIX}-libtheora"
  "${MINGW_PACKAGE_PREFIX}-libvorbis"
  "${MINGW_PACKAGE_PREFIX}-libx264"
  "${MINGW_PACKAGE_PREFIX}-svt-av1"
  "${MINGW_PACKAGE_PREFIX}-x265"
)
makedepends=(
  "${MINGW_PACKAGE_PREFIX}-autotools"
  "${MINGW_PACKAGE_PREFIX}-cc"
  "${MINGW_PACKAGE_PREFIX}-pkgconf"
  "${MINGW_PACKAGE_PREFIX}-python"
)
source=(
  "https://github.com/HandBrake/HandBrake/releases/download/${pkgver}/HandBrake-${pkgver}-source.tar.bz2"{,.sig}
  "https://github.com/HandBrake/HandBrake/commit/6f2cd466ef0e029d9e5a51ac8640c3ab64e212f6.patch"
  "https://github.com/HandBrake/HandBrake/commit/09e99ce641c840686b1b5f860263e89ad7d6651d.patch"
  "0001-handbrake-workaround-build-system.patch"
  "0003-Remove-ambient-viewing-support.patch"
  "0004-fix-svt-av1-3-build.patch"
  "0005-arch-detection.patch"
)
sha256sums=('f56696b9863a6c926c0eabdcb980cece9aa222c650278d455ac6873d3220ce49'
            'SKIP'
            '010910a0c287d7a93ba9d13c52e1c671a64b65915f6b232c37b695e44ef02eb5'
            'f1da71bb593a5ad18893c60a3e07d059d34245351fb7957472b2e749fc6f939d'
            '3a240bc164143706c025dd8809f969352e0dbd0226611ee9a97c05eb772a9d75'
            '5245ef3310df3326a28f0da40e78d8bfd6cd83f790600e3557acc99b1de6ac32'
            'f420fb4de585ccb461eb0fdae3572c5c3d8b80a8a3b1dde822aa81e9754a6189'
            '3f3331d5d4ef822ae4cefa00c528691a46207a3df1365da9f1441c12bde524d8')
validpgpkeys=('1629C061B3DDE7EB4AE34B81021DB8B44E4A8645') # HandBrake Team <developers@handbrake.fr>

_apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -p1 -i "${srcdir}/${_patch}"
  done
}

prepare() {
  cd "HandBrake-${pkgver}"

  # Remove reference to bundled dependencies
  rm -rf contrib/
  sed -i -E '/(\/contrib|contrib\/)/d' make/include/main.defs

  # error: 'AV_FRAME_DATA_DOVI_RPU_BUFFER_T35' undeclared (needs ffmpeg > 7.1)
  patch -R -p1 -i "${srcdir}/6f2cd466ef0e029d9e5a51ac8640c3ab64e212f6.patch"
  patch -R -p1 -i "${srcdir}/09e99ce641c840686b1b5f860263e89ad7d6651d.patch"

  # update SVT-AV1 to 3.0.0
  # https://github.com/HandBrake/HandBrake/commit/75f9c84c140c8841cfe1324ef59452025899ad8b
  patch -p1 -i "${srcdir}/0004-fix-svt-av1-3-build.patch"

  # Put all the magic here
  _apply_patch_with_msg 0001-handbrake-workaround-build-system.patch

  # https://salsa.debian.org/multimedia-team/handbrake/-/blob/master/debian/patches/0003-Remove-ambient-viewing-support.patch?ref_type=heads
  _apply_patch_with_msg 0003-Remove-ambient-viewing-support.patch

  _apply_patch_with_msg 0005-arch-detection.patch
}

build() {
  cd "HandBrake-${pkgver}"

  # Build against ffmpeg 7.1
  export PKG_CONFIG_PATH="${MINGW_PREFIX}/lib/ffmpeg7.1/pkgconfig:${PKG_CONFIG_PATH}"
  CFLAGS+=" $(pkgconf --cflags libavcodec)"
  LDFLAGS+=" $(pkgconf --libs-only-L libavcodec)"

  ./configure

  make -C build

  # meson setup -Dhb-dir=$PWD/build build/gtk gtk
}

package() {
  cd "HandBrake-${pkgver}/build"
  install -D HandBrakeCLI.exe "${pkgdir}${MINGW_PREFIX}/bin/HandBrakeCLI.exe"
  install -Dm644 "${srcdir}/${_realname}-${pkgver}/LICENSE" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}
