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

_realname=handbrake
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=1.10.2
pkgrel=1
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}-ffmpeg"
  "${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}
  "0001-handbrake-workaround-build-system.patch"
  "0002-fix-build-with-external-ffmpeg.patch"
  "0003-Remove-ambient-viewing-support.patch"
  "0004-fix-build-with-ffmpeg-8.patch"
  "0005-arch-detection.patch"
)
sha256sums=('c65e1cc4f8cfc36c24107b92c28d60e71ef185ec983e9a5841facffafea5f8db'
            'SKIP'
            '3a240bc164143706c025dd8809f969352e0dbd0226611ee9a97c05eb772a9d75'
            'f0ed0f8f42c74e098db8c8f361411bc712dc594e2464fa0476d554d3153dfa3b'
            '5245ef3310df3326a28f0da40e78d8bfd6cd83f790600e3557acc99b1de6ac32'
            '4a12334c0cf6e4655fd264e2ae0e9607b7deaf65c8b757766ab15c248738b83b'
            '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


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

  # error: 'AV_FRAME_DATA_DOVI_RPU_BUFFER_T35' undeclared (needs bundled ffmpeg)
  _apply_patch_with_msg 0002-fix-build-with-external-ffmpeg.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

  # https://github.com/HandBrake/HandBrake/commit/c7867ba4
  _apply_patch_with_msg 0004-fix-build-with-ffmpeg-8.patch

  _apply_patch_with_msg 0005-arch-detection.patch
}

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

  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
}
