# Template file for 'mupdf'
# Static library, revbump all dependants on mupdf updates
pkgname=mupdf
version=1.24.9
revision=1
hostmakedepends="pkg-config zlib-devel libcurl-devel freetype-devel
 libjpeg-turbo-devel jbig2dec-devel libXext-devel libXcursor-devel
 libXrandr-devel libXinerama-devel harfbuzz-devel readline-devel
 MesaLib-devel libopenjpeg2-devel glu-devel libXi-devel libfreeglut-devel
 gumbo-parser-devel"
makedepends="$hostmakedepends"
depends="desktop-file-utils"
short_desc="Lightweight PDF and XPS viewer"
maintainer="cinerea0 <cinerea0@protonmail.com>"
license="AGPL-3.0-only"
homepage="https://mupdf.com"
changelog="https://mupdf.com/releases/history.html"
distfiles="https://mupdf.com/downloads/archive/mupdf-${version}-source.tar.lz"
checksum=d87da097ae943ad0113003190ed370d39bde817383c59dc753dce23c7ba2b710

pre_build() {
	if [ "$CROSS_BUILD" ]; then
		make ${makejobs} CC=cc LD=ld AR=ar CFLAGS="-fPIC" LDFLAGS='' build=release generate
	fi
}

do_build() {
	CFLAGS+=" -fPIC"
	CXXFLAGS+=" -fPIC"

	local _crosscompile

	if [ "$CROSS_BUILD" ]; then
		_crosscompile="CROSSCOMPILE=yes"
	fi

	make ${makejobs} USE_SYSTEM_LIBS=yes CURL_LIBS='-lcurl -lpthread' build=release ${_crosscompile} all
	make ${makejobs} CFLAGS="$CFLAGS" CXXFLAGS="$CXXFLAGS" build=release ${_crosscompile} -C thirdparty/mujs
}

do_install() {
	make USE_SYSTEM_LIBS=yes build=release prefix=${DESTDIR}/usr install
	for f in build/release/libmupdf-*.a; do
		vinstall $f 644 usr/lib
	done

	ln -rs ${DESTDIR}/usr/bin/mupdf-x11 ${DESTDIR}/usr/bin/mupdf

	vinstall docs/logo/mupdf-icon.xpm 644 usr/share/pixmaps mupdf.xpm
	for px in 16 24 32 48 72 128 256 512; do
		vinstall docs/logo/mupdf-icon-${px}.png 644 /usr/share/icons/hicolor/${px}x${px}/apps mupdf.png
	done
	vinstall docs/logo/mupdf-icon.svg 644 /usr/share/icons/hicolor/scalable/apps mupdf.svg
	vinstall ${FILESDIR}/mupdf.desktop 644 usr/share/applications

	vmkdir usr/lib/pkgconfig
	sed "s/@VERSION@/${version}/" ${FILESDIR}/mupdf.pc.in > ${DESTDIR}/usr/lib/pkgconfig/mupdf.pc
}

post_install() {
	vlicense COPYING LICENSE
}

mupdf-devel_package() {
	short_desc+=" - development files"
	depends="libjpeg-turbo-devel jbig2dec-devel libopenjpeg2-devel"
	pkg_install() {
		vmove usr/lib/pkgconfig
		vmove usr/include
		vmove "usr/lib/*.a"
	}
}

mupdf-tools_package() {
	short_desc+=" - tools"
	pkg_install() {
		vmove usr/bin/muraster
		vmove usr/bin/mutool
		vmove usr/share/man/man1/mutool.1
	}
}
