##
##  tetex.spec -- OpenPKG RPM Package Specification
##  Copyright (c) 2000-2025 OpenPKG Project <http://openpkg.org/>
##
##  Permission to use, copy, modify, and distribute this software for
##  any purpose with or without fee is hereby granted, provided that
##  the above copyright notice and this permission notice appear in all
##  copies.
##
##  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
##  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
##  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
##  IN NO EVENT SHALL THE AUTHORS AND COPYRIGHT HOLDERS AND THEIR
##  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
##  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
##  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
##  USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
##  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
##  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
##  OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
##  SUCH DAMAGE.
##

#   package versions
%define       V_base       3.0
%define       V_src        3.0
%define       V_texmf      3.0
%define       V_xmltex     20020625
%define       V_passivetex 20031022

#   package information
Name:         tetex
Summary:      The TeX Typesetting System
URL:          http://tug.org/teTeX/
Vendor:       Thomas Esser et al.
Packager:     OpenPKG Project
Distribution: OpenPKG Community
Class:        BASE
Group:        Typesetting
License:      GPL
Version:      %{V_src}
Release:      20120623

#   list of sources
Source0:      ftp://tug.ctan.org/tex-archive/systems/unix/teTeX/%{V_base}/distrib/tetex-src-%{V_src}.tar.gz
Source1:      ftp://tug.ctan.org/tex-archive/systems/unix/teTeX/%{V_base}/distrib/tetex-texmf-%{V_texmf}.tar.gz
Source2:      http://download.openpkg.org/components/versioned/tetex/passivetex-%{V_passivetex}.zip
Source3:      http://download.openpkg.org/components/versioned/tetex/base-%{V_xmltex}.zip
Patch0:       tetex.patch

#   build information
BuildPreReq:  OpenPKG, openpkg >= 20160101, make, gcc, gcc::with_cxx = yes, infozip, flex, bison
PreReq:       OpenPKG, openpkg >= 20160101, perl
BuildPreReq:  png, zlib
PreReq:       png, zlib

%description
    teTeX is a complete TeX distribution for Unix compatible systems,
    maintained by Thomas Esser. It is based on Karl Berry's original
    Web2c distribution. TeX was invented by Donald E. Knuth, and the
    most popular TeX macro set (LaTeX) for TeX was invented by Leslie
    Lamport. The OpenPKG teTeX package also contains David Carlisle'
    xmltex (namespace-aware XML parser written in TeX) and Sebastian
    Rahtz' PassiveTeX (XSL-FO rendering engine).

%track
    prog tetex:tetex-src = {
        version   = %{V_src}
        url       = ftp://tug.ctan.org/tex-archive/systems/unix/teTeX/
        regex     = ((\d+\.)+\d+)
        url       = ftp://tug.ctan.org/tex-archive/systems/unix/teTeX/__NEWVER__/distrib/
        regex     = tetex-src-(__VER__)\.tar\.gz
    }
    prog tetex:tetex-texmf = {
        version   = %{V_texmf}
        url       = ftp://tug.ctan.org/tex-archive/systems/unix/teTeX/
        regex     = ((\d+\.)+\d+)
        url       = ftp://tug.ctan.org/tex-archive/systems/unix/teTeX/__NEWVER__/distrib/
        regex     = tetex-texmf-(__VER__)\.tar\.gz
    }

%prep
    %setup -q -n tetex-src-%{V_src}
    %patch -p0

    #   teTeX requires the texmf stuff to be already in place
    #   for building and installing the source parts.
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/share/texmf
    %{l_gzip} -d -c %{SOURCE tetex-texmf-%{V_texmf}.tar.gz} |\
        ( umask 0 && cd $RPM_BUILD_ROOT%{l_prefix}/share/texmf && %{l_tar} xf - ) || exit $?

    #   add XMLTeX (XML parsing via TeX)
    ( cd $RPM_BUILD_ROOT%{l_prefix}/share/texmf/tex
      %{l_prefix}/bin/unzip -q -x %{SOURCE base-%{V_xmltex}.zip}
      mv base xmltex
    ) || exit $?
    ( echo ""
      echo "# XMLTeX (XML parsing via TeX) [does not work as expected here]"
      echo "xmltex    latex    language.dat xmltex.ini"
      echo "pdfxmltex pdflatex language.dat pdfxmltex.ini"
      echo ""
    ) >>texk/web2c/fmtutil.in
    ( echo ""
      echo "% XMLTeX (XML parsing via TeX)"
      echo "main_memory.xmltex = 2500000"
      echo "main_memory.pdfxmltex = 2500000"
      echo "param_size.xmltex = 1500"
      echo "param_size.pdfxmltex = 1500"
      echo "stack_size.xmltex = 1500"
      echo "stack_size.pdfxmltex = 1500"
      echo "hash_extra.xmltex = 50000"
      echo "hash_extra.pdfxmltex = 50000"
      echo "string_vacancies.xmltex = 45000"
      echo "string_vacancies.pdfxmltex = 45000"
      echo "pool_free.xmltex = 47500"
      echo "pool_free.pdfxmltex = 47500"
      echo "nest_size.xmltex = 500"
      echo "nest_size.pdfxmltex = 500"
      echo "save_size.xmltex = 10000"
      echo "save_size.pdfxmltex = 10000"
      echo "pool_size.xmltex = 500000"
      echo "pool_size.pdfxmltex = 500000"
      echo "max_strings.xmltex = 55000"
      echo "max_strings.pdfxmltex = 55000"
      echo "buf_size.xmltex = 200000"
      echo "buf_size.pdfxmltex = 200000"
      echo ""
    ) >>texk/kpathsea/texmf.in

    #   add PassiveTeX (XSL-FO to DVI/PDF translation via TeX)
    ( %{l_shtool} mkdir -f -p -m 755 \
         $RPM_BUILD_ROOT%{l_prefix}/share/texmf/tex/latex/passivetex
      cd $RPM_BUILD_ROOT%{l_prefix}/share/texmf/tex/latex/passivetex
      %{l_prefix}/bin/unzip -q -x %{SOURCE passivetex-%{V_passivetex}.zip}
      rm -f ChangeLog Makefile README.passivetex send
      rm -rf test
    ) || exit $?

    #   minor fixes to the teTeX sources
    %{l_shtool} subst \
        -e '1s;/usr/bin/perl;/usr/bin/env perl;' \
        texk/*/t1mapper
    %{l_shtool} subst \
        -e "s;\(\$(scriptdir)/fmtutil-sys --all\);\1 --fmtdir $RPM_BUILD_ROOT%{l_prefix}/share/texmf/web2c;" \
        -e "s,\(\$(scriptdir)/updmap-sys\);,\1 --dvipsoutputdir $RPM_BUILD_ROOT%{l_prefix}/share/texmf/fonts/map/dvips/updmap --pdftexoutputdir $RPM_BUILD_ROOT%{l_prefix}/share/texmf/fonts/map/dvips/updmap --dvipdfmoutputdir $RPM_BUILD_ROOT%{l_prefix}/share/texmf/fonts/map/dvips/updmap;," \
        Makefile.in

%build
    #   configure the system by using the (wrong) paths to the temporary
    #   installation area, followed by by a step for fixing the paths
    #   (to the correct values) inside the kpathsea library (which
    #   performs the run-time searches). This way we get all the magic
    #   of the teTeX build system without problems, but still make sure
    #   that the built-in paths are correct.
    %{l_shtool} subst -s -v \
        -e 's;^vartexfonts =.*;vartexfonts = %{l_prefix}/var/tetex/fonts;' \
        texk/make/paths.mk
    CC="%{l_cc}" \
    CXX="%{l_cxx}" \
    CFLAGS="%{l_cflags -O}" \
    CXXFLAGS="%{l_cxxflags -O}" \
    CPPFLAGS="%{l_cppflags}" \
    LEX="%{l_prefix}/bin/flex" \
    ./configure \
        --prefix=$RPM_BUILD_ROOT%{l_prefix} \
        --datadir=$RPM_BUILD_ROOT%{l_prefix}/share \
        --without-xdvik \
        --without-oxdvik \
        --without-x \
        --without-x11 \
        --without-x11win \
        --without-texinfo \
        --disable-multiplatform \
        --enable-gf \
        --with-system-pnglib \
        --with-pnglib-libdir=%{l_prefix}/lib \
        --with-pnglib-include=%{l_prefix}/include \
        --with-system-zlib \
        --with-zlib-libdir=%{l_prefix}/lib \
        --with-zlib-include=%{l_prefix}/include
    ( cd texk/kpathsea
      %{l_shtool} subst -v \
          -e "s;$RPM_BUILD_ROOT%{l_prefix};%{l_prefix};" \
          `find . -type f -print`
      eval "make all"
    ) || exit $?

    #   build the sources
    #   (we cannot use %{l_make}, because teTeX dislikes absolute paths
    #   for make. Instead we work-around via $PATH. The redundant
    #   "eval" around the call is just to shut-up speclint)
    eval "make all"

%install
    #   reinstall teTeX texmf stuff
    #   (because RPM 5 implicitly removes $RPM_BUILD_ROOT at begin of %install)
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/share/texmf
    %{l_gzip} -d -c %{SOURCE tetex-texmf-%{V_texmf}.tar.gz} |\
        ( umask 0 && cd $RPM_BUILD_ROOT%{l_prefix}/share/texmf && %{l_tar} xf - ) || exit $?

    #   perform the standard installation procdure of teTeX
    #   (we cannot use %{l_make}, because teTeX dislikes absolute paths
    #   for make. Instead we work-around via $PATH. The redundant
    #   "eval" around the call is just to shut-up speclint)
    eval "make install"

    #   create dynamically generated font directory
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/var/tetex
    %{l_shtool} mkdir -f -p -m 777 \
        $RPM_BUILD_ROOT%{l_prefix}/var/tetex/fonts

    #   cleanup the installation
    rm -f $RPM_BUILD_ROOT%{l_prefix}/info/dir
    strip $RPM_BUILD_ROOT%{l_prefix}/bin/* >/dev/null 2>&1 || true

    #   determine installation files
    %{l_rpmtool} files -v -ofiles -r$RPM_BUILD_ROOT \
        %{l_files_std} \
        '%config %{l_prefix}/share/texmf/dvipdfm/config/config' \
        '%config %{l_prefix}/share/texmf/dvips/config/config.ps' \
        '%config %{l_prefix}/share/texmf/metafont/misc/modes.mf' \
        '%config %{l_prefix}/share/texmf/web2c/mktex.cnf'

%files -f files

%clean

