#!/bin/sh

# TexLive build script for Slackware

# Copyright 2009  Robby Workman, Northport, AL, USA
# Copyright 2009  Patrick J. Volkerding, Sebeka, MN, USA
# All rights reserved.
#
# Redistribution and use of this script, with or without modification, is
# permitted provided that the following conditions are met:
#
# 1. Redistributions of this script must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#
#  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ''AS IS'' AND ANY EXPRESS 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 AUTHOR 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.

# URL: ftp://tug.org/texlive/historic/2008/
# You only need these two files:
#   texlive-20080816-source.tar.bz2
#   texlive-20080822-texmf.tar.bz2
# I uncompressed them with lzma and then recompressed them with bzip2

VERSION=20080816
TEXMF_VERSION=20080822
ARCH=${ARCH:-i486}
BUILD=${BUILD:-1}

CWD=$(pwd)
TMP=${TMP:-/tmp}
PKG=$TMP/package-texlive-main
PKG_TEXDOCS=$TMP/package-texlive-docs
PKG_TEXMF=$TMP/package-texlive-texmf

if [ "$ARCH" = "i486" ]; then
  SLKCFLAGS="-O2 -march=i486 -mtune=i686"
elif [ "$ARCH" = "i686" ]; then
  SLKCFLAGS="-O2 -march=i686 -mtune=i686"
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2 -fPIC"
fi

rm -rf $PKG $PKG_TEXDOCS
mkdir -p $TMP $PKG $PKG_TEXDOCS $PKG_TEXMF
cd $TMP
rm -rf texlive-$VERSION-source
tar xvf $CWD/texlive-$VERSION-source.tar.?z* || exit 1
cd texlive-$VERSION-source
chown -R root:root .
chmod -R a-s .
find . \
 \( -perm 777 -o -perm 775 -o -perm 711 -o -perm 555 -o -perm 511 \) \
 -exec chmod 755 {} \; -o \
 \( -perm 666 -o -perm 664 -o -perm 600 -o -perm 444 -o -perm 440 -o -perm 400 \) \
 -exec chmod 644 {} \;

# Building --without-{omega,aleph} because they're allegedly unmaintained
# and slated for removal at some point
TL_BUILD_ENV='CFLAGS="$SLKCFLAGS" CXXFLAGS="$SLKCFLAGS"' \
TL_CONFIGURE_ARGS="\
  --enable-ipc \
  --with-x \
  --without-omega \
  --without-aleph \
  --with-system-ncurses \
  --with-system-zlib \
  --with-system-pnglib \
  --with-system-t1lib \
  --with-system-gd \
  --with-system-freetype2 \
  --build=$ARCH-slackware-linux" \
./Build

mkdir -p $PKG/usr/share/texmf/bin
mv inst/bin/$ARCH-slackware-linux-gnu/* $PKG/usr/share/texmf/bin

# Install the README file while we're still here
mkdir -p $PKG/usr/doc/texlive-$VERSION
cp -a README \
  $PKG/usr/doc/texlive-$VERSION

cd $TMP
tar xf $CWD/texlive-$TEXMF_VERSION-texmf.tar.?z* || exit 1
cd texlive-$TEXMF_VERSION-texmf
chown -R root:root .
chmod -R a-s .
find . \
 \( -perm 777 -o -perm 775 -o -perm 711 -o -perm 555 -o -perm 511 \) \
 -exec chmod 755 {} \; -o \
 \( -perm 666 -o -perm 664 -o -perm 600 -o -perm 444 -o -perm 440 -o -perm 400 \) \
 -exec chmod 644 {} \;

# Move the docs to $PKG_TEXDOCS
# Perhaps consider shipping only english docs??
mkdir -p $PKG_TEXDOCS/usr/share/{texmf,texmf-dist}
rm -rf texmf-doc/source
mv texmf-doc $PKG_TEXDOCS/usr/share
mv texmf-dist/doc $PKG_TEXDOCS/usr/share/texmf-dist
mv texmf/doc $PKG_TEXDOCS/usr/share/texmf

# Move the non-essential texmf-dist stuff to $PKG_TEXMF
rm -rf texmf-dist/{source,fonts/source}
mkdir -p $PKG_TEXMF/usr/share/texmf-dist
mv texmf-dist/* $PKG_TEXMF/usr/share/texmf-dist

# Move the required texmf stuff into place
# Ideally, some of the fonts can go, but which ones??
rm -rf texmf/{source,fonts/source}
mv texmf/* $PKG/usr/share/texmf

# Bring the man pages into the main package
mv $PKG_TEXDOCS/usr/share/texmf/doc/man $PKG/usr/share/texmf/

# Compress man pages
( cd $PKG/usr/share/texmf/man || exit 1
  rm -f Makefile
  find . -type f -name "*.pdf" -exec rm -f {} \;
  find . -type f -exec gzip -9 {} \;
  for i in $(find . -type l) ; do ln -s $(readlink $i).gz $i.gz ; rm $i ; done
)

mkdir -p $PKG/etc/profile.d
cat $CWD/profile.d/texlive.sh > $PKG/etc/profile.d/texlive.sh
cat $CWD/profile.d/texlive.csh > $PKG/etc/profile.d/texlive.csh
chmod 0755 $PKG/etc/profile.d/*

mkdir -p $PKG/install $PKG_TEXDOCS/install $PKG_TEXMF/install
zcat $CWD/doinst.sh.gz > $PKG/install/doinst.sh
cat $CWD/slack-desc.texlive > $PKG/install/slack-desc
cat $CWD/slack-desc.texdocs > $PKG_TEXDOCS/install/slack-desc
cat $CWD/slack-desc.texmf > $PKG_TEXMF/install/slack-desc

cd $PKG
/sbin/makepkg -l y -c n $TMP/texlive-$VERSION-$ARCH-$BUILD.tgz

cd $PKG_TEXMF
/sbin/makepkg -l y -c n $TMP/texlive-texmf-$TEXMF_VERSION-$ARCH-$BUILD.tgz

cd $PKG_TEXDOCS
/sbin/makepkg -l y -c n $TMP/texlive-docs-$TEXMF_VERSION-noarch-$BUILD.tgz

