#!/usr/bin/bash
# This script assumes it will be launched within "/NAME/VERSION/src" dir.
# With all sources in "src" Your Vector Linux .tlz package, slack-desc,
# and slack-required will be found in "VERSION" dir. The extraction and
# build will be in a temp dir created in "NAME" dir, and then removed on exit.
# Comment out second to last line to keep this dir intact.
#
# This Template was compiled from the contributions of many users of the Vector
# Linux forum at http://forum.vectorlinux.com and from tidbits collected 
# from all over the internet. 
#
# Generated by sbbuilder-0.4.12.3, written by Rodrigo Bistolfi 
# (rbistolfi) and Raimon Grau Cuscó (Kidd) for VectorLinux.
#
# Please put your name below if you add some original scripting lines.
# AUTHORS = 

NAME="tetex"            #Enter package Name!
VERSION=${VERSION:-"3.0"}      #Enter package Version!
VER=$(echo $VERSION|sed 's/-/_/') #this fixes - in version
VL_PACKAGER=${VL_PACKAGER:-"hata_ph"}   #Enter your Name!
LINK=${LINK:-""}  #Enter URL for package here!


#SYSTEM VARIABLES
#----------------------------------------------------------------------------
BUILDNUM=${BUILDNUM:-"1"}
VL_VERSION=${VL_VERSION:-"$(ls /var/log/packages/|grep vlconfig2|cut -d "-" -f4|cut -c 2-5)"}
BUILD=${BUILD:-"$BUILDNUM""$VL_VERSION"}
ARCH=${ARCH:-"$(uname -m)"}
CONFIG_OPTIONS=${CONFIG_OPTIONS:-""}
LDFLAG_OPTIONS=${LDFLAG_OPTIONS:-""}
ADDRB=${ADDRB:-""} #Add deps that need to be added to the slack-required file here
EXRB=${EXRB:-""} #Add deps that need to be excluded from the slack-required file here
#----------------------------------------------------------------------------


#SETUP PACKAGING ENVIRONMENT
#--------------------------------------------
CWD=$(pwd)
cd ../
RELEASEDIR=$(pwd)
cd $CWD
mkdir -p $RELEASEDIR/tmp
TMP=$RELEASEDIR/tmp
PKG=$TMP/package-$NAME
NUMJOBS=${NUMJOBS:-" -j7 "}
#--------------------------------------------



if [ $UID != 0 ]; then
   echo "You are not authorized to run this script. Please login as root"
   exit 1
fi

if [ ! -x /usr/bin/requiredbuilder ]; then
   echo "Requiredbuilder not installed, or not executable."
   exit 1
fi

if [ $VL_PACKAGER = "YOURNAME" ]; then
   echo 'Who are you?
   Please edit VL_PACKAGER=${VL_PACKAGER:-YOURNAME} in this script.
   Change the word "YOURNAME" to your VectorLinux packager name.
   You may also export VL_PACKAGER, or call this script with
   VL_PACKAGER="YOUR NAME HERE"'
   exit 1
fi


#CFLAGS SETUP
#--------------------------------------------
if [[ "$ARCH" = i?86 ]]; then
  ARCH=i586
  SLKCFLAGS="-O2 -march=i586 -mtune=i686"
  CONFIGURE_TRIPLET="i486-slackware-linux"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2 -fpic"
  CONFIGURE_TRIPLET="x86_64-slackware-linux"
  LIBDIRSUFFIX="64"
elif [ "$ARCH" = "powerpc" ]; then
  SLKCFLAGS="-O2"
  CONFIGURE_TRIPLET="powerpc-vlocity-linux"
  LIBDIRSUFFIX=""
fi

export CFLAGS="$SLKCFLAGS $CFLAG_OPTIONS"
export CXXFLAGS=$CFLAGS
export LDFLAGS="$LDFLAGS $LDFLAG_OPTIONS"
#--------------------------------------------


#GET THE SOURCE
#--------------------------------------------
for SRC in $(echo $LINK);do
if [ ! -f $CWD/$(basename $SRC) ]
then
        wget -c $SRC
fi
done
#--------------------------------------------


rm -rf $PKG
mkdir -p $PKG
cd $TMP
rm -rf $NAME-$VERSION

# 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 xvf $CWD/tetex-texmf-$VERSION.tar.*
  chown -R root:root .
  find . -type f -perm 666 -exec chmod 644 {} \;
  # Change the sell-by date:
  zcat $CWD/patches/tetex.leet.diff.gz | patch -p1 --verbose || exit 1
)


#EXTRACT SOURCES
#-----------------------------------------------------
echo "Extracting source..."
tar xvf $CWD/$NAME-src-$VERSION.tar.* || exit 1
#-----------------------------------------------------


cd $TMP/$NAME-src-$VERSION

# Put any Patches into a patches folder in the src dir
#-----------------------------------------------------
#for i in $CWD/patches/*;do
#	patch -p1 <$i
#done
patch -p1 < $CWD/patches/tetex-3.0-gcc_4.4-1.patch
#-----------------------------------------------------

# Fix possible xpdf overflows:
zcat $CWD/patches/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/patches/tetex.var.fonts.diff.gz | patch -p1 --verbose || exit 1

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

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

# Use mktemp in scripts:
zcat $CWD/patches/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/patches/tetex.browsers.diff.gz | patch -p1 --verbose || exit 1

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

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

# Fix the cpascal header so there's no conflict with getline() in the
# generated C code:
zcat $CWD/patches/tetex.cpascal.getline.posix.2008.diff.gz | patch -p1 --verbose || exit 1

# Patch for libpng-1.4.0:
zcat $CWD/patches/tetex.png14.diff.gz | patch -p1 --verbose || exit 1

# Another problem caused by getline() in glibc:
zcat $CWD/patches/tetex.afm2tfm.getline.diff.gz | patch -p1 --verbose || exit 1

#SET PERMISSIONS
#-----------------------------------------
echo "Setting permissions..."
chown -R root:root .
find . -perm 664 -exec chmod 644 {} \;
find . -perm 777 -exec chmod 755 {} \;
find . -perm 2777 -exec chmod 755 {} \;
find . -perm 775 -exec chmod 755 {} \;
find . -perm 2755 -exec chmod 755 {} \;
find . -perm 774 -exec chmod 644 {} \;
find . -perm 666 -exec chmod 644 {} \;
find . -perm 600 -exec chmod 644 {} \;
find . -perm 444 -exec chmod 644 {} \;
find . -perm 400 -exec chmod 644 {} \;
find . -perm 440 -exec chmod 644 {} \;
find . -perm 511 -exec chmod 755 {} \;
find . -perm 711 -exec chmod 755 {} \;
find . -perm 555 -exec chmod 755 {} \;
#-----------------------------------------



#CONFIGURE & MAKE
#----------------------------------------------------------------------
# If you are building a KDE-related app, then change the following
# arguments in the script below:
# --prefix=$(kde-config -prefix) \
# --sysconfdir=/etc/kde \
#
# Making these changes will ensure that your package will build in the
# correct path and that it will work seamlessly within the KDE environment.
#
#-----------------------------------------------------------------------

echo "Configuring source..."
./configure --prefix=/usr/share/texmf \
  --libdir=/usr/lib${LIBDIRSUFFIX} \
  --datadir=/usr/share \
  --infodir=/usr/info \
  --sysconfdir=/etc \
  --localstatedir=/var \
  --mandir=/usr/man \
  --with-included-gettext \
  --disable-debug \
  --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 \
  --program-prefix="" \
  --program-suffix="" \
  --build=$CONFIGURE_TRIPLET \
  $CONFIG_OPTIONS || exit 1

# 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/patches/tetex.etex.diff.gz | patch -p1 --verbose || exit 1

make all $NUMJOBS || make all || exit 1
make install strip || exit 1

#######################################################################
#Miscellenious tweaks and things outside a normal ./configure go here #
#######################################################################

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.txz and tetex-bin.txz seperately.

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

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

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

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

#Fix manpage symlinks:
( cd $PKG/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
)

# copy some info file for tetex
mkdir -p $PKG2/usr/info
( cd /usr/info ; mv latex.info dvips.info dir web2c.info texi2html* kpathsea* $PKG2/usr/info )

# Move some of the files into the docs package:
mkdir -p $PKG2/usr/share/texmf
mv $PKG/usr/share/texmf/doc $PKG2/usr/share/texmf
mkdir -p $PKG2/usr/share/texinfo
mv /usr/share/texinfo/html $PKG2/usr/share/texinfo
mv $PKG/usr/share/texmf/info $PKG2/usr
( cd $PKG/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 )

mkdir -p $PKG/usr/doc/$NAME-$VERSION
cp -a ANNOUNCE* AUTHORS* BUGS* COPYING* ChangeLog* CHANGELOG* Credits* DEVELOPERS \
LICENSE* MANIFEST PROVENANCE README* TODO* Exception_to_GPL_for_Qt.txt \
$PKG/usr/doc/$NAME-$VERSION
cat $CWD/$NAME.SlackBuild > $PKG/usr/doc/$NAME-$VERSION/$NAME.SlackBuild

#----------------------------------------------------------------------

if [ -d $PKG/usr/share/man ] ; then
mkdir -p $PKG/usr/man
mv $PKG/usr/share/man/* $PKG/usr/man
rm -rf $PKG/usr/share/man
fi
find $PKG/usr/man -type f -exec gzip -9 {} \;

if [ -d $PKG/usr/share/info ] ; then
mkdir -p $PKG/usr/info
mv $PKG/usr/share/info/* $PKG/usr/info
rm -rf $PKG/usr/share/info
fi 

find $PKG/usr/info -type f -exec gzip -9 {} \;
mkdir -p $PKG/install
if [ -d $PKG/usr/info ] ; then
cat >> $PKG/install/doinst.sh << EOF
CWD=\$(pwd)
cd usr/info
if [ -f dir ];then
    rm dir
fi
if [ -f dir.gz ];then
    rm dir.gz
fi
for i in *.info.gz;do
        install-info \$i dir
done
cd \$CWD
EOF
fi

mkdir -p $PKG/install


#if there is a slack-desc in src dir use it
if test -f $CWD/slack-desc; then
cp $CWD/slack-desc $RELEASEDIR/slack-desc
else
# This creates the white space in front of "handy-ruler" in slack-desc below.

LENGTH=$(expr length "$NAME")
SPACES=0
SHIM=""
until [ "$SPACES" = "$LENGTH" ]; do
SHIM="$SHIM "
let SPACES=$SPACES+1
done

# Fill in the package summary between the () below.
# Then package the description, License, Author and Website.
# There may be no more then 11 $NAME: lines in a valid slack-desc.

cat > $RELEASEDIR/slack-desc.tetex << EOF
# HOW TO EDIT THIS FILE:
# The "handy ruler" below makes it easier to edit a package description.  Line
# up the first '|' above the ':' following the base package name, and the '|'
# on the right side marks the last column you can put a character in.  You must
# make exactly 11 lines for the formatting to be correct.  It's also
# customary to leave one space after the ':'.

$SHIM|-----handy-ruler------------------------------------------------------|
$NAME: $NAME (teTeX binaries and support files)
$NAME:
$NAME: This package contains the binaries and support files required for the
$NAME: teTeX TeX distribution, such as input files for TeX, runtime
$NAME: configuration files, hyphenation tables, manpages, and the computer
$NAME: modern fonts.
$NAME:
$NAME: License: GPL
$NAME: Authors: Thomas Esser
$NAME: Website: http://web.bilkent.edu.tr/History/valley/tetex-index.html

EOF
fi
cat >> $RELEASEDIR/slack-desc.tetex << EOF



#----------------------------------------
BUILDDATE: $(date)
PACKAGER:  $VL_PACKAGER
HOST:      $(uname -srm)
DISTRO:    $(cat /etc/vector-version)
CFLAGS:    $CFLAGS
LDFLAGS:   $LDFLAGS
CONFIGURE: $(awk "/\.\/configure\ /" $TMP/$NAME-$VERSION/config.log)

EOF

cat $RELEASEDIR/slack-desc.tetex > $PKG/install/slack-desc

mkdir -p $PKG2/install

cat > $RELEASEDIR/slack-desc.tetex-doc << EOF
# HOW TO EDIT THIS FILE:
# The "handy ruler" below makes it easier to edit a package description.  Line
# up the first '|' above the ':' following the base package name, and the '|'
# on the right side marks the last column you can put a character in.  You must
# make exactly 11 lines for the formatting to be correct.  It's also
# customary to leave one space after the ':'.

         |-----handy-ruler------------------------------------------------------|
tetex-doc: tetex-doc (Documentation for teTeX)
tetex-doc:
tetex-doc: This package contains documentation for AMSTeX, BibTeX, extended plain
tetex-doc: format, font packages, format files (babel, texdraw, pstricks, xypic),
tetex-doc: LaTeX, MetaPost, Makeindex, pdftex, the Kpathsea library, as well as
tetex-doc: general information about TeX such as a list of ftp servers, a draft
tetex-doc: for the TeX Directory Standard (TDS), The TeX and LaTeX Catalogue,
tetex-doc: TeX FAQs, and more.
tetex-doc:
tetex-doc: License: GPL
tetex-doc: Authors: Thomas Esser
tetex-doc: Website: http://web.bilkent.edu.tr/History/valley/tetex-index.html

EOF

cat >> $RELEASEDIR/slack-desc.tetex-doc << EOF



#----------------------------------------
BUILDDATE: $(date)
PACKAGER:  $VL_PACKAGER
HOST:      $(uname -srm)
DISTRO:    $(cat /etc/vector-version)
CFLAGS:    $CFLAGS
LDFLAGS:   $LDFLAGS
CONFIGURE: $(awk "/\.\/configure\ /" $TMP/$NAME-$VERSION/config.log)

EOF

cat $RELEASEDIR/slack-desc.tetex-doc > $PKG2/install/slack-desc


#STRIPPING
#------------------------------------------------------------------------------------------------------------------
cd $PKG
echo " "
echo "Stripping...."
echo " "
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
#------------------------------------------------------------------------------------------------------------------


#FINISH PACKAGE
#--------------------------------------------------------------
echo "Finding dependencies..."
ADD="$ADDRB" EXCLUDE="$EXRB" requiredbuilder -v -y -s $RELEASEDIR $PKG
echo "Creating package $NAME-$VERSION-$ARCH-$BUILD.txz"
makepkg -l y -c n $RELEASEDIR/$NAME-$VERSION-$ARCH-$BUILD.txz

#FINISH DOC PACKAGE
#--------------------------------------------------------------
cd $PKG2
echo "Finding dependencies..."
ADD="$ADDRB" EXCLUDE="$EXRB" requiredbuilder -v -y -s $RELEASEDIR $PKG
echo "Creating package $NAME-doc-$VERSION-$ARCH-$BUILD.txz"
makepkg -l y -c n $RELEASEDIR/$NAME-doc-$VERSION-$ARCH-$BUILD.txz

cd $CWD
echo "Cleaning up temp files..." && rm -rf $TMP
echo "Package Complete"
#--------------------------------------------------------------

# vim: set tabstop=4 shiftwidth=4 foldmethod=marker : ##
