SUMMARY="Ebook management application"
DESCRIPTION="Calibre is an e-book manager. It can view, convert, edit and \
catalog e-books in all of the major e-book formats. It can also talk to \
e-book reader devices. It can go out to the internet and fetch metadata for \
your books. It can download newspapers and convert them into e-books for \
convenient reading. It is cross platform, running on Linux, Windows and macOS."
HOMEPAGE="https://calibre-ebook.com/"
COPYRIGHT="2008-2019 Kovid Goyal"
LICENSE="GNU GPL v3"
REVISION="2"
SOURCE_URI="https://download.calibre-ebook.com/$portVersion/calibre-$portVersion.tar.xz"
CHECKSUM_SHA256="3571efc1cc773e453b459096a38b141fd0c7784fb9611505a1f9a55648f47e5e"
PATCHES="calibre-$portVersion.patchset"
ADDITIONAL_FILES="calibre.rdef.in"

ARCHITECTURES="all ?x86_gcc2 ?x86"
SECONDARY_ARCHITECTURES="?x86"

# On x86_gcc2 we don't want to install the commands in bin/<arch>/, but in bin/.
commandSuffix=$secondaryArchSuffix
commandBinDir=$binDir
if [ "$targetArchitecture" = x86_gcc2 ]; then
	commandSuffix=
	commandBinDir=$prefix/bin
fi

PROVIDES="
	calibre$secondaryArchSuffix = $portVersion
	cmd:calibre_complete$commandSuffix
	cmd:calibre_customize$commandSuffix
	cmd:calibre_debug$commandSuffix
	cmd:calibre_parallel$commandSuffix
	cmd:calibre_server$commandSuffix
	cmd:calibre_smtp$commandSuffix
	cmd:calibre_uninstall$commandSuffix
	cmd:calibre$commandSuffix
	cmd:calibredb$commandSuffix
	cmd:ebook_convert$commandSuffix
	cmd:ebook_device$commandSuffix
	cmd:ebook_edit$commandSuffix
	cmd:ebook_meta$commandSuffix
	cmd:ebook_polish$commandSuffix
	cmd:ebook_viewer$commandSuffix
	cmd:fetch_ebook_metadata$commandSuffix
	cmd:lrf2lrs$commandSuffix
	cmd:lrfviewer$commandSuffix
	cmd:lrs2lrf$commandSuffix
	cmd:markdown_calibre$commandSuffix
	cmd:web2disk$commandSuffix
	"
REQUIRES="
	dbus$secondaryArchSuffix
	haiku$secondaryArchSuffix
	cmd:python3.8
	apsw_python38
	beautifulsoup_python38
	chardet_python38
	css_parser_python38
	dateutil_python38
	dbuspython_python38
	dnspython_python38
	dukpy_python38
	feedparser_python38
	html2text_python38
	html5_parser_python38
	html5lib_python38
	lxml_python38
	markdown_python38
	mechanize_python38
	msgpack_python38
#	netifaces_python38
	pillow_python38
#	psutil_python38
	pycrypto_python38
	pygments_python38
	pyqt5_python38
	pyqtwebengine5_python38
	regex_python38
	setuptools_python38
	six_python38
	soupsieve_python38
	unrardll_python38
	webencodings_python38
	pythonzeroconf_python38
#	lib:libdbus_1$secondaryArchSuffix
#	lib:libdbus_glib_1$secondaryArchSuffix
#	lib:libeasylzma$secondaryArchSuffix
	lib:libexpat$secondaryArchSuffix
	lib:libffi$secondaryArchSuffix
	lib:libfontconfig$secondaryArchSuffix
	lib:libfreetype$secondaryArchSuffix
	lib:libgcrypt$secondaryArchSuffix
	lib:libglib_2.0$secondaryArchSuffix
	lib:libgpg_error$secondaryArchSuffix
	lib:libhunspell_1.7$secondaryArchSuffix
	lib:libhyphen$secondaryArchSuffix
	lib:libiconv$secondaryArchSuffix
	lib:libicui18n$secondaryArchSuffix >= 66
	lib:libjpeg$secondaryArchSuffix
#	lib:libjxrlib$secondaryArchSuffix
#	lib:libmozjpeg$secondaryArchSuffix
	lib:libmtp$secondaryArchSuffix
	lib:libncursesw$secondaryArchSuffix
	lib:libpng16$secondaryArchSuffix
	lib:libpodofo$secondaryArchSuffix
	lib:libpoppler$secondaryArchSuffix
	lib:libpoppler_qt5$secondaryArchSuffix
	lib:libQt5Core$secondaryArchSuffix
	lib:libQt5WebEngine$secondaryArchSuffix
	lib:libreadline$secondaryArchSuffix
	lib:libstemmer$secondaryArchSuffix
	lib:libsqlite3$secondaryArchSuffix
	lib:libssl$secondaryArchSuffix
	lib:libusb_1.0$secondaryArchSuffix
	lib:libwebp$secondaryArchSuffix
	lib:libxml2$secondaryArchSuffix
	lib:libxslt$secondaryArchSuffix
	lib:libz$secondaryArchSuffix
	"

BUILD_REQUIRES="
	haiku${secondaryArchSuffix}_devel
	apsw_python38
	beautifulsoup_python38
	chardet_python38
	css_parser_python38
	dateutil_python38
#	dbuspython_python38
	dnspython_python38
	dukpy_python38
	feedparser_python38
	html2text_python38
	html5_parser_python38
	html5lib_python38
	lxml_python38
	markdown_python38
	mechanize_python38
	msgpack_python38
#	netifaces_python38
	pillow_python38
#	psutil_python38
	pycrypto_python38
	pygments_python38
	pyqt5_python38
	pyqt_builder_python38
	regex_python38
	setuptools_python38
	sip_python38
#	sip_pyqt_python38
	six_python38
	soupsieve_python38
	unrardll_python38
	webencodings_python38
	pythonzeroconf_python38
	devel:libchm$secondaryArchSuffix
#	devel:libdbus_1$secondaryArchSuffix
#	devel:libdbus_glib_1$secondaryArchSuffix
#	devel:libeasylzma$secondaryArchSuffix
	devel:libexpat$secondaryArchSuffix
	devel:libffi$secondaryArchSuffix
	devel:libfontconfig$secondaryArchSuffix
	devel:libfreetype$secondaryArchSuffix
	devel:libgcrypt$secondaryArchSuffix
	devel:libglib_2.0$secondaryArchSuffix
	devel:libgpg_error$secondaryArchSuffix
	devel:libhunspell_1.7$secondaryArchSuffix
	devel:libhyphen$secondaryArchSuffix
	devel:libiconv$secondaryArchSuffix
	devel:libicui18n$secondaryArchSuffix >= 66
	devel:libjpeg$secondaryArchSuffix
#	devel:libjxrlib$secondaryArchSuffix
#	devel:libmozjpeg$secondaryArchSuffix
	devel:libmtp$secondaryArchSuffix
	devel:libncursesw$secondaryArchSuffix
	devel:libpng16$secondaryArchSuffix
	devel:libpodofo$secondaryArchSuffix
	devel:libpoppler$secondaryArchSuffix
	devel:libpoppler_qt5$secondaryArchSuffix
	devel:libQt5Core$secondaryArchSuffix
	devel:libQt5WebEngine$secondaryArchSuffix
	devel:libreadline$secondaryArchSuffix
	devel:libstemmer$secondaryArchSuffix
	devel:libsqlite3$secondaryArchSuffix
	devel:libssl$secondaryArchSuffix
	devel:libusb_1.0$secondaryArchSuffix
	devel:libwebp$secondaryArchSuffix
	devel:libxml2$secondaryArchSuffix
	devel:libxslt$secondaryArchSuffix
	devel:libz$secondaryArchSuffix
	"
BUILD_PREREQUIRES="
	cmd:bzip2
	cmd:cmake
	cmd:gcc$secondaryArchSuffix
	cmd:make
	cmd:nasm
	cmd:optipng
	cmd:pkg_config$secondaryArchSuffix
	cmd:python3.8
	cmd:pyrcc5
	cmd:qmake >= 5
	cmd:sip
	cmd:unrar
	"

BUILD()
{
	# For current REQUIMENTS check:
	# https://github.com/kovidgoyal/build-calibre/blob/master/scripts/sources.json

	export PODOFO_INC_DIR=`finddir B_SYSTEM_HEADERS_DIRECTORY`/podofo
	python=python3.8
	installLocation=$prefix/../pyqt5_python38/lib/$python/vendor-packages
	export BINDINGS_DIR=$installLocation/PyQt5/bindings/

	LANG="en_US.UTF-8" $python setup.py build
	LANG="en_US.UTF-8" $python setup.py gui
}

INSTALL()
{
	export PODOFO_INC_DIR=`finddir B_SYSTEM_HEADERS_DIRECTORY`/podofo

	python=python3.8
	$python setup.py install \
		--prefix=$appsDir/Calibre

	mv $appsDir/Calibre/bin/calibre $appsDir/Calibre/bin/Calibre

	# Fix python path for scripts
	sed -i 's|/usr/bin/env python3|/bin/python3.8|' $appsDir/Calibre/bin/*

	# Preparing the attributes
	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/calibre.rdef.in \
			> calibre.rdef

	# Installing extra-attribs
	rc calibre.rdef

	scriptList="Calibre ebook-edit ebook-viewer lrfviewer"
	for scriptName in $scriptList; do
		settype -t application/x-vnd.Be-elfexecutable $appsDir/Calibre/bin/$scriptName
		resattr -o $appsDir/Calibre/bin/$scriptName calibre.rsrc
	done

	addAppDeskbarSymlink $appsDir/Calibre/bin/Calibre
}

TEST()
{
	export SIP_DIR=`finddir B_SYSTEM_DATA_DIRECTORY`
	export PODOFO_INC_DIR=`finddir B_SYSTEM_HEADERS_DIRECTORY`/podofo

	python3.8 setup.py test
}
