SUMMARY="Video editing library used by OpenShot"
DESCRIPTION="libopenshot is an open-source, cross-platform C++ library \
dedicated to delivering high quality video editing, animation, and playback \
solutions to the world. This is the same library which powers \
OpenShot Video Editor (version 2.0+). C++, Python are fully supported."
HOMEPAGE="https://www.openshot.org/"
COPYRIGHT="2008-2021 OpenShot Studios, LLC"
LICENSE="GNU LGPL v3"
REVISION="1"
SOURCE_URI="https://github.com/OpenShot/libopenshot/archive/v$portVersion.tar.gz"
CHECKSUM_SHA256="568eab6d69d469c5f745f0e25387ca5e000f7c28be48417b0d7770577ac74a28"
SOURCE_FILENAME="libopenshot-$portVersion.tar.gz"
PATCHES="libopenshot-$portVersion.patchset"

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

PYTHON_VERSION="3.8"
libVersion="20"

PROVIDES="
	libopenshot$secondaryArchSuffix = $portVersion
	lib:libopenshot$secondaryArchSuffix = $libVersion
	"
REQUIRES="
	haiku$secondaryArchSuffix
	lib:libavcodec$secondaryArchSuffix
	lib:libavformat$secondaryArchSuffix
	lib:libavutil$secondaryArchSuffix
	lib:libgomp$secondaryArchSuffix
	lib:libjsoncpp$secondaryArchSuffix
	lib:libMagick++_6.Q16$secondaryArchSuffix
	lib:libMagickCore_6.Q16$secondaryArchSuffix
	lib:libopencv_core$secondaryArchSuffix
	lib:libopenshot_audio$secondaryArchSuffix
	lib:libQt5Core$secondaryArchSuffix
	lib:libQt5Gui$secondaryArchSuffix
	lib:libQt5Widgets$secondaryArchSuffix
	lib:libswresample$secondaryArchSuffix
	lib:libswscale$secondaryArchSuffix
	lib:libzmq$secondaryArchSuffix
	"

PROVIDES_devel="
	libopenshot${secondaryArchSuffix}_devel = $portVersion compat >= 0
	devel:libopenshot${secondaryArchSuffix} = $libVersion
	"
REQUIRES_devel="
	libopenshot$secondaryArchSuffix == $portVersion base
	"

SUMMARY_python38="$SUMMARY (python3 bindings)"
PROVIDES_python38="
	libopenshot${secondaryArchSuffix}_python38 = $portVersion
	"
REQUIRES_python38="
	libopenshot$secondaryArchSuffix == $portVersion base
	lib:libavutil$secondaryArchSuffix
	lib:libopenshot_audio$secondaryArchSuffix
	lib:libpython${PYTHON_VERSION}$secondaryArchSuffix
	"

if [ "$targetArchitecture" = "x86_gcc2" ]; then
	PROVIDES_python38+="
		libopenshot_python38 = $portVersion
		"
fi

SUMMARY_python38_debuginfo="$SUMMARY (python3 bindings debug info)"
PROVIDES_python38_debuginfo="
	libopenshot${secondaryArchSuffix}_python38_debuginfo = $portVersion
	debuginfo:_openshot.so(libopenshot_python38) = $portVersion
	"
REQUIRES_python38_debuginfo="
	libopenshot${secondaryArchSuffix}_python38 == $portVersion base
	"

BUILD_REQUIRES="
	haiku${secondaryArchSuffix}_devel
	unittest++${secondaryArchSuffix}_devel
	devel:libavcodec$secondaryArchSuffix
	devel:libavdevice$secondaryArchSuffix
	devel:libavformat$secondaryArchSuffix
	devel:libavutil$secondaryArchSuffix
	devel:libexecinfo$secondaryArchSuffix
	devel:libjsoncpp$secondaryArchSuffix
	devel:libMagick++_6.Q16$secondaryArchSuffix
	devel:libopencv_core$secondaryArchSuffix
	devel:libopenshot_audio$secondaryArchSuffix
	devel:libQt5Core$secondaryArchSuffix
	devel:libQt5Gui$secondaryArchSuffix
	devel:libQt5Multimedia$secondaryArchSuffix
	devel:libQt5MultimediaWidgets$secondaryArchSuffix
	devel:libswresample$secondaryArchSuffix
	devel:libswscale$secondaryArchSuffix
	devel:libzmq$secondaryArchSuffix
	devel:libz$secondaryArchSuffix
	"
BUILD_PREREQUIRES="
	cmd:cmake
	cmd:gcc$secondaryArchSuffix
	cmd:ld$secondaryArchSuffix
	cmd:make
	cmd:pkg_config$secondaryArchSuffix
	cmd:python${PYTHON_VERSION}
	cmd:swig
	"

defineDebugInfoPackage libopenshot$secondaryArchSuffix \
	$libDir/libopenshot.so.$portVersion

BUILD()
{
	python=$portPackageLinksDir/cmd~python${PYTHON_VERSION}/bin/python${PYTHON_VERSION}m
	pythonVersion=$($python --version 2>&1 | sed 's/Python //' | head -c3)
	installLocation=$prefix/lib/python$pythonVersion/vendor-packages/

	export LDFLAGS="-Wl,--as-needed" # Trim unneeded links

	cmake . $cmakeDirArgs \
		-DCMAKE_BUILD_TYPE=RelWithDebInfo \
		-DLIBOPENSHOT_AUDIO_INCLUDE_DIR=/system/$relativeIncludeDir/libopenshot-audio/JuceLibraryCode \
		-DPYTHON_MODULE_PATH=$installLocation \
		-DENABLE_RUBY=OFF \
		-DENABLE_TESTS=OFF \
		-DENABLE_DOCS=OFF \
		-DUSE_SYSTEM_JSONCPP=ON
	make $jobArgs
}

INSTALL()
{
	make install
	prepareInstalledDevelLibs libopenshot

	# devel package
	packageEntries devel \
		$developDir

	python=$portPackageLinksDir/cmd~python${PYTHON_VERSION}/bin/python${PYTHON_VERSION}
	pythonVersion=$($python --version 2>&1 | sed 's/Python //' | head -c3)
	pythonLocation=$prefix/lib/python$pythonVersion
	installLocation=$pythonLocation/vendor-packages
	pythonDebugInfoDir=$(getPackagePrefix python38_debuginfo)/$relativeDebugInfoDir
	mkdir -p $pythonLocation
	mv $prefix/lib/python/po/vendor-packages $pythonLocation
	extractDebugInfo $installLocation/_openshot.so \
		"$pythonDebugInfoDir/_openshot.so(libopenshot_python38-$portFullVersion).debuginfo"
	# python package
	packageEntries python38 \
		$pythonLocation
}

TEST()
{
	make $jobArgs test
}
