# Template file for 'calibre'
pkgname=calibre
version=6.13.0
revision=1
build_wrksrc=calibre
build_style=python3-module
pycompile_dirs="/usr/lib/calibre/"
hostmakedepends="pkg-config cmake python3-BeautifulSoup4 python3-Pillow
 python3-apsw python3-css-parser python3-dateutil sip qt6-base python3-py7zr
 python3-html5-parser python3-msgpack python3-regex python3-mechanize
 mathjax liberation-fonts-ttf python3-zeroconf xdg-utils rapydscript-ng
 python3-PyQt-builder python3-Markdown"
makedepends="libinput-devel libmagick-devel libmtp-devel libstemmer-devel
 libpodofo-devel libwmf-devel libxkbcommon-devel poppler-cpp-devel
 freetype-devel uchardet-devel openssl-devel icu-devel libusb-devel
 sqlite-devel tslib-devel hunspell-devel hyphen-devel libstemmer-devel
 python3-pyqt6-devel python3-pyqt6-webengine-devel"
depends="desktop-file-utils optipng poppler-utils python3-BeautifulSoup4
 qt6-imageformats python3-Pygments python3-pycryptodome
 python3-Markdown python3-Pillow python3-zeroconf python3-Pygments python3-apsw
 python3-css-parser python3-cssselect python3-dateutil python3-jeepney
 python3-dnspython python3-feedparser python3-html5-parser python3-mechanize
 python3-msgpack python3-netifaces python3-psutil python3-regex python3-html2text
 python3-pychm python3-cchardet speech-dispatcher python3-sgmllib
 python3-html5lib python3-mechanize python3-py7zr mathjax liberation-fonts-ttf
 python3-pyqt6-network python3-pyqt6-dbus python3-pyqt6-opengl-widgets
 python3-pyqt6-widgets python3-pyqt6-gui python3-pyqt6-declarative
 python3-pyqt6-printsupport python3-pyqt6-webchannel python3-pyqt6-svg
 python3-pyqt6-webengine"
checkdepends="${depends} libjpeg-turbo-tools jxrlib"
short_desc="Ebook management application"
maintainer="skmpz <dem.procopiou@gmail.com>"
license="GPL-3.0-only"
homepage="https://calibre-ebook.com"
changelog="https://raw.githubusercontent.com/kovidgoyal/calibre/master/Changelog.txt"
_lover=7.4.3.2
distfiles="https://download.calibre-ebook.com/${version}/calibre-${version}.tar.xz
 https://download.documentfoundation.org/libreoffice/src/${_lover%.*}/libreoffice-dictionaries-${_lover}.tar.xz"
checksum="eb40f34b7106e3f36d37e0b65dacb5cec863665c68fb2350592dbc1ec133aea3
 3a55d5807b763827f820c4aee0a0fd9f44520e70629c9eb9748e1f8c57c64389"
python_version=3
lib32disabled=yes
nocross=yes

post_extract() {
	mv calibre-${version} calibre
}

do_configure() {
	# Remove unneeded files and libs
	rm -rf resources/${pkgname}-portable.*

	# Desktop integration (e.g. enforce arch defaults)
	vsed -i src/calibre/linux.py \
		-e "/import config_dir/,/os.rmdir(config_dir)/d" \
		-e "s/'ctc-posml'/'text' not in mt and 'pdf' not in mt and 'xhtml'/" \
		-e "s/^Name=calibre/Name=Calibre/g"
	cat >"${XBPS_WRAPPERDIR}/sip-qmake" <<-_EOF
	#!/bin/sh
	exec /usr/lib/qt6/bin/qmake \\
		"\$@" \\
		PREFIX=/usr \\
		QT_INSTALL_PREFIX=/usr \\
		LIB=/usr/lib \\
		QMAKE_CC="$CC" QMAKE_CXX="$CXX" \\
		QMAKE_LINK="$CXX" QMAKE_LINK_C="$CC" \\
		QMAKE_CFLAGS+="$CFLAGS" \\
		QMAKE_CXXFLAGS+="$CXXFLAGS" \\
		QMAKE_LFLAGS+="$LDFLAGS" \\
		CONFIG+=no_qt_rpath
	_EOF
	chmod 755 ${XBPS_WRAPPERDIR}/sip-qmake
}

do_build() {
	export QMAKE=${XBPS_WRAPPERDIR}/sip-qmake
	python3 setup.py build
	python3 setup.py gui
	python3 setup.py kakasi
	python3 setup.py hyphenation \
		--path-to-hyphenation "$wrksrc/libreoffice-${_lover}/dictionaries"
	python3 setup.py liberation_fonts \
		--system-liberation_fonts \
		--path-to-liberation_fonts /usr/share/fonts/TTF
	python3 setup.py mathjax \
		--path-to-mathjax /usr/share/mathjax --system-mathjax
	python3 setup.py iso639
	python3 setup.py iso3166
	# python3 setup.py recent_uas
	python3 setup.py rapydscript
	python3 setup.py translations
	python3 setup.py resources \
		--path-to-hyphenation "$wrksrc/libreoffice-${_lover}/dictionaries" \
		--system-liberation_fonts \
		--path-to-liberation_fonts /usr/share/fonts/TTF \
		--path-to-mathjax /usr/share/mathjax --system-mathjax
}

do_check() {
	export QMAKE=${XBPS_WRAPPERDIR}/sip-qmake
	# qt test crashes without any error messages
	# disabled for now
	# unrar test fails because python3-unrardll isn't packaged
	# searching test case fails (non-deterministic)
	# test_dom_load case fails
	python3 setup.py test \
		--exclude-test-name unrar \
		--exclude-test-name qt \
		--exclude-test-name searching \
		--exclude-test-name test_dom_load
}

do_install() {
	export QMAKE=${XBPS_WRAPPERDIR}/sip-qmake
	vmkdir usr/share/zsh/site-functions
	vmkdir usr/share/man

	python3 setup.py \
		install --prefix=/usr --staging-root=${DESTDIR}/usr
	vcopy "man-pages/*" usr/share/man

	# fake some user-agent, otherwise we need to build recent_uas target,
	# which generates a (very long) list from current data,
	cat <<-'EOF' >${DESTDIR}/usr/share/calibre/user-agent-data.json
{
  "chrome_versions": [
    {"chrome_version":"110.0.5481","date":"2023-02-07","webkit_version":"110"},
    {"chrome_version":"109.0.5414","date":"2023-01-10","webkit_version":"109"}
  ],
  "common_user_agents": [
    "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36",
    "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36"
  ],
  "desktop_platforms": ["Windows NT 10.0; Win64; x64"],
  "firefox_versions": ["110.0","109.0","108.0","102.0"],
  "timestamp": "2023-02-24T04:17:21.721566+00:00",
  "user_agents_popularity": {
    "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36": 252652,
    "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36": 115713
  }
}
	EOF
}
