#!/bin/sh
# Compile teTeX for Slackware.
# Build script by Patrick Volkerding <volkerdi@slackware.com>
CWD=`pwd`
VERSION=3.0

if [ "$ARCH" = "i386" ]; then
  SLKCFLAGS="-O2 -march=i386 -mcpu=i686"
elif [ "$ARCH" = "i486" ]; then
  SLKCFLAGS="-O2 -march=i486 -mcpu=i686"
elif [ "$ARCH" = "s390" ]; then
  SLKCFLAGS="-O2"
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2"
fi

# There were so many local security problems with this shared fontcache
# scheme that it's not worth trying to support.  It was not enabled by
# default, and newer versions of teTeX don't seem to recongnize it any
# longer either.
#export fontcache=/var/cache/fonts
#export vartexfonts=$fontcache
#mkdir -p /var/cache/fonts/pk /var/cache/fonts/source /var/cache/fonts/tfm
#chmod 1777 /var/cache/fonts /var/cache/fonts/pk \
#  /var/cache/fonts/source /var/cache/fonts/tfm

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 * )
cd /tmp
rm -rf tetex-$VERSION
tar xjvf $CWD/tetex-src-$VERSION.tar.bz2
# Make docs link in /usr/doc/:
( cd /usr/doc ; rm -rf tetex-$VERSION )
( cd /usr/doc ; ln -sf /usr/share/texmf/doc tetex-$VERSION )
cd tetex-src-$VERSION
CFLAGS="$SLKCFLAGS" \
CXXFLAGS="$SLKCFLAGS" \
./configure --prefix=/usr/share/texmf \
   --datadir=/usr/share \
   --disable-multiplatform \
   --without-dialog \
   --without-texinfo \
   --with-system-ncurses \
   --with-system-pnglib \
   --with-system-t1lib \
   --with-system-tifflib \
   --with-system-zlib \
   --with-ps=gs \
   --enable-ipc \
   --with-etex
#   --without-texinfo  # Really, it doesn't hurt to include these twice...

# This was reported to fix a problem with newer GNU sources and "make dvi"
# but it didn't help here.
#
#zcat $CWD/tetex.etex.diff.gz | patch -p1 --verbose || exit
#

make all
make install strip
if echo $PATH | fgrep /usr/share/texmf/bin ; then
  true
else
  PATH=$PATH:/usr/share/texmf/bin
fi
# ... [don't] make links to all binaries in /usr/bin for the final package.
#     [that would be really pointless... $PATH should work, period. ]

# 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
)

# Now, package the /usr/share/texmf/ tree.  Package tetex-doc.tgz and
# tetex-bin.tgz seperately.

# Exactly *how* to package it is left as an exercise for the reader. :)
# Hint:  'man makepkg'

mkdir -p /install
cp $CWD/slack-desc* /install