# Maintainer (Arch): Evangelos Foutras <evangelos@foutrelis.com>
# Contributor (Arch): Giovanni Scafora <giovanni@archlinux.org>
# Contributor (Arch): Sebastien Piccand <sebcactus gmail com>
# Maintainer: Omar Vega Ramos <ovruni@gnu.org.pe>
# Contributor: Denis 'GNUtoo' Carikli <GNUtoo@cyberdimension.org>

pkgname=('handbrake' 'handbrake-cli')
pkgver=1.3.3
pkgrel=5
pkgrel+=.par2
arch=('x86_64')
arch+=('i686' 'armv7h')
url="https://handbrake.fr/"
license=('GPL')
_commondeps=('libxml2' 'libass' 'libvorbis' 'opus' 'speex' 'libtheora' 'lame'
             'x264' 'jansson' 'libvpx' 'libva' 'numactl' 'bzip2' 'gcc-libs'
             'zlib' 'xz')
_commondeps+=('dav1d' 'libbluray' 'numactl' 'meson' 'x265')
_commondeps+=('ffmpeg' 'libdvdnav')
_guideps=('gst-plugins-base' 'gtk3' 'librsvg' 'libgudev')
makedepends=('intltool' 'python' 'nasm' 'wget' 'cmake' 'meson'
             "${_commondeps[@]}" "${_guideps[@]}")
source=(https://github.com/HandBrake/HandBrake/releases/download/$pkgver/HandBrake-$pkgver-source.tar.bz2{,.sig}
        handbrake-fix-close-button-in-about-dialog.patch::https://github.com/HandBrake/HandBrake/commit/d3eb4a11ac.patch)
source+=(0001-Fix-linking-issue-when-disabling-internal-libbluray.patch
         0002-Fix-linking-issue-when-disabling-internal-x265.patch)
sha256sums=('218a37d95f48b5e7cf285363d3ab16c314d97627a7a710cab3758902ae877f85'
            'SKIP'
            '628451adc1341abf0917da86dcae7922c42b6e61225ef4b55182e8dbe74cfa49')
sha256sums+=('8a2818c31815764227d52ea99f7291acacec16c62487f5ae7ea64fedd52546fd'
             'a1181cba4d83a20ead3e1d92649f8f25add000ebc3293829b9604cfc6ed79326')
validpgpkeys+=('1629C061B3DDE7EB4AE34B81021DB8B44E4A8645') # HandBrake Team <developers@handbrake.fr>

prepare() {
  cd "$srcdir/HandBrake-$pkgver"
  patch -Np1 -i ../handbrake-fix-close-button-in-about-dialog.patch

  # Prevent fetching and use system libraries instead
  _libs="\
    ffmpeg \
    libbluray \
    libdav1d \
    libdvdnav \
    libdvdread \
    libvpx \
    nvenc \
    x264 \
    x265 \
    x265_8bit \
    x265_10bit \
    x265_12bit \
  "

  patch -Np1 -i ../0001-Fix-linking-issue-when-disabling-internal-libbluray.patch
  patch -Np1 -i ../0002-Fix-linking-issue-when-disabling-internal-x265.patch

  for _lib in $_libs ; do
    sed -i "/MODULES += contrib\/$_lib/d" make/include/main.defs
    rm -rf contrib/$_lib
  done
}

build() {
  cd "$srcdir/HandBrake-$pkgver"

  ./configure \
    --prefix=/usr \
    --disable-df-fetch \
    --disable-gtk-update-checks \
    --disable-nvenc
  make -C build
}

package_handbrake() {
  pkgdesc="Multithreaded video transcoder"
  depends=('desktop-file-utils' 'hicolor-icon-theme'
           "${_commondeps[@]}" "${_guideps[@]}")
  optdepends=('gst-plugins-good: for video previews'
              'gst-libav: for video previews'
              'libdvdcss: for decoding encrypted DVDs')

  cd "$srcdir/HandBrake-$pkgver/build"

  make DESTDIR="$pkgdir" install
  rm "$pkgdir/usr/bin/HandBrakeCLI"
}

package_handbrake-cli() {
  pkgdesc="Multithreaded video transcoder (CLI)"
  depends=("${_commondeps[@]}")
  optdepends=('libdvdcss: for decoding encrypted DVDs')

  cd "$srcdir/HandBrake-$pkgver/build"
  install -D HandBrakeCLI "$pkgdir/usr/bin/HandBrakeCLI"
}

# vim:set ts=2 sw=2 et:
