##
##  tetex.spec -- Portable RPM Specification
##  Copyright (c) 2000-2003 Cable & Wireless Deutschland GmbH
##  Copyright (c) 2000-2003 The OpenPKG Project <http://www.openpkg.org/>
##  Copyright (c) 2000-2003 Ralf S. Engelschall <rse@engelschall.com>
##
##  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    1.0
%define       V_src     1.0.7
%define       V_texmf   1.0.2

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

#   package options
%option       with_x11  %{l_test -p x11}

#   list of sources
Source0:      ftp://sunsite.informatik.rwth-aachen.de/pub/comp/tex/teTeX/1.0/distrib/sources/teTeX-src-%{V_src}.tar.gz
Source1:      ftp://sunsite.informatik.rwth-aachen.de/pub/comp/tex/teTeX/1.0/distrib/sources/teTeX-texmf-%{V_texmf}.tar.gz
Source2:      http://www.tei-c.org.uk/Software/passivetex/passivetex.zip
Source3:      ftp://ftp.tex.ac.uk/tex-archive/macros/xmltex/base.zip
Patch0:       tetex.patch

#   build information
Prefix:       %{l_prefix}
BuildRoot:    %{l_buildroot}
BuildPreReq:  OpenPKG, openpkg >= 20030103, make, infozip, flex, bison
PreReq:       OpenPKG, openpkg >= 20030103
%if "%{with_x11}" == "yes"
BuildPreReq:  X11
PreReq:       X11
%endif
AutoReq:      no
AutoReqProv:  no

%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 (XML-FO rendering engine).

%prep
    %setup -q -n teTeX-%{V_base}
    %patch -p0

    #   teTeX requires the texmf stuff to be already in place
    #   for building and installing the source parts.
    rm -rf $RPM_BUILD_ROOT
    %{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 -)

    #   add XMLTeX (XML parsing via TeX)
    ( cd $RPM_BUILD_ROOT%{l_prefix}/share/texmf/tex
      %{l_prefix}/bin/unzip -x %{SOURCE base.zip}
      mv base xmltex
    )
    ( 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 (XML-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 -x %{SOURCE passivetex.zip}
      rm -f ChangeLog Makefile README.passivetex send
      rm -rf test
    )

    #   minor fixes to the teTeX sources
    %{l_shtool} subst -e "s;\|O_SYNC;;g" libs/libwww/HTMemLog.c
    %{l_shtool} subst -e "s;strnstr;my_strnstr;g" libs/libwww/HTReader.c

%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}" \
    ./configure \
        --prefix=$RPM_BUILD_ROOT%{l_prefix} \
        --datadir=$RPM_BUILD_ROOT%{l_prefix}/share \
%if "%{with_x11}" == "yes"
        --with-xdvik \
        --with-oxdvik \
        --with-x \
        --with-x11 \
        --with-x11win \
        --x-includes=`%{l_prefix}/etc/rc --query x11_incdir` \
        --x-libraries=`%{l_prefix}/etc/rc --query x11_libdir` \
%else
        --without-xdvik \
        --without-oxdvik \
        --without-x \
        --without-x11 \
        --without-x11win \
%endif
        --disable-multiplatform \
        --enable-gf
    ( cd texk/kpathsea
      %{l_shtool} subst -v \
          -e "s;$RPM_BUILD_ROOT%{l_prefix};%{l_prefix};" \
          `find . -type f -print`
      make %{l_mflags} all
    )

    #   build the sources
    #   (we cannot use %{l_make}, because teTeX dislikes absolute
    #    paths for make. Instead we work-around via $PATH)
    make %{l_mflags} all

%install
    #   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)
    make %{l_mflags} install

    #   perform manual steps for installing XMLTeX
    ( TEXMFMAIN="$RPM_BUILD_ROOT%{l_prefix}/share/texmf"
      export TEXMFMAIN
      PATH="$RPM_BUILD_ROOT%{l_prefix}/bin:$PATH"
      cd $RPM_BUILD_ROOT%{l_prefix}/share/texmf/web2c
      $RPM_BUILD_ROOT%{l_prefix}/bin/tex -ini "&latex" xmltex.ini
      $RPM_BUILD_ROOT%{l_prefix}/bin/pdftex -ini "&pdflatex" pdfxmltex.ini
	  $RPM_BUILD_ROOT%{l_prefix}/bin/mktexlsr
      ( cd $RPM_BUILD_ROOT%{l_prefix}/bin
        ln -s tex xmltex
        ln -s pdftex pdfxmltex
      )
    )

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

    #   adjust paths in manual pages
    %{l_shtool} subst -s -v \
        -e "s;$RPM_BUILD_ROOT%{l_prefix};%{l_prefix};g" \
        $RPM_BUILD_ROOT%{l_prefix}/man/man1/*.1

    #   cleanup the installation
    rm -f $RPM_BUILD_ROOT%{l_prefix}/bin/install-info
    rm -f $RPM_BUILD_ROOT%{l_prefix}/bin/makeinfo
    rm -f $RPM_BUILD_ROOT%{l_prefix}/bin/texi2dvi
    rm -f $RPM_BUILD_ROOT%{l_prefix}/bin/texindex
    rm -f $RPM_BUILD_ROOT%{l_prefix}/info/dir
    rm -f $RPM_BUILD_ROOT%{l_prefix}/info/info*
    rm -f $RPM_BUILD_ROOT%{l_prefix}/info/texinfo*

    #   determine installation files
    %{l_rpmtool} files -v -ofiles -r$RPM_BUILD_ROOT %{l_files_std}

%files -f files

%clean
    rm -rf $RPM_BUILD_ROOT

