# Template file for 'texlive'
pkgname=texlive
version=20200406
revision=3
wrksrc="texlive-${version}-source"
build_wrksrc="build"
build_style=gnu-configure
configure_script="../configure"
configure_args="
 --enable-biber
 --enable-epsfwin
 --enable-ipc
 --enable-luajittex
 --enable-luajithbtex
 --enable-mfluajit
 --enable-mfluajit-nowin
 --enable-luatex
 --enable-luahbtex
 --enable-mflua
 --enable-mfluajit
 --enable-mfluajit-nowin
 --enable-mftalkwin
 --enable-regiswin
 --enable-shared
 --enable-tektronixwin
 --enable-unitermwin
 --enable-xetex
 --enable-dvipng
 --enable-dvipsk
 --enable-ps2eps
 --enable-psutils
 --disable-bibtex-x
 --disable-chktex
 --disable-cjkutils
 --disable-detex
 --disable-dialog
 --disable-dvi2tty
 --disable-dvisvgm
 --disable-largefile
 --disable-lcdf-typetools
 --disable-multiplatform
 --disable-native-texlive-build
 --disable-pdfopen
 --disable-ps2pkm
 --disable-t1utils
 --disable-tex4htk
 --disable-ttf2pk2
 --disable-vlna
 --disable-xindy
 --with-ps=gs
 --with-banner-add=/Ymir GNU/Linux-libre
 --with-system-cairo
 --with-system-freetype2
 --with-system-gd
 --with-system-graphite2
 --with-system-gmp
 --with-system-harfbuzz
 --with-system-icu
 --with-system-libpaper
 --with-system-libpng
 --with-system-mpfr
 --with-system-poppler
 --with-system-pixman
 --with-system-teckit
 --with-system-xpdf
 --with-system-zlib
 --with-system-zziplib
 --with-xdvi-x-toolkit=Xaw"
hostmakedepends="pkg-config perl lua52-BitOp texinfo"
makedepends="cairo-devel freetype-devel gd-devel graphite-devel gmp-devel
 harfbuzz-devel icu-devel libpaper-devel libpng-devel mpfr-devel
 poppler-devel pixman-devel libteckit-devel zlib-devel zziplib-devel
 libXaw-devel"
depends="dialog ghostscript perl-Tk texlive-core xbps-triggers>=0.115_1"
short_desc="TeX Live"
maintainer="fosslinux <fosslinux@aussies.space>"
license="GPL-2.0-or-later"
homepage="https://www.tug.org/texlive/"
distfiles="ftp://tug.org/texlive/historic/2020/texlive-${version}-source.tar.xz"
checksum=e32f3d08cbbbcf21d8d3f96f2143b64a1f5e4cb01b06b761d6249c8785249078
python_version=3
# Virtual package cares only about year part of version
provides="tex-${version%${version#????}}_1"

if [ "$CROSS_BUILD" ] ; then
	# Tangle is required for cross compile
	hostmakedepends+=" texlive"
fi

case "$XBPS_TARGET_MACHINE" in
	mips*) broken="cannot find libraries" ;;
esac

build_options="luajit"

_luajit_host_cc="cc"
if [ "$XBPS_WORDSIZE" != "$XBPS_TARGET_WORDSIZE" ] ; then
	case "$XBPS_MACHINE" in
		x86_64*)
			hostmakedepends+=" cross-i686-linux-musl"
			_luajit_host_cc="i686-linux-musl-gcc -static"
			;;
		ppc64le*) _luajit_host_cc="broken" ;; # no 32-bit counterpart yet
		ppc64*)
			hostmakedepends+=" cross-powerpc-linux-musl"
			_luajit_host_cc="powerpc-linux-musl-gcc -static"
			;;
		*) _luajit_host_cc="broken" ;;
	esac
fi

if [ "${_luajit_host_cc}" != "broken" ] ; then
	build_options_default+=" luajit"
fi

if [ ! "$build_option_luajit" ] ; then
	configure_args+=" --disable-luajittex --disable-mfluajit
	 --disable-mfluajit-nowin --disable-luajithbtex"
else
	if [ "${_luajit_host_cc}" = "broken" ] ; then
		nocross="Cannot cross compile LuaJIT for different wordsizes"
	fi
fi

case "$XBPS_TARGET_MACHINE" in
	aarch64*) DASM_ARCH=arm64 ;;
	arm*) DASM_ARCH=arm ;;
	mips64*) DASM_ARCH=mips64 ;;
	mips*) DASM_ARCH=mips ;;
	ppc*) DASM_ARCH=ppc ;;
	i686*) DASM_ARCH=x86 ;;
	x86_64*) DASM_ARCH=x64 ;;
esac
export DASM_ARCH

post_extract() {
	# pdftex poppler compatibility
	cp -pv texk/web2c/pdftexdir/pdftoepdf{-poppler0.76.0,}.cc
	cp -pv texk/web2c/pdftexdir/pdftosrc{-poppler0.76.0,}.cc

	mkdir -p "${build_wrksrc}"
	cp -rf texk/web2c "${build_wrksrc}/web2c-host"
}

post_patch() {
	pushd "${wrksrc}/libs/luajit/LuaJIT-src"

	# Apply all LuaJIT patches first
	for i in ${XBPS_SRCPKGDIR}/LuaJIT/patches/*.patch ; do
		# This patch breaks the build
		if [ "$(basename ${i})" != "get-rid-of-luajit-version-sym.patch" ] ; then
			msg_normal "patching: $(basename ${i})\n"
			patch -sNp0 -i ${i}
		fi
	done

	# Per-architecture patches
	# ppc/ppc64: LuaJIT patches
	local patchdir

	case "$XBPS_TARGET_MACHINE" in
		ppc64*) patchdir="ppc64" ;;
		ppc*) patchdir="ppc" ;;
		*) popd; return ;;
	esac

	for i in ${XBPS_SRCPKGDIR}/LuaJIT/files/patches/${patchdir}/*.patch ; do
		msg_normal "patching: $(basename ${i})\n"
		patch -sNp0 -i ${i}
	done

	popd
}

pre_configure() {
	if [ "$CROSS_BUILD" ] ; then
		# For some reason, when cross-building, the configure script has this path
		# for freetype2 include:
		# /usr/x86_64-linux-musl/usr/x86_64-linux-musl/usr/include/freetype2.
		# It shouldn't have two /usr/x86_64-linux-musl's, add it to CPPFLAGS.
		CPPFLAGS="-I${XBPS_CROSS_BASE}/usr/include/freetype2"
	fi
}

pre_build() {
	if [ -n "$build_option_luajit" ] ; then
		export HOST_CC="${_luajit_host_cc}"
		export HOSTCC="${_luajit_host_cc}"
		if [ -n "$CROSS_BUILD" ] ; then
			# LuaJIT configure script does not cross compile correctly
			local _cflags=$CFLAGS
			local _ldflags=$LDFLAGS
			unset CFLAGS LDFLAGS
			mkdir -p "${wrksrc}/build/libs/luajit/native"
			cd "${wrksrc}/build/libs/luajit/native"
			PREFIX=/usr HOST_LUA=lua5.2 HOST_CFLAGS="$XBPS_CFLAGS" \
			HOST_LDFLAGS="$XBPS_LDFLAGS" TARGET_CFLAGS="${_cflags}" \
			TARGET_LDFLAGS="${_ldflags}" CROSS="${XBPS_CROSS_TRIPLET}-" \
				../../../../libs/luajit/native/configure \
					--build=${XBPS_TRIPLET} \
					--host=${XBPS_CROSS_TRIPLET} \
					--target=${XBPS_CROSS_TRIPLET}
			CFLAGS="${_cflags}"
			LDFLAGS="${_ldflags}"
			# Replace configure script with dummy script
			cd "${wrksrc}"
			cat > libs/luajit/native/configure << EOF
#!/bin/sh
printf "already complete\\n"
EOF
		fi
	fi

	# web2c should not be cross
	mkdir -p "${wrksrc}/build/texk/web2c/web2c"
	cd "${wrksrc}/build/texk/web2c/web2c"
	unset CC CXX
	CFLAGS="$XBPS_CFLAGS" LDFLAGS="$XBPS_LDFLAGS" RANLIB=ranlib \
		../../../../texk/web2c/web2c/configure

	# Replace configure script with dummy script
	cd "${wrksrc}"
	cat > texk/web2c/web2c/configure << EOF
#!/bin/sh
printf "already complete\\n"
EOF
}

post_install() {
	cp -rf ${wrksrc}/texk/tests/TeXLive \
		"${DESTDIR}/usr/share/texmf-dist/scripts/texlive"
	sed -i -e 's:^\(TEXMFROOT *= *\)$SELFAUTOPARENT$:\1/usr/share:g' \
		"${DESTDIR}/usr/share/texmf-dist/web2c/texmf.cnf"
	# Remove files provided by other texlive-* packages
	while IFS=' ' read -r f p ; do
		if [ "$p" = "$pkgname" ] && ! [ -e "${DESTDIR}/usr/share/texmf-dist/${f}" ]; then
			msg_error "$pkgver: missing file $f\n"
		elif [ "$p" != "$pkgname" ] && [ -e "${DESTDIR}/usr/share/texmf-dist/${f}" ]; then
			echo "removed $f"
			mkdir -p ${DESTDIR}/usr/share/texlive/removed
			echo "$f" >> ${DESTDIR}/usr/share/texlive/removed/$pkgname.txt
			rm -f "${DESTDIR}/usr/share/texmf-dist/$f"
		fi
	done < "${XBPS_COMMONDIR}/environment/build-style/texmf/ownership.txt"
	for cmd in amstex cslatex csplain eplain etex jadetex latex mex llatex \
				mltex mllatex pdfetex pdfcslatex pdfcsplain pdfjadetex \
				pdflatex pdfmex pdfxmltex texsis utf8mex xmltex ; do
		ln -s pdftex "${DESTDIR}/usr/bin/${cmd}"
	done
	for cmd in lollipop ; do
		ln -s tex "${DESTDIR}/usr/bin/${cmd}"
	done
	ln -s eptex "${DESTDIR}/usr/bin/platex"
	ln -s euptex "${DESTDIR}/usr/bin/uplatex"
	# Create tlmgr smallscript
	rm -f "${DESTDIR}/usr/bin/tlmgr"
	vbin "${FILESDIR}/tlmgr"
}

texlive-XeTeX_package() {
	depends="${sourcepkg}>=${version}_${revision}"
	short_desc+=" - engine supporting modern typography"
	pkg_install() {
		vmove usr/bin/xetex
		ln -s xetex "${PKGDESTDIR}/usr/bin/xelatex"
	}
}

texlive-dvi_package() {
	depends="${sourcepkg}>=${version}_${revision}"
	short_desc+=" - tools for dvi based workflow"
	pkg_install() {
		for i in afm2tfm bbox epsffit extractres includeres ps2eps psbook psjoin \
				 psnup psresize psselect pstops ; do
			vmove "usr/bin/${i}"
		done
		vmove usr/bin/dvi*
	}
}

texlive-LuaTeX_package() {
	depends="${sourcepkg}>=${version}_${revision} bash"
	short_desc+=" - extended pdfTeX using Lua"
	pkg_install() {
		if [ "$build_option_luajit" ] ; then
			for i in luajittex mfluajit mfluajit-nowin luajithbtex texluajit \
					 texluajitc ; do
				vmove "usr/bin/${i}"
			done
		fi
		for i in checkcites cllualatex cluttex diadia getmapdl l3build luahbtex \
				 luaotfload-tool luatools luatex lwarpmk m-tx \
				 makeglossaries-lite mflua mflua-nowin musixflx musixtex pmxchords \
				 ptex2pdf texlua texluac xindex ; do
			vmove "usr/bin/${i}"
		done
		for i in checkcites cluttex diadia getmap glossaries/makeglossaries-lite.lua \
				 l3build luaotfload lwarp m-tx musixtex pmxchords ptex2pdf xindex ; do
			vmove "usr/share/texmf-dist/scripts/${i}"
		done
		for i in luatex texlua texluac ; do
			vmove "usr/share/man/man1/${i}.1"
		done
		vmove "usr/lib/libtexlua*.so.*"
		for cmd in dvilualatex dviluatex optex ; do
			ln -s luatex "${PKGDESTDIR}/usr/bin/${cmd}"
		done
		ln -s luahbtex "${PKGDESTDIR}/usr/bin/lualatex"
	}
}

texlive-LuaTeX-devel_package() {
	depends="${sourcepkg}-devel>=${version}_${revision}
	 ${sourcepkg}-LuaTeX>=${version}_${revision}"
	short_desc+=" - extended pdfTeX using Lua - development files"
	pkg_install() {
		for i in texlua53 $(vopt_if luajit texluajit); do
			vmove "usr/include/${i}"
			vmove "usr/lib/lib${i}.a"
			vmove "usr/lib/lib${i}.so"
			vmove "usr/lib/pkgconfig/${i}.pc"
		done
	}
}

texlive-Xdvi_package() {
	depends="${sourcepkg}>=${version}_${revision}"
	short_desc+=" - displaying DVI files on the X window system"
	pkg_install() {
		for i in xdvi xdvi-xaw xdvipdfmx ; do
			vmove "usr/bin/${i}"
		done
		vmove usr/share/texmf-dist/xdvi
		for i in xdvi xdvipdfmx ; do
			vmove "usr/share/man/man1/${i}.1"
		done
		vmove usr/share/applications/xdvi.desktop
	}
}

libsynctex_package() {
	short_desc+=" - libsynctex"
	pkg_install() {
		vmove "usr/lib/libsynctex.so.*"
	}
}

texlive-devel_package() {
	depends="${sourcepkg}>=${version}_${revision}"
	short_desc+=" - development files"
	pkg_install() {
		for i in kpathsea ptexenc synctex; do
			vmove "usr/include/${i}"
			vmove "usr/lib/lib${i}.a"
			vmove "usr/lib/lib${i}.so"
			vmove "usr/lib/pkgconfig/${i}.pc"
		done
	}
}

texlive-BibTeX_package() {
	depends="${sourcepkg}>=${version}_${revision} bash"
	short_desc+=" - reference management software"
	pkg_install() {
		for i in bbl2bib ctanbib ; do
			vmove "usr/bin/${i}"
		done
		for i in bib2gls bibexport ; do
			vmove "usr/share/texmf-dist/scripts/${i}"
		done
		vmove usr/bin/bib*
		vmove usr/share/man/man1/bibtex.1
	}
}

texlive-ConTeXt_package() {
	depends="${sourcepkg}>=${version}_${revision} perl ghostscript"
	short_desc+=" - alternative general-purpose document processor"
	pkg_install() {
		for i in context contextjit mptopdf mtxrun mtxrunjit texexec \
				 texmfstart dosepsbin epspdf epspdftk purifyeps repstopdf ; do
			vmove "usr/bin/${i}"
		done
		for i in context dosepsbin epspdf epstopdf ps2eps purifyeps ; do
			vmove "usr/share/texmf-dist/scripts/${i}"
		done
		for i in epsffit ps2eps ; do
			vmove "usr/share/man/man1/${i}.1"
		done
	}
}

texlive-PythonTeX_package() {
	depends="${sourcepkg}>=${version}_${revision} python3"
	short_desc+=" - executing Python in LaTeX"
	pkg_install() {
		vmove usr/bin/*pythontex
		vmove usr/share/texmf-dist/scripts/pythontex
	}
}
