SUMMARY="A tool to convert video from nearly any format to modern codecs"
DESCRIPTION="HandBrake is an open-source, GPL-licensed, multiplatform, \
multithreaded video transcoder.
Convert from many common multimedia file formats, including unprotected \
DVD or BluRay sources to a handful of modern output file formats."
HOMEPAGE="https://handbrake.fr/"
LICENSE="GNU GPL v2"
COPYRIGHT="2003-2019 HandBrake Team"
REVISION="8"
SOURCE_URI="https://download.handbrake.fr/releases/$portVersion/HandBrake-$portVersion-source.tar.bz2"
CHECKSUM_SHA256="df6816f517d60ae8a6626aa731821af2d1966c155fa53b2b9a06c47f3c565e4c"
SOURCE_DIR="HandBrake-$portVersion"
PATCHES="handbrake-$portVersion.patchset"
ADDITIONAL_FILES="HandBrake.rdef"

ARCHITECTURES="all !x86_gcc2"
SECONDARY_ARCHITECTURES="x86"

PROVIDES="
	handbrake$secondaryArchSuffix = $portVersion
	cmd:HandBrakeCLI
	"
REQUIRES="
	haiku$secondaryArchSuffix
	lib:libass$secondaryArchSuffix
	lib:libavcodec$secondaryArchSuffix
	lib:libbluray$secondaryArchSuffix
	lib:libbz2$secondaryArchSuffix
	lib:libdvdnav$secondaryArchSuffix
	lib:libdvdread$secondaryArchSuffix
	lib:libogg$secondaryArchSuffix
	lib:libfontconfig$secondaryArchSuffix
	lib:libfreetype$secondaryArchSuffix
	lib:libfribidi$secondaryArchSuffix
	lib:libharfbuzz$secondaryArchSuffix
	lib:libiconv$secondaryArchSuffix
	lib:libjansson$secondaryArchSuffix
	lib:liblzma$secondaryArchSuffix
	lib:libmkv$secondaryArchSuffix
	lib:libmp3lame$secondaryArchSuffix
	lib:libmp4v2$secondaryArchSuffix
	lib:libmpeg2$secondaryArchSuffix
	lib:libopus$secondaryArchSuffix
	lib:libsamplerate$secondaryArchSuffix
	lib:libspeex$secondaryArchSuffix
	lib:libtheora$secondaryArchSuffix
	lib:libvorbis$secondaryArchSuffix
	lib:libvpx$secondaryArchSuffix
	lib:libx264$secondaryArchSuffix
	lib:libxml2$secondaryArchSuffix
	lib:libz$secondaryArchSuffix
	"

BUILD_REQUIRES="
	haiku${secondaryArchSuffix}_devel
	devel:libass$secondaryArchSuffix
	devel:libavcodec$secondaryArchSuffix
	devel:libbluray$secondaryArchSuffix >= 2
	devel:libbz2$secondaryArchSuffix
	devel:libdvdnav$secondaryArchSuffix
	devel:libdvdread$secondaryArchSuffix
	devel:libfreetype$secondaryArchSuffix
	devel:libiconv$secondaryArchSuffix
	devel:libjansson$secondaryArchSuffix
	devel:liblzma$secondaryArchSuffix
	devel:libmkv$secondaryArchSuffix
	devel:libmp3lame$secondaryArchSuffix
	devel:libmp4v2$secondaryArchSuffix
	devel:libmpeg2$secondaryArchSuffix
	devel:libogg$secondaryArchSuffix
	devel:libopus$secondaryArchSuffix
	devel:libsamplerate$secondaryArchSuffix
	devel:libtheora$secondaryArchSuffix
	devel:libvorbis$secondaryArchSuffix
	devel:libvpx$secondaryArchSuffix
	devel:libx264$secondaryArchSuffix
	devel:libxml2$secondaryArchSuffix
	devel:libz$secondaryArchSuffix
	"
BUILD_PREREQUIRES="
	cmd:autoconf
	cmd:automake
	cmd:cmake
	cmd:gcc$secondaryArchSuffix
	cmd:libtool
	cmd:m4
	cmd:make
	cmd:patch
	cmd:pkg_config$secondaryArchSuffix
	cmd:python2
	cmd:sed
	cmd:tar
	cmd:yasm
	"

# Please check the file state.md in this directory for a detailed description
# of the current state of the HandBrake port

PATCH()
{
	sed -i 's/MODULES += contrib\/.*//g' make/include/main.defs
	INCLUDE_DIR=`finddir B_SYSTEM_DIRECTORY`/$relativeIncludeDir
	sed -i "s|/system/develop/headers|$INCLUDE_DIR|g" libhb/module.defs
	sed -i 's/i686/i586/g' make/include/gcc.defs
}

BUILD()
{
	./configure --prefix=$prefix --force --disable-x265
	cd build
	make $jobArgs
}

INSTALL()
{
	cd build
	make install
	local MAJOR="`echo "$portVersion" | cut -d. -f1`"
	local MIDDLE="`echo "$portVersion" | cut -d. -f2`"
	local MINOR="`echo "$portVersion" | cut -d. -f3`"
	sed \
		-e "s|@MAJOR@|$MAJOR|" \
		-e "s|@MIDDLE@|$MIDDLE|" \
		-e "s|@MINOR@|$MINOR|" \
		$portDir/additional-files/HandBrake.rdef > HandBrake.rdef
	addResourcesToBinaries HandBrake.rdef $prefix/bin/HandBrakeCLI
}
