SUMMARY="Open source Portable Document Format (PDF) viewer"
DESCRIPTION="Xpdf is a free PDF viewer and toolkit, including a text extractor, image \
converter, HTML converter, and more. Most of the tools are available as \
open source."
HOMEPAGE="https://www.xpdfreader.com/"
COPYRIGHT="1996-2021 Glyph & Cog, LLC."
LICENSE="GNU GPL v2"
REVISION="1"
SOURCE_URI="http://dl.xpdfreader.com/xpdf-$portVersion.tar.gz"
CHECKSUM_SHA256="0fe4274374c330feaadcebb7bd7700cb91203e153b26aa95952f02bf130be846"
SOURCE_DIR="xpdf-$portVersion"
ADDITIONAL_FILES="xpdfreader.rdef.in"

ARCHITECTURES="all !x86_gcc2"
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="
	xpdf$secondaryArchSuffix = $portVersion
	app:XpdfViewer$commandSuffix = $portVersion
	cmd:pdftopng$commandSuffix = $portVersion
	cmd:xpdf$commandSuffix = $portVersion
	"
REQUIRES="
	haiku$secondaryArchSuffix
	lib:libfreetype$secondaryArchSuffix
	lib:libjpeg$secondaryArchSuffix
	lib:libpng16$secondaryArchSuffix
	lib:libQt5Core$secondaryArchSuffix
	lib:libQt5Gui$secondaryArchSuffix
	lib:libQt5Widgets$secondaryArchSuffix
	lib:libtiff$secondaryArchSuffix
	lib:libz$secondaryArchSuffix
	"

BUILD_REQUIRES="
	haiku${secondaryArchSuffix}_devel
	devel:libfreetype$secondaryArchSuffix
	devel:libjpeg$secondaryArchSuffix
	devel:libpng16$secondaryArchSuffix
	devel:libQt5Core$secondaryArchSuffix
	devel:libQt5Gui$secondaryArchSuffix
	devel:libQt5Widgets$secondaryArchSuffix
	devel:libtiff$secondaryArchSuffix
	devel:libz$secondaryArchSuffix
	"
BUILD_PREREQUIRES="
	cmd:awk
	cmd:cmake
	cmd:gcc$secondaryArchSuffix
	cmd:ld$secondaryArchSuffix
	cmd:make
	"

BUILD()
{
	cmake -B build -S . -DCMAKE_BUILD_TYPE=Release \
		-DCMAKE_INSTALL_PREFIX=$prefix \
		-DCMAKE_INSTALL_MANDIR=$manDir \
		-DSPLASH_CMYK=ON -L
	make -C build $jobArgs
}

INSTALL()
{
	make -C build install

	mkdir -p $appsDir
	mv $prefix/bin/xpdf $appsDir/XpdfReader
	ln -s $appsDir/XpdfReader $prefix/bin/xpdf

	# remove stuff provided by poppler
	for tool in pdfdetach pdffonts pdfimages pdfinfo pdftoppm pdftops pdftotext pdftohtml; do
		rm "$commandBinDir/${tool}" "$manDir/man1/${tool}.1"
	done

	local APP_SIGNATURE="application/x-vnd.xpdf"
	local MAJOR="`echo "$portVersion" | cut -d. -f1`"
	local MIDDLE="`echo "$portVersion" | cut -d. -f2`"
	local MINOR="`echo "$portVersion" | cut -d. -f3`"
	local MINOR="01"
	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/xpdfreader.rdef.in > xpdfreader.rdef

	addResourcesToBinaries xpdfreader.rdef $appsDir/XpdfReader
	addAppDeskbarSymlink $appsDir/XpdfReader
}
