#!/bin/bash

###############################################################################
# linuxdoc-tools.build
# Build & install all components that form the linuxdoc-tools Slackware Package.
# by Stuart Winter <mozes@armedslack.org>
###############################################################################

# This installs onto the filesystem.  altertrack (from slacktrack) will
# build you a good package.  The other way would be to create individual packages.
# Having looked at the spec and Debian rules files and seen some of the kludges, 
# I concluded that it's far easier to install everything onto the filesystem - 
# especially since scripts already exist to track and build packages such build 
# scripts.
# If you don't want to use altertrack then take heed of the additional pre and post
# build scripts 'trackbuild.linuxdoc-tools' and 'postbuildfixes.sh' respectively.
#
# The configuration options are based on the RPM spec files contained
# in the source RPMs used to build the original sgmltools Slackware
# package.  This helps keep the package as similar as possible.

# Bundled package versions:
LINUXDOCTOOLSVER=0.9.21
SGMLCOMMONVER=0.6.3
OPENJADEVER=1.3.2
OPENSPVER=1.5.2
DOCBOOKVER=4.3
GTKDOCVER=1.6
SGMLSPLVER=1.03ii
XMLTOVER=0.0.18
ASCIIDOCVER=7.1.2
DOCBOOKUTILSVER=0.6.14

# Determine the general CFLAGS for the known architectures:
case $ARCH in
   arm)     export SLKCFLAGS="-O2 -march=armv3 -mtune=xscale" ;;
   i486)    export SLKCFLAGS="-O2 -march=i486 -mtune=i686" ;;
   s390)    export SLKCFLAGS="-O2" ;;
   x86_64)  export SLKCFLAGS="-O2" ;;
esac

# The build order is purposive -- don't modify it unless you know
# what you're doing.

####################### Build sgml-common ############################

# Extract source.  We're using a source RPM since it contains a number of
# patches that we need:
cd $TMP
# Extract the contents of the source rpm:
rpm2tgz $CWD/sources/sgml-common-${SGMLCOMMONVER}*.src.rpm
tar zxvvf sgml-common-*src.tgz
cd sgml-common-$SGMLCOMMONVER-*
# We need to store this path because it has some OpenJade files we require:
RPMCWD=$PWD
# Extract the real source archive:
tar zxvvf sgml-common-$SGMLCOMMONVER.tgz
cd sgml-common-$SGMLCOMMONVER
chown -R root:root .
chmod -R og-w .

# Apply patches (these are included in the source rpm):
patch -p1 < ../sgml-common-umask.patch        || exit 1
patch -p1 < ../sgml-common-xmldir.patch       || exit 1
patch -p1 < ../sgml-common-quotes.patch       || exit 1
# Hacks from the spec file:
tar zxvvf ../sgml-common-automake.tar.gz 
patch -p1 < ../sgml-common-automake.patch     || exit 1
patch -p1 < ../sgml-common-0.6.3-docdir.patch || exit 1

# Configure:
aclocal
automake --add-missing --copy
./configure \
   --prefix=/usr \
   --infodir=/usr/info \
   --with-docdir=/usr/doc \
   --sysconfdir=/etc || exit 1
#   --with-etcsgml=/etc/sgml || exit 1

# Build:
make || exit 1

# Install onto the filesystem:
make -e 'mkdir_p=mkdir -p' install || exit 1
mkdir -pm755 /usr/share/sgml/docbook/
mkdir -pm755 /etc/xml

# Create an empty XML catalog:
XMLCATALOG=/etc/xml/catalog
/usr/bin/xmlcatalog --noout --create $XMLCATALOG
# Now put the common DocBook entries in it
/usr/bin/xmlcatalog --noout --add "delegatePublic" \
        "-//OASIS//ENTITIES DocBook XML" \
        "file:///usr/share/sgml/docbook/xmlcatalog" $XMLCATALOG
/usr/bin/xmlcatalog --noout --add "delegatePublic" \
        "-//OASIS//DTD DocBook XML" \
        "file:///usr/share/sgml/docbook/xmlcatalog" $XMLCATALOG
/usr/bin/xmlcatalog --noout --add "delegatePublic" \
        "ISO 8879:1986" \
        "file:///usr/share/sgml/docbook/xmlcatalog" $XMLCATALOG
/usr/bin/xmlcatalog --noout --add "delegateSystem" \
        "http://www.oasis-open.org/docbook/" \
        "file:///usr/share/sgml/docbook/xmlcatalog" $XMLCATALOG
/usr/bin/xmlcatalog --noout --add "delegateURI" \
        "http://www.oasis-open.org/docbook/" \
        "file:///usr/share/sgml/docbook/xmlcatalog" $XMLCATALOG
# Also create the common DocBook catalog
/usr/bin/xmlcatalog --noout --create \
        /usr/share/sgml/docbook/xmlcatalog
rm -f /usr/share/sgml/xml.dcl
install -m644 $RPMCWD/{xml.soc,xml.dcl,html.dcl,html.soc} \
        /usr/share/sgml

# Install docs:
cp -fa COPYING AUTHORS INSTALL NEWS README ../sgml-common-CHANGES \
       /usr/doc/sgml-common-$SGMLCOMMONVER

####################### Install docbook ############################

# Do not alter the order of these unless you know what you're doing:
# the installation order is important -- we can't let RPM resolve
# dependencies (which it normally would if you issued rpm -Uvh *.rpm)
# because this is not a Red Hat box so RPM doesn't know about our
# installed Slackware packages.
# The installation order came from examining the .spec files.

export DOCBOOKS="docbook-dtds
docbook-simple
docbook-style-xsl
docbook-style-dsssl
docbook-slides"

# There are three ways of doing this:
# [1] Spend a considerable amount of time writing a docbook build script.
#     Having examined Debian's and Fedora's scripts, I realised that 
#     this would consume too much time, but would be ideal.
# [2] Build Fedora's source RPMs and install them - this is the way it was
#     done previously in Slackware.
# [3] Install the prebuilt noarch RPMs which will be identical to those
#     built from this script.
# 
# I choose the [2].  I chose [3] for a brief moment due to rpm not knowing
# about more recent additions to the ARM architecture family, but then 
# I found that rpm won't even install a *noarch* RPM if it doesn't
# recognise the architecture reported from uname(2).  LD_PRELOAD is great.

# This is option [3]:
#for rpmfile in $DOCBOOKS ; do
#  # Install the pre-built RPM onto the filesystem:
#  rpm --force --nodeps -Uvh $CWD/sources/$rpmfile-*noarch.rpm || exit 2
#done

# Option [2]:
# Save any previous rpm macros file:
if [ -f ~/.rpmmacros ]; then
   mv -f ~/.rpmmacros ~/.rpmmacros.bak
fi

# We'll have none of this /usr/src/rpm hanky panky:
cat << EOF > ~/.rpmmacros
%_topdir    $TMP/rpms/
EOF

# Create structure for rpmbuild:
mkdir -p $TMP/rpms/{BUILD/noarch,RPMS,SOURCES,SPECS,SRPMS,tmp,wrappers}

# Install my tar wrapper to work around older versions of RPM
# so that we call unzip instead of tar when required:
# I know, I know!
install -m755 $CWD/tar-wrapper $TMP/rpms/wrappers/tar

# Build & install RPMs:
# On Red Hat, rpmbuild should work to save us faffing about extracting
# sources, but on Slackware it doesn't seem to work reliably (rpm tries to
# use tar to extract zip files and so on; maybe because the version of rpm
# Slackware ships is older).  This way I can wrap tar and prevent the builds
# from failing; plus we can inject any of our own patches more easily in the
# future.
for rpmbasename in $DOCBOOKS ; do
  rpmfile=$CWD/sources/$rpmbasename*.src.rpm
  echo "[**] Building $rpmfile [**]"
  # Clean out the temp & source directories:
  rm -rf $TMP/rpms/tmp/*
  rm -rf $TMP/rpms/SOURCES/*
  ( cd $TMP/rpms/tmp
    # Add the tar wrapper to the PATH so rpm finds the tar wrapper:
    export PATH=$TMP/rpms/wrappers:$PATH
    # Convert the Source RPM into a tar archive:
    rpm2tgz $rpmfile
    # Extract the tar archive into the RPM 'SOURCES' directory:
    tar zxvvf ${rpmbasename}*.tgz -C $TMP/rpms/SOURCES
    # Build the binary RPM:
    cd $TMP/rpms/SOURCES/${rpmbasename}*
    cp -fa * ../ # So that RPM can find the sources... it's just an uphill struggle, this thing..
    rpmbuild -bb --nodeps --rebuild $rpmbasename.spec || exit 2 )
done

# Install the new RPM onto the filesystem.  Previously I was
# converting it into a tgz then exploding it onto the filesystem but
# the problem with that was that it wasn't updating the /etc/sgml/catalog 
# files.  The post install script for the RPMs handles all of this.
# Install in the right order.  This line below works but that may be
# because the shell happens to choose the right order due to file name sorting.
# Best be safe for the future:
# rpm --force --nodeps -Uvh $TMP/rpms/RPMS/noarch/*.rpm || exit 2
( cd $TMP/rpms/RPMS/noarch
  for rpmname in $DOCBOOKS ; do
      rpm --force --nodeps -Uvh ${rpmname}*.rpm
  done ) || exit 2

# If there was one, move back the original ~/.rpmmacros file:
if [ -f ~/.rpmmacros.bak ]; then
   mv -f ~/.rpmmacros.bak ~/.rpmmacros
  else
   rm -f ~/.rpmmacros # to save breaking any future build using rpm
fi

####################### Build xmlto ################################

# Extract source:
cd $TMP
tar jxvvf $CWD/sources/xmlto-$XMLTOVER.tar.bz2
cd xmlto-$XMLTOVER
chown -R root:root .
chmod -R og-w .

# Configure:
./configure \
   --prefix=/usr || exit 1

# Build:
make || exit 1

# Install onto filesystem:
make install || exit 1

# Copy docs:
mkdir -pm755 /usr/doc/xmlto-$XMLTOVER
cp -fa AUTHORS COPYING ChangeLog FAQ INSTALL NEWS README \
       /usr/doc/xmlto-$XMLTOVER

####################### Build OpenSP ############################

# Extract source:
cd $TMP
tar zxvvf $CWD/sources/OpenSP-$OPENSPVER.tar.gz
cd OpenSP-$OPENSPVER
chown -R root:root .
chmod -R og-w .

# Configure:
CFLAGS="$SLKCFLAGS" \
CXXFLAGS="$SLKCFLAGS" \
./configure \
   --prefix=/usr \
   --mandir=/usr/man \
   --datadir=/usr/share \
   --infodir=/usr/info \
   --disable-dependency-tracking \
   --disable-static \
   --enable-http \
   --enable-default-catalog=/usr/etc/sgml/catalog \
   --enable-default-search-path=/usr/share/sgml:/usr/share/xml || exit 1

# Build:
make || exit 1
#make check || exit 1

# Install onto the filesystem:
make install

# Rename sx to sgml2xml:
mv -f /usr/bin/osx /usr/bin/osgml2xml
mv -f /usr/man/man1/osx.1 /usr/man/man1/osgml2xml.1

# Create symlinks to binaries:
( cd /usr/bin
  for file in nsgmls sgmlnorm spam spent sgml2xml ; do
     rm -f $file
     ln -vfs o$file $file
  done )

# Fix man pages:
( cd /usr/man/man1
  for file in nsgmls sgmlnorm spam spent sgml2xml ; do
     rm -f ${file}.1*
     ln -vfs o${file}.1 ${file}.1 
  done )

# Rename docs directory:
mv -f /usr/doc/OpenSP /usr/doc/OpenSP-$OPENSPVER

####################### Build OpenJade ############################

# Extract source:
cd $TMP
tar zxvvf $CWD/sources/openjade-$OPENJADEVER.tar.gz
cd openjade-$OPENJADEVER*
chown -R root:root .
chmod -R og-w .

# Configure:
CFLAGS="$SLKCFLAGS" \
CXXFLAGS="$SLKCFLAGS" \
./configure \
   --prefix=/usr \
   --disable-static \
   --mandir=/usr/man \
   --infodir=/usr/info \
   --disable-static \
   --enable-splibdir=/usr/lib \
   --datadir=/usr/share/sgml/openjade-$OPENJADEVER || exit 1

# Incase later builds can't find the catalog file:
#export SGML_CATALOG_FILES="./catalog;/etc/sgml/catalog"

# Build:
make || exit 1

# Install onto the filesystem:
mkdir -p /etc/sgml
make install
cp -fa dsssl/catalog /usr/share/sgml/openjade-$OPENJADEVER/
cp -fa dsssl/{dsssl,style-sheet,fot}.dtd /usr/share/sgml/openjade-$OPENJADEVER/
touch /etc/sgml/openjade-$OPENJADEVER.soc
/usr/bin/install-catalog \
   --add /etc/sgml/openjade-$OPENJADEVER.soc \
   /usr/share/sgml/openjade-$OPENJADEVER/catalog 

# Create binary symlinks:
( cd /usr/bin && ln -vfs openjade jade )

# Fix man pages:
install -m644 docsrc/openjade.1 /usr/man/man1
( cd /usr/man/man1 && ln -vfs openjade.1 jade.1 )

# Copy docs:
mkdir -pm755 /usr/doc/openjade-$OPENJADEVER
cp -fa COPYING NEWS README VERSION doc/* dsssl/README.jadetex pubtext jadedoc unicode \
       /usr/doc/openjade-$OPENJADEVER

# The original package has this file, but it has most of the world's documentation
# too; we'll leave it in for the time being but I don't like it.
# rm -f /usr/doc/openjade-$OPENJADEVER/doc/Makefile

####################### Build SGMLSPL ##############################

# Extract source:
cd $TMP
tar zxvvf $CWD/sources/libsgmls-perl_$SGMLSPLVER.orig.tar.gz
cd libsgmls-perl-$SGMLSPLVER.orig
chown -R root:root .
chmod -R og-w .
zcat $CWD/sources/libsgmls-perl_$SGMLSPLVER-*.diff.gz | patch -p1 || exit 1

# Configure:
perl Makefile.PL INSTALLDIRS=site || exit 1

# Slackware man pages live in /usr/man:
sed -i 's?/share/man?/man/?g' Makefile

# Build:
make OPTIMIZE="$SLKCFLAGS" || exit 1

# Install into filesystem:
make install PREFIX=/usr
sed 's?usr/share/doc/sgmls-doc/?usr/doc/sgmlspl-'"$SGMLSPLVER"'/?g' sgmlspl.1 > /usr/man/man1/sgmlspl.1

# Copy docs:
mkdir -pm755 /usr/doc/sgmlspl-$SGMLSPLVER
cp -fa BUGS COPYING README TODO \
       /usr/doc/sgmlspl-$SGMLSPLVER
cp -fa DOC/HTML/{SGMLSpm,sgmlspl} \
       /usr/doc/sgmlspl-$SGMLSPLVER

####################### Build docbook-utils ########################

# docbook-utils requires some of the docbook docs to be installed
# prior to being built.

# Extract source:
cd $TMP
rpm2tgz $CWD/sources/docbook-utils-${DOCBOOKUTILSVER}*.src.rpm
tar zxvvf docbook-utils-*src.tgz
cd docbook-utils-${DOCBOOKUTILSVER}*
tar zxvvf docbook-utils-$DOCBOOKUTILSVER.tar.gz
cd docbook-utils-$DOCBOOKUTILSVER
chown -R root:root .
chmod -R og-w .

# Apply patches:
patch -p1 < ../docbook-utils-spaces.patch || exit 1

# Configure:
./configure \
   --prefix=/usr \
   --mandir=/usr/man || exit 1

# Build:
make || exit 1

# Install onto the filesystem:
make install htmldir=/usr/doc/docbook-utils-$DOCBOOKUTILSVER/html || exit 1

# db2html is not just a symlink, as it has to create the output directory:
rm -f /usr/bin/db2html
install -m755 ../db2html /usr/bin
install -m644 ../gdp-both.dsl /usr/share/sgml/docbook/utils-$DOCBOOKUTILSVER/docbook-utils.dsl

# Make binary symlinks:
( cd /usr/bin
  for util in dvi html pdf ps rtf ; do
     rm -f db2$util
     ln -vfs docbook2$util db2$util
  done )

# Make man page symlinks:
( cd /usr/man/man1
  for util in dvi html pdf ps rtf ; do
     rm -f db2$util.1*
     ln -vfs jw.1 db2$util.1
  done )

# Install docs:
mkdir -pm755 /usr/doc/docbook-utils-$DOCBOOKUTILSVER
cp -fa AUTHORS NEWS README TODO \
       /usr/doc/docbook-utils-$DOCBOOKUTILSVER

####################### Build linuxdoc-tools ########################
#
# This is the suite that replaced the original 'sgml-tools' package.
# It was later superceded by sgml-tools-lite.
# However, sgmltools-lite is missing things such as the sgml2{rtf,txt}
# et al.
# So we'll use linuxdoc-tools to maintain as much compatability
# with the original Slackware package as possible.

# Extract source:
cd $TMP
rpm2tgz $CWD/sources/linuxdoc-tools-${LINUXDOCTOOLSVER}*.src.rpm
# Extract the contents of the source rpm:
tar zxvvf linuxdoc-tools-${LINUXDOCTOOLSVER}*.src.tgz
cd linuxdoc-tools-${LINUXDOCTOOLSVER}*
# Extract the real source archive:
tar zxvvf linuxdoc-tools_$LINUXDOCTOOLSVER.tar.gz
cd linuxdoc-tools-$LINUXDOCTOOLSVER
chown -R root:root .
chmod -R og-w .

# Apply patches (these are included in the source rpm):
patch --verbose -p1 < ../linuxdoc-tools-0.9.13-letter.patch || exit 1
patch --verbose -p1 < ../linuxdoc-tools-0.9.20-strip.patch  || exit 1
patch --verbose -p1 < ../linuxdoc-tools-0.9.20-lib64.patch  || exit 1
patch --verbose -p1 < ../linuxdoc-tools-0.9.21-badif.patch  || exit 1
patch --verbose -p1 < ../linuxdoc-tools-fi.patch            || exit 1

# Configure:
./configure \
   --prefix=/usr \
   --mandir=/usr/man \
   --infodir=/usr/info \
   --with-installed-iso-entities \
   --with-installed-nsgmls || exit 1
pushd entity-map
autoconf
./configure \
   --prefix=/usr \
   --mandir=/usr/man \
   --infodir=/usr/info || exit 1
popd

# Build:
make OPTIMIZE="$SLKCFLAGS" \
     prefix=/usr \
     mandir=/usr/man \
     PERL=/usr/bin/perl \
     LINUXDOCDOC=/usr/doc/linuxdoc-tools-$LINUXDOCTOOLSVER 
perl -pi -e 's,\$main::prefix/share/sgml/iso-entities-8879.1986/iso-entities.cat,/usr/share/sgml/sgml-iso-entities-8879.1986/catalog,' \
           lib/LinuxDocTools.pm

# Ensure that we install this into Perl's site_perl directory.  Red Hat has
# a 'vendor_perl' directory; we don't.
eval `perl '-V:installsitelib'`

# Install onto the filesystem:
make prefix=/usr \
     mandir=/usr/man \
     bindir=/usr/bin \
     PERL=/usr/bin/perl \
     perl5libdir=/$installsitelib \
     LINUXDOCDOC=/usr/doc/linuxdoc-tools-$LINUXDOCTOOLSVER \
     install || exit 1
perl -pi -e 's,/usr/share/sgml/iso-entities-8879.1986/iso-entities.cat,\$main::prefix/share/sgml/sgml-iso-entities-8879.1986/catalog,' \
           /usr/share/linuxdoc-tools/LinuxDocTools.pm
rm -f /usr/share/linuxdoc-tools/epsf.*
rm -f /usr/share/linuxdoc-tools/url.sty

# This package contains a number of programs. 
# We maintain our own change log for this package.  Since the package name is
# 'linuxdoc-tools', let's put our changelog into here:
cp -favv $CWD/ChangeLog.txt /usr/doc/linuxdoc-tools-$LINUXDOCTOOLSVER/Slackware-ChangeLog.txt

####################### Build gtk-doc ###############################

# Extract source:
cd $TMP
tar jxvvf $CWD/sources/gtk-doc-$GTKDOCVER.tar.bz2
cd gtk-doc-$GTKDOCVER
chown -R root:root .
chmod -R og-w .

# Configure:
./configure \
   --prefix=/usr \
   --enable-public-id || exit 1

# Install onto the filesystem:
make install || exit 1

# Install docs:
mkdir -pm755 /usr/doc/gtk-doc-$GTKDOCVER
cp -fa AUTHORS COPYING INSTALL MAINTAINERS NEWS README TODO \
       /usr/doc/gtk-doc-$GTKDOCVER

####################### Build AsciiDoc ##############################

# Extract source:
cd $TMP
tar zxvvf $CWD/sources/asciidoc-$ASCIIDOCVER.tar.gz
cd asciidoc-$ASCIIDOCVER
chown -R root:root .
chmod -R og-w .

# Install:
cp -favv asciidoc.py /usr/bin/asciidoc
cp -favv a2x /usr/bin
chmod 755 /usr/bin/{asciidoc,a2x}
mkdir -p /etc/asciidoc/filters
mkdir -p /usr/share/asciidoc
cp -favv *.conf /etc/asciidoc
# This weird looking way of making a symlink is for makepkg's benefit:
( cd /etc/asciidoc
  ln -vfs ../../usr/share/asciidoc/stylesheets . )
cp -favv filters/{code-filter.conf,code-filter.py} /etc/asciidoc/filters/
install -m644 doc/asciidoc.1  /usr/man/man1
cp -favv images/ stylesheets/ /usr/share/asciidoc/
find /etc/asciidoc -type f -print0 | xargs -0 chmod 644

# Install docs:
mkdir -pm755 /usr/doc/asciidoc-$ASCIIDOCVER
cp -fa BUGS CHANGELOG COPY* README \
       /usr/doc/asciidoc-$ASCIIDOCVER/

#######################################################################

# Install the package description:
rm -rf /install
mkdir -pm755 /install
install -m644 $CWD/slack-desc /install
