#!/bin/sh
# Compile teTeX for Slackware.
# Build script by Patrick Volkerding <volkerdi@slackware.com>
TMP=${TMP:-/tmp}
CWD=`pwd`

VERSION=3.0
ARCH=${ARCH:-i486}
BUILD=${BUILD:-5}

if [ "$ARCH" = "i386" ]; then
  SLKCFLAGS="-O2 -march=i386 -mcpu=i686"
elif [ "$ARCH" = "i486" ]; then
  SLKCFLAGS="-O2 -march=i486 -mtune=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

# Safer to purge this first:
rm -rf /usr/share/texmf
rm -rf /usr/share/texmf-var
rm -rf /usr/share/texi2html

# Make a clean new directory:
mkdir -p /usr/share/texmf
( cd /usr/share/texmf 
  tar xjvf $CWD/tetex-texmf-$VERSION.tar.bz2
  chown -R root:root .
  find . -type f -perm 666 -exec chmod 644 {} \;
)
cd $TMP
rm -rf tetex-src-$VERSION
tar xjvf $CWD/tetex-src-$VERSION.tar.bz2
cd tetex-src-$VERSION
chown -R root:root .
find . -type f -perm 666 -exec chmod 644 {} \;

# Fix possible xpdf overflows:
zcat $CWD/tetex.CVE-2005-3193.diff.gz | patch -p1 --verbose || exit 1

# /var/lib/texmf is a better font location than /var/tmp/texfonts:
zcat $CWD/tetex.var.fonts.diff.gz | patch -p1 --verbose || exit 1

# Even better is something in your $HOME directory:
zcat $CWD/tetex.fontcachedirinhome.diff.gz | patch -p1 --verbose || exit 1

# Use mkstemp() in dvipdfm:
zcat $CWD/tetex.dvipdfm.mkstemp.diff.gz | patch -p1 --verbose || exit 1

# Use mktemp in scripts:
zcat $CWD/tetex.scripts.mktemp.diff.gz | patch -p1 --verbose || exit 1

# Try to use more modern browsers.
# Attempt to use them in this order:  firefox, seamonkey, mozilla, links, lynx.
zcat $CWD/tetex.browsers.diff.gz | patch -p1 --verbose || exit 1

# Add bplain and bamstex info to fmtutil.cnf:
zcat $CWD/tetex.amstex.fmtutil.diff.gz | patch -p1 --verbose || exit 1

# Don't leave texconfig junk in $TMP:
zcat $CWD/tetex.tmp.cleanup.diff.gz | patch -p1 --verbose || exit 1

CFLAGS="$SLKCFLAGS" \
CXXFLAGS="$SLKCFLAGS" \
./configure --prefix=/usr/share/texmf \
   --datadir=/usr/share \
   --disable-multiplatform \
   --disable-a4 \
   --without-dialog \
   --without-texinfo \
   --with-system-ncurses \
   --with-system-pnglib \
   --with-system-t1lib \
   --with-system-tifflib \
   --with-system-zlib \
   --with-ps=gs \
   --enable-ipc \
   --enable-freetype \
   --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".
# It does, in fact, fix a bug where the etex binary/engine really should be
# used when etex and dvi output are requested, but the example given to me
# (which was "make dvi" in GNU binutils) also has some problems that have to
# be fixed.  This confused me for a good long time.  ;-)
# BTW, the fix for the binutils docs is to edit bfd.texinfo and remove that
# @tex through @end tex section near the top.  Then build twice.  Hit enter
# if it "sticks".
zcat $CWD/tetex.etex.diff.gz | patch -p1 --verbose || exit 1

make all
make install strip

if ! echo $PATH | grep /usr/share/texmf/bin ; then
  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 paper us

# These *might* be useful...
# Some are known to be not-so-useful, though, as they put the config files
# into /root/.texmf-config, which we're simply going to throw away anyway
# so it won't interfere with server testing.  If you need these options,
# you'll probably have to run texconfig yourself in your own user (or root)
# account.
texconfig mode ljfour
#texconfig xdvi paper 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 ro
texconfig rehash

# Add jadetex to the package:
. $CWD/jadetex.build

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

# Discard local configurations:
if [ -e /root/.texmf-config ]; then
  mv /root/.texmf-config /root/.texmf-config-$(mcookie)
  echo "/root/.texmf-config moved out of the way...  you may wish"
  echo "to purge any /root/.texmf-config-* directories if the"
  echo "cruft heap gets too large."
fi

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

PKG1=$TMP/package-tetex
PKG2=$TMP/package-tetex-doc
rm -rf $PKG1 $PKG2
mkdir -p $PKG1 $PKG2

mkdir -p $PKG1/usr/share
mv /usr/share/texmf $PKG1/usr/share
mv /usr/share/texmf-var $PKG1/usr/share
mv /usr/share/texi2html $PKG1/usr/share

# 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 $PKG1/etc/profile.d
cp -a $CWD/profile.d/tetex.* $PKG1/etc/profile.d
chown root:root $PKG1/etc/profile.d/tetex.*
chmod 755 $PKG1/etc/profile.d/tetex.*

mkdir -p $PKG1/install
cp $CWD/slack-desc.tetex $PKG1/install/slack-desc
mkdir -p $PKG2/install
cp $CWD/slack-desc.tetex-doc $PKG2/install/slack-desc

# Strip everything:
( cd $PKG1
  find . | xargs file | grep "executable" | grep ELF | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null
  find . | xargs file | grep "shared object" | grep ELF | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null
)

# Gzip man pages:
gzip -9 $PKG1/usr/share/texmf/man/man?/*.?

# Fix manpage symlinks:
( cd $PKG1/usr/share/texmf/man
  for dir in man? ; do
    ( cd $dir
      for file in *.? ; do
        if [ -L $file ]; then
          LINK=$(readlink $file)
          rm $file
          ln -sf ${LINK}.gz ${file}.gz
        fi
      done
    )
  done
)

# Move some of the files into the docs package:
mkdir -p $PKG2/usr/share/texmf/doc
mv $PKG1/usr/share/texmf/doc/* $PKG2/usr/share/texmf/doc
mkdir -p $PKG2/usr/share/texinfo
mv /usr/share/texinfo/html $PKG2/usr/share/texinfo
mv $PKG1/usr/share/texmf/info $PKG2/usr
( cd $PKG1/usr/share/texmf ; ln -sf /usr/info . )

# Compress info pages and purge "dir" file from the package:
if [ -d $PKG2/usr/info ]; then
  ( cd $PKG2/usr/info
    rm -f dir
    gzip -9 *
  )
fi

# Make docs link in /usr/doc/:
mkdir -p $PKG2/usr/doc
( cd $PKG2/usr/doc ; rm -rf tetex-$VERSION )
( cd $PKG2/usr/doc ; ln -sf /usr/share/texmf/doc tetex-$VERSION )

# Build the packages:
cd $PKG1
makepkg -l y -c n ../tetex-$VERSION-$ARCH-$BUILD.tgz
cd $PKG2
makepkg -l y -c n ../tetex-doc-$VERSION-$ARCH-$BUILD.tgz