# Template file for 'netpbm'
pkgname=netpbm
# from http://svn.code.sf.net/p/netpbm/code/advanced/doc/HISTORY
version=10.92.00
revision=1
_githash=4c361c0a7638b4f068e8b524e4ff43374a286018
_githash_guide=2ab5fffd17f1e979ce63db9212331e51f53811fc
create_wrksrc=yes
build_wrksrc="netpbm-mirror-${_githash}"
hostmakedepends="pkg-config perl python flex"
makedepends="libX11-devel libjpeg-turbo-devel libpng-devel libxml2-devel
 tiff-devel zlib-devel"
short_desc="Toolkit for manipulation of graphic images"
maintainer="Leah Neukirchen <leah@vuxu.org>"
license="BSD-3-Clause,GPL-2.0-only,custom:netpbm"
homepage="http://netpbm.sourceforge.net/"
distfiles="https://github.com/leahneukirchen/netpbm-mirror/archive/${_githash}.tar.gz
 https://github.com/leahneukirchen/netpbm-mirror/archive/${_githash_guide}.tar.gz"
checksum="fec87c54b7ff7aeb78f9e2bd48f658821c58b86a41332d9c547b31f2ea8f2593
 542a9532d5c0a81ff47b3a65529670f51f45da4939150a8545e2f3a4af5b120b"

post_extract() {
	cd $build_wrksrc
	for p in ${FILESDIR}/*.patch; do
		patch -Np1 -i $p
	done

	cd ../netpbm-mirror-${_githash_guide}
	sed -i 's/ id="pammixmulti"//g' pammixmulti.html
}

# Liberation
# Some commands have been taken from:
# https://git.parabola.nu/abslibre.git/tree/libre/netpbm
post_patch() {
	# Removing hpcdtoppm/pcdindex (non comercial)
	rm -rv converter/ppm/hpcdtoppm

	# Removing ppmtogif/giftopnm (patent restriction)
	rm -v converter/other/giftopnm.c converter/ppm/ppmtogif.c
	rm -v test/gif-quant-roundtrip.test test/gif-roundtrip.test test/all-in-place.ok

	# Removing programs without license information
	rm -v other/pamchannel.c test/pamchannel.ok
	rm -v converter/other/pamtopnm.c
	rm -v converter/pbm/pbmto4425.c
	rm -v converter/pbm/pbmtoln03.c
	rm -v converter/pbm/pbmtolps.c
	rm -v converter/pbm/pbmtopk.c
	rm -v converter/pbm/pktopbm.c
	rm -v converter/ppm/ppmtopjxl.c
	rm -v converter/pgm/spottopgm.c

	rm -v test/avs-roundtrip.test test/facesaver-roundtrip.test test/ilbm-roundtrip.test test/pamchannel.test \
		test/pamditherbw.test test/pamenlarge.test test/pamfile.test test/pfm-roundtrip.test \
		test/pgmtoppm.test test/pict-roundtrip.test test/pnminvert.test test/pnmremap1.test \
		test/pnmremap2.test test/pnmtopnm-plain.test test/pnm-pam-roundtrip.test test/pnm-plain-roundtrip.test \
		test/ps-alt-roundtrip.test test/ps-roundtrip.test test/sbig-roundtrip.test test/st4-roundtrip.test \
		test/targa-roundtrip.test test/utahrle-roundtrip.test test/winicon-roundtrip.test test/xwd-roundtrip.test

	# Removing references about hpcdtoppm/pcdindex (non comercial)
	sed -i 's/hpcdtoppm//g' converter/ppm/Makefile
	sed -i 's/hpcdtoppm\.1//g' buildtools/manpage.mk
	sed -i 's/pcdindex\.1//g' buildtools/manpage.mk

	# Removing references about ppmtogif/giftopnm (patent restriction)
	sed -i 's/ppmtogif//g' converter/ppm/Makefile test/all-in-place.test
	sed -i 's/ppmtogif\.1//g' buildtools/manpage.mk
	sed -i 's/giftopnm//g' converter/other/Makefile test/all-in-place.test
	sed -i 's/giftopnm\.1//g' buildtools/manpage.mk
	sed -i '/giftopnm/d' converter/other/pnmtopalm/README

	# Removing references about programs without license information
	sed -i 's/pamchannel//g' other/Makefile test/all-in-place.test
	sed -i 's/pamchannel\.1//g' buildtools/manpage.mk
	sed -i 's/pamchannel\.test//g' test/Test-Order

	sed -i 's/pamtopnm//g' converter/other/Makefile test/all-in-place.test
	sed -i 's/pamtopnm\.1//g' buildtools/manpage.mk
	sed -i '/pnmtopnm/d' netpbm.c

	sed -i 's/pbmto4425//g' converter/pbm/Makefile test/all-in-place.test
	sed -i 's/pbmto4425\.1//g' buildtools/manpage.mk

	sed -i 's/pbmtoln03//g' converter/pbm/Makefile test/all-in-place.test
	sed -i 's/pbmtoln03\.1//g' buildtools/manpage.mk

	sed -i 's/pbmtolps//g' converter/pbm/Makefile test/all-in-place.test
	sed -i 's/pbmtolps\.1//g' buildtools/manpage.mk

	sed -i 's/pbmtopk//g' converter/pbm/Makefile test/all-in-place.test
	sed -i 's/pbmtopk\.1//g' buildtools/manpage.mk

	sed -i 's/pktopbm//g' converter/pbm/Makefile test/all-in-place.test
	sed -i 's/pktopbm\.1//g' buildtools/manpage.mk

	sed -i 's/ppmtopjxl//g' converter/ppm/Makefile test/all-in-place.test
	sed -i 's/ppmtopjxl\.1//g' buildtools/manpage.mk

	sed -i 's/spottopgm//g' converter/pgm/Makefile test/all-in-place.test
	sed -i 's/spottopgm\.1//g' buildtools/manpage.mk

	#Removing references about nonfree licenses part
	sed -i '338,383d' doc/copyright_summary
}

do_configure() {
	local endian bitsperlong
	cat config.mk.in ${FILESDIR}/config.mk >config.mk
	if [ "$XBPS_TARGET_MACHINE" = "i686" -o "$XBPS_TARGET_MACHINE" = "x86_64" ]; then
		echo "WANT_SSE = Y" >>config.mk
	fi
	sed -i 's|misc|share/netpbm|' common.mk
	sed -i 's|/link|/lib|' lib/Makefile
	sed -i 's|/sharedlink|/lib|' lib/Makefile
	sed -i 's|install.manwebmain install.manweb install.man|install.man|' GNUmakefile
	if [ "$CROSS_BUILD" ]; then
		# Patch buildutils/endiangen.c to return the endianess
		# and word size for the target architecture
		# XXX: perhaps add XBPS_ENDIAN to common/cross-profiles/*.sh
		case "$XBPS_TARGET_MACHINE" in
			ppc|ppc-musl|ppc64|ppc64-musl) endian="ENDIAN_BIG";;
			*) endian="ENDIAN_LITTLE";;
		esac
		# XXX: perhaps add XBPS_BITSPERLONG to common/cross-files/*.sh
		case "$XBPS_TARGET_MACHINE" in
			aarch64*|x86_64*|ppc64*) bitsperlong="64";;
			*) bitsperlong="32";;
		esac
		vsed -i buildtools/endiangen.c \
			-e "s;byteOrder();$endian;" \
			-e "s;bitsPerLong();$bitsperlong;"
		# ... now build the tools for the host
		CC=$CC_host CFLAGS="$CFLAGS_host" \
		LD=$LD_host LDFLAGS="$LDFLAGS_host" \
			make -C buildtools
	fi
}

do_build() {
	make ${makejobs} CC=$CC CFLAGS="$CFLAGS" LDFLAGS_FOR_BUILD=
	make MAKEMAN="${PWD}/buildtools/makeman" USERGUIDE=. \
		-C ../netpbm-mirror-${_githash_guide} \
		-f "${PWD}/buildtools/manpage.mk" manpages
}

do_install() {
	make pkgdir=$DESTDIR/usr PKGMANDIR=share/man install-run install-dev
	vmkdir usr/share/man/man1
	vmkdir usr/share/man/man3
	vmkdir usr/share/man/man5
	make MAKEMAN="${PWD}/buildtools/makeman" MANDIR="$DESTDIR/usr/share/man" USERGUIDE=. \
		-C ../netpbm-mirror-${_githash_guide} \
		-f "${PWD}/buildtools/manpage.mk" installman
	vlicense doc/copyright_summary LICENSE
}

libnetpbm_package() {
	replaces="${sourcepkg}<10.66.03_2"
	short_desc+=" - runtime libraries"
	pkg_install() {
		vmove usr/lib/*.so.*
	}
}

libnetpbm-devel_package() {
	replaces="${sourcepkg}<10.66.03_2"
	depends="libnetpbm>=${version}_${revision}"
	short_desc+=" - development files"
	pkg_install() {
		vmove usr/include
		vmove usr/lib/*.so
	}
}
