################################################################################
#
# texlive.cygclass - functions for building TeX Live packages
#
# Part of cygport - Cygwin packaging application
# Copyright (C) 2006-2016 Yaakov Selkowitz
# Provided by the Cygwin Ports project <http://sourceware.org/cygwinports/>
#
# cygport is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# cygport is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with cygport.  If not, see <http://www.gnu.org/licenses/>.
#
################################################################################

#****h* Cygclasses/texlive.cygclass
#  DESCRIPTION
#  TeX is an extensible typesetting system used to produce documentation
#  in many formats.  TeX Live is the primary TeX distribution for *NIX systems,
#  replacing teTeX when the latter became unsupported in 2006.
#
#  TeX Live packages, which total in the thousands, are distributed individually
#  via CTAN and are updated on a continuous basis.
#
#  This cygclass creates Cygwin packages from one or more TeX Live packages.
#  SEE ALSO
#  mirror_ctan
#****

#****v* texlive.cygclass/TEXLIVE_TEXMF_PKGS
#  DESCRIPTION
#  A list of one or more TeX Live arch-independent packages consisting of TeX
#  macros, fonts, language support, and other files which are not documentation.
#****
#****v* texlive.cygclass/TEXLIVE_ARCH_PKGS
#  DESCRIPTION
#  A list of one or more TeX Live arch-specific packages, consisting of scripts
#  and symlinks which are to be installed into the system PATH.
#  NOTE
#  All arch-specific packages also provide an arch-independent component, which
#  will be included and need not be listed separately in TEXLIVE_TEXMF_PKGS.
#****
#****v* texlive.cygclass/TEXLIVE_DOC_PKGS
#  DESCRIPTION
#  A list of one or more TeX Live documentation packages.
#****
#****v* texlive.cygclass/TEXLIVE_PRETEST
#  DESCRIPTION
#  Define if packages should be downloaded from a pretest package repository
#  mirror instead of from stable release repository mirrors, which are frozen
#  during the pretest phase.
#****
if ! defined TEXLIVE_TEXMF_PKGS && ! defined TEXLIVE_ARCH_PKGS && ! defined TEXLIVE_DOC_PKGS
then
	TEXLIVE_TEXMF_PKGS=${PN#texlive-}
fi

#****o* texlive.cygclass/CATEGORY (texlive)
#  DEFINITION
CATEGORY="Publishing"
#****
#****o* texlive.cygclass/SUMMARY (texlive)
#  DESCRIPTION
#  The default SUMMARY depends on the value of PN.
#****
case ${PN} in
*-doc)
	SUMMARY="Documentation for ${PN%-doc}" ;;
texlive-collection-documentation-*)
	SUMMARY="TeX Live ${PN##*-} documentation" ;;
texlive-collection-lang*)
	SUMMARY="TeX Live ${PN#*-lang} language support" ;;
texlive-collection-*|texlive-scheme-*)
	SUMMARY="TeX Live ${PN##*-} package collection" ;;
*)	SUMMARY="TeX Live ${PN#texlive-} package(s)" ;;
esac

#****o* texlive.cygclass/DESCRIPTION (texlive)
#  DEFINITION
DESCRIPTION="TeX Live is an easy way to get up and running with the TeX
document production system.  It includes all the major TeX-related
programs, macro packages, and fonts that are free software, including
support for many languages around the world."
#****
#****o* texlive.cygclass/HOMEPAGE (texlive)
#  DEFINITION
HOMEPAGE="http://www.tug.org/texlive/"
#****

case ${ARCH} in
i686)	TEXLIVE_ARCH="i386-cygwin" ;;
x86_64)	TEXLIVE_ARCH="x86_64-cygwin" ;;
esac

#****o* texlive.cygclass/SRC_URI (texlive)
#  DESCRIPTION
#  SRC_URI is constructed automatically based on the value of TEXLIVE_TEXMF_PKGS,
#  TEXLIVE_ARCH_PKGS, TEXLIVE_DOC_PKGS, and TEXLIVE_PRETEST.
#****
if defined TEXLIVE_PRETEST
then
	tlnet="mirror://tlpretest"
else
	tlnet="mirror://ctan/systems/texlive/tlnet"
fi
for pkg in ${TEXLIVE_TEXMF_PKGS//,/ }
do
	SRC_URI+=" ${tlnet}/archive/$pkg.tar.xz"
done
for pkg in ${TEXLIVE_ARCH_PKGS//,/ }
do
	SRC_URI+=" ${tlnet}/archive/$pkg.tar.xz"
	SRC_URI+=" ${tlnet}/archive/$pkg.${TEXLIVE_ARCH}.tar.xz"
done
for pkg in ${TEXLIVE_DOC_PKGS//,/ }
do
	SRC_URI+=" ${tlnet}/archive/$pkg.doc.tar.xz"
done
unset pkg tlnet

SRC_DIR="."


#****I* texlive.cygclass/texlive_install
#  SYNOPSIS
#  texlive_install
#  DESCRIPTION
#  Installs the TeX Live package file(s) into their respective locations and
#  queues the necessary postinstall steps.
#****
texlive_install() {
	local f pkg pkgs tlpobj dst src fmt map perm

	__config_set texlive_fmts
	__config_set texlive_maps

	for pkg in ${TEXLIVE_TEXMF_PKGS//,/ }
	do
		pkgs+="$pkg "
	done
	for pkg in ${TEXLIVE_ARCH_PKGS//,/ }
	do
		pkgs+="$pkg $pkg.${TEXLIVE_ARCH} "
	done
	for pkg in ${TEXLIVE_DOC_PKGS//,/ }
	do
		pkgs+="$pkg.doc "
	done

	for pkg in $pkgs
	do
		tlpobj="tlpkg/tlpobj/$pkg.tlpobj"
		for f in $(grep '^ ' $tlpobj)
		do
			case $f in
			*.exe|*.dll|*.mem)
				# compiled from source in the 'texlive' package
				continue ;;
			*.bat)
				# only needed for native Win32
				continue ;;
			bin/${TEXLIVE_ARCH}/*)
				if [ ! -L ${f} ]
				then
					dobin $f
				else
					dst=$(readlink $f)
					dodir /usr/bin
					case ${dst} in
					\.\./*) dosym /usr/share/${dst#../../} /usr/bin/${f##*/} ;;
					*) dosym ${dst} /usr/bin/${f##*/} ;;
					esac
				fi
				continue ;;
			*doc/info/dir)
				# system-managed file
				continue ;;
			*doc/info/*.info)
				doinfo ${f#RELOC/}
				continue ;;
			*doc/man/man[15]/*.[15])
				doman ${f#RELOC/}
				continue ;;
			RELOC/*)
				src=${f#RELOC/}; dst=texmf-dist/${src} ;;
			texmf/*|texmf-dist/*|tlpkg/TeXLive/*) # used by updmap
				src=$f; dst=$f ;;
			*)	# we don't use tlmgr, so tlpkg files aren't used
				continue ;;
			esac

			if [ "$(head -c 2 $src)" = '#!' -o "$(head -c 4 $src)" = 'eval' ]
			then
				perm=755
			else
				perm=644
			fi

			dodir /usr/share/${dst%/*}
			__doinstall ${perm} $src /usr/share/${dst%/*}
		done

		for fmt in $(grep -E 'execute AddFormat' $tlpobj \
		             | sed '/mode=disabled/d;s|.* name=\([^ ]*\).* engine=\([^ ]*\).*|\1/\2|')
		do
			__config_set texlive_fmts "$(__config_get texlive_fmts) $fmt"
		done

		for map in $(grep -E 'execute add[^ ]*Map' $tlpobj \
		             | sed 's/execute add\([^ ]*\) */\1=/g')
		do
			__config_set texlive_maps "$(__config_get texlive_maps) $map"
		done
	done
}

#****o* texlive.cygclass/src_compile (texlive)
#  DEFINITION
src_compile() { :; }
#****

#****o* texlive.cygclass/src_install (texlive)
#  DEFINITION
src_install() {
	cd ${S}
	texlive_install
}
#****

readonly -f texlive_install
