SUMMARY="Multi-platform WYSIWYG ebook editor for ePub format"
DESCRIPTION="Sigil is a free, open source, multi-platform ebook editor that uses \
Qt (and QtWebKit). It is designed to edit books in ePub format (both ePub 2 and ePub 3)."
HOMEPAGE="https://sigil-ebook.com/"
COPYRIGHT="2009-2019 Sigil team"
LICENSE="GNU GPL v3"
REVISION="2"
SOURCE_URI="https://github.com/threedeyes/Sigil_WebKit/archive/$portVersion.tar.gz"
CHECKSUM_SHA256="3819e30d72c09b84798294a9ee39c635b1dd034661955f8665631f9bcce01ac5"
SOURCE_DIR="Sigil_WebKit-$portVersion"

ADDITIONAL_FILES="sigil.rdef.in"

PYTHON_VERSION="3.7"

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

PROVIDES="
	sigil$secondaryArchSuffix = $portVersion
	app:Sigil$secondaryArchSuffix = $portVersion
	"
REQUIRES="
	haiku$secondaryArchSuffix
	chardet_python3
	cssselect_python3
	html5lib_python3
	lxml_python3
	six_python3
	cmd:python$PYTHON_VERSION
	lib:libhunspell_1.7$secondaryArchSuffix
	lib:libminizip$secondaryArchSuffix
	lib:libpcre16$secondaryArchSuffix
	lib:libQt5Core$secondaryArchSuffix
	lib:libQt5WebKit$secondaryArchSuffix
	lib:libQt5WebKitWidgets$secondaryArchSuffix
	lib:libz$secondaryArchSuffix
	"

BUILD_REQUIRES="
	haiku${secondaryArchSuffix}_devel
	chardet_python3
	cssselect_python3
	html5lib_python3
	lxml_python3
	six_python3
	devel:libgl$secondaryArchSuffix
	devel:libhunspell_1.7$secondaryArchSuffix
	devel:libminizip$secondaryArchSuffix
	devel:libpcre16$secondaryArchSuffix
	devel:libQt5Core$secondaryArchSuffix
	devel:libQt5WebKit$secondaryArchSuffix
	devel:libz$secondaryArchSuffix
	"
BUILD_PREREQUIRES="
	cmd:cmake
	cmd:gcc$secondaryArchSuffix
	cmd:lrelease$secondaryArchSuffix >= 5
	cmd:make
	cmd:pkg_config$secondaryArchSuffix
	cmd:python$PYTHON_VERSION
	"

BUILD()
{
	mkdir -p build
	cd build

	pythonBin=/bin/python${PYTHON_VERSION}
	pythonLib=/system/$relativeDevelopLibDir/libpython${PYTHON_VERSION}m.so
	pythonInclude=/system/develop/headers/python${PYTHON_VERSION}m

	cmake .. \
		-DCMAKE_BUILD_TYPE=Release \
		-DCMAKE_INSTALL_PREFIX=$appsDir/Sigil \
		-DCMAKE_INSTALL_BINDIR=$appsDir/Sigil \
		-DSHARE_INSTALL_PREFIX=$appsDir/Sigil \
		-DCMAKE_INSTALL_LIBDIR=$appsDir/Sigil \
		-DPYTHON_LIBRARY=$pythonLib \
		-DPYTHON_INCLUDE_DIR=$pythonInclude \
		-DPYTHON_EXECUTABLE=$pythonBin \
		-DUSE_SYSTEM_LIBS=1 \
		-DINSTALL_BUNDLED_DICTS=0
	make $jobArgs
}

INSTALL()
{
	cd build
	make install

	mv $appsDir/Sigil/sigil/sigil $appsDir/Sigil/Sigil
	ln -s $appsDir/Sigil/sigil/libsigilgumbo.so $appsDir/Sigil/libsigilgumbo.so
	ln -s /system/$relativeLibDir/libhunspell-1.7.so.0 $appsDir/Sigil/libhunspell.so
	rm -rf $appsDir/Sigil/{share/applications,share/pixmaps}

	local APP_SIGNATURE="application/x-vnd.sigil"
	local MAJOR="`echo "$portVersion" | cut -d. -f1`"
	local MIDDLE="`echo "$portVersion" | cut -d. -f2`"
	local MINOR="`echo "$portVersion" | cut -d. -f3`"
	local LONG_INFO="$SUMMARY"
	sed \
		-e "s|@APP_SIGNATURE@|$APP_SIGNATURE|" \
		-e "s|@MAJOR@|$MAJOR|" \
		-e "s|@MIDDLE@|$MIDDLE|" \
		-e "s|@MINOR@|$MINOR|" \
		-e "s|@LONG_INFO@|$LONG_INFO|" \
		$portDir/additional-files/sigil.rdef.in \
			> sigil.rdef

	addResourcesToBinaries sigil.rdef $appsDir/Sigil/Sigil

	addAppDeskbarSymlink $appsDir/Sigil/Sigil
}
