#!/bin/bash

# tetex.build
# Based on the original build script by Patrick Volkerding <volkerdi@slackware.com>
# Modified by Stuart Winter for ARMedslack.

VERSION=$1

# Extract source:
cd $SLACKTRACKSCRATCHDIR
tar jxvvf $CWD/tetex-src-$VERSION.tar.bz2
cd tetex-src-$VERSION
slackhousekeeping

mkdir -p /usr/share/texmf
( cd /usr/share/texmf ; umask 000 ; tar xjvf $CWD/tetex-texmf-$VERSION.tar.bz2 )
( cd /usr/share/texmf ; chown -R root:root * )

# Make docs link in /usr/doc/.  The /usr/share/texmf/doc ends up in its own
# package (tetex-doc) and is removed from the main tetex package:
( cd /usr/doc ; rm -rf tetex-$VERSION )
( cd /usr/doc ; ln -sf /usr/share/texmf/doc tetex-$VERSION )

# Configure:
CFLAGS="$SLKCFLAGS" \
CXXFLAGS="$SLKCFLAGS" \
./configure --prefix=/usr/share/texmf \
   --datadir=/usr/share \
   --disable-multiplatform \
   --without-dialog \
   --with-system-ncurses \
   --with-system-pnglib \
   --with-system-t1lib \
   --with-system-tifflib \
   --with-system-zlib \
   --with-ps=gs \
   --enable-ipc \
   --with-etex || failconfig

# Build:
make all || failmake

# Install:
make install strip || failinstall

# Include the location of the new tetex binaries in our paths - we need them
# to continue building the package contents:
if echo $PATH | fgrep /usr/share/texmf/bin ; then
  true
else
  PATH=$PATH:/usr/share/texmf/bin
fi

# If latex isn't there, maybe we need to do this:
if [ ! -r /usr/share/texmf/bin/latex ]; then
  texconfig init
fi

# Let's start with these defaults:
texconfig confall
texconfig dvips paper letter
texconfig xdvi us

# These *might* be useful...
texconfig mode ljfour
#texconfig xdvi a4
texconfig dvips mode ljfour
#texconfig dvips paper A4
texconfig dvips printcmd -
texconfig dvips add bjc600
texconfig dvips add stcolor
texconfig dvips add deskjet
texconfig dvips add ljet4
texconfig dvips -P bjc600  mode canonbjc
texconfig dvips -P stcolor mode epstylus
texconfig dvips -P deskjet mode deskjet
texconfig dvips -P ljet4   mode ljfour
texconfig font options appendonlydir varfonts
texconfig font ro
texconfig rehash

# The tetex.tgz file will need an installation script to add
# /usr/share/texmf/bin to the $PATH and /usr/share/texmf/man to the $MANPATH:
mkdir -p /etc/profile.d
cp -a $CWD/profile.d/tetex.* /etc/profile.d
chown root:root /etc/profile.d/tetex.*
chmod 755 /etc/profile.d/tetex.*

# Toss redundant docs:
( cd /usr/share/texmf/doc
  for file in `find . -name "*.dvi"` ; do
    rm -f `dirname $file`/`basename $file .dvi`.pdf
  done )

# Build the tetex-doc package (we create a temporary package directory
# inside slacktrack's scratch space).
( mkdir -p ${SLACKTRACKSCRATCHDIR}/package-tetex-doc/usr/share/texmf
  cd ${SLACKTRACKSCRATCHDIR}/package-tetex-doc
  mv /usr/share/texmf/doc usr/share/texmf
  mkdir install
  install -m644 $CWD/slack-desc.tetex-doc install/slack-desc
  chown -R root:root .
  find . -type f -print0 | xargs -0 chmod 644 
  find . -type d -print0 | xargs -0 chmod 755
  makepkg -l y -c n $PKGSTORE/$PKGSERIES/$PACKAGE-doc-$VERSION-noarch-$BUILD.tgz 
  # And we don't include the docs in the main package so let's zap 'em!	
  rm -rf /usr/share/texmf/doc )

# Install the package description for the main package:
mkdir /install
install -m644 $CWD/slack-desc.tetex /install/slack-desc
