#!/bin/bash

# groff.SlackBuild
# Heavily based on the original Slackware build scripts,
# Modified by Stuart Winter <stuart@armedslack.org>

# Record toolchain & other info for the build log:
slackbuildinfo

# Paths to skeleton port's source & real Slackware source tree:
export CWD=$SLACKSOURCE/$PKGSERIES/$PACKAGE
export PORTCWD=$PWD

# Temporary build locations:
export TMPBUILD=$TMP/build-$PACKAGE
export PKG=$TMP/package-$PACKAGE
mkpkgdirs # Delete & re-create temporary directories then cd into $TMPBUILD

# Determine the CFLAGS for the known architectures:
case $PORTARCH in
   arm)     export SLKCFLAGS="-O2 -march=armv3 -mtune=xscale" ;;
   powerpc) export SLKCFLAGS="-O2" ;;
   sparc)   export SLKCFLAGS="-O2" ;;
   hppa)    export SLKCFLAGS="-O2" ;;
   *)       export SLKCFLAGS="-O2" ;;
esac

# Extract source:
tar jxvvf $CWD/$PACKAGE-$VERSION.tar.bz2
cd $PACKAGE-$VERSION
slackhousekeeping

# Apply patches:
zcat $CWD/groff.docdir.diff.gz | patch -p1 --verbose --backup --suffix=.orig || failpatch

# Configure:
CFLAGS="$SLKCFLAGS" \
CXXFLAGS="$SLKCFLAGS" \
./configure \
   --prefix=/usr \
   $ARCH-slackware-linux || failconfig

# Build:
make || failmake

# Install into package:
mkdir -p $PKG/usr
make install prefix=$PKG/usr
( cd src/devices/xditview
  mkdir -p $PKG/etc/X11/app-defaults
  cp -a GXditview.ad $PKG/etc/X11/app-defaults/GXditview )

# Copy docs:
mkdir -p $PKG/usr/doc/$PACKAGE-$VERSION/xditview
cp -a BUG-REPORT COPYING FDL INSTALL INSTALL.gen MORE.STUFF NEWS PROBLEMS PROJECTS README TODO VERSION \
      $PKG/usr/doc/$PACKAGE-$VERSION
( cd src/devices/xditview ; cp -a README TODO $PKG/usr/doc/$PACKAGE-$VERSION/xditview )

# If you want all this stuff, it's in the source tarball:
( cd $PKG/usr/doc/$PACKAGE-$VERSION
  rm -f *.ps examples html pdf )

# Do not use color ANSI output by default for man pages.
# A silly "innovation" if ever there was one, sure to break
# a ton of existing scripts otherwise...
zcat $CWD/groff.man.mdoc.local.gz >> $PKG/usr/share/groff/site-tmac/man.local
zcat $CWD/groff.man.mdoc.local.gz >> $PKG/usr/share/groff/site-tmac/mdoc.local

# Make links:
( cd $PKG
  ( cd usr/bin ; rm -rf geqn )
  ( cd usr/bin ; ln -sf eqn geqn )
  ( cd usr/bin ; rm -rf gindxbib )
  ( cd usr/bin ; ln -sf indxbib gindxbib )
  ( cd usr/bin ; rm -rf gpic )
  ( cd usr/bin ; ln -sf pic gpic )
  ( cd usr/bin ; rm -rf grefer )
  ( cd usr/bin ; ln -sf refer grefer )
  ( cd usr/bin ; rm -rf gsoelim )
  ( cd usr/bin ; ln -sf soelim gsoelim )
  ( cd usr/bin ; rm -rf zsoelim )
  ( cd usr/bin ; ln -sf soelim zsoelim )
  ( cd usr/bin ; rm -rf gtbl )
  ( cd usr/bin ; ln -sf tbl gtbl )
  ( cd usr/bin ; rm -rf gtroff )
  ( cd usr/bin ; ln -sf troff gtroff )
  ( cd usr/bin ; rm -rf glookbib )
  ( cd usr/bin ; ln -sf lookbib glookbib )
  ( cd usr/bin ; rm -rf gnroff )
  ( cd usr/bin ; ln -sf nroff gnroff )
  ( cd usr/bin ; rm -rf gneqn )
  ( cd usr/bin ; ln -sf neqn gneqn )
)

# If necessary, start the fakeroot server so we can set file/dir ownerships:
start_fakeroot

# Apply generic Slackware packaging policies:
cd $PKG
slackstripall   # strip all .a archives and all ELFs
slackgzpages -i # compress man & info pages and delete usr/info/dir
slackslack      # chown -R root:root, chmod -R og-w, slackchown, slack644docs
slackdesc       # install slack-desc and doinst.sh

# Build the package:
if [ $PORTARCH = arm ]; then
   slackmp # run makepkg
  else
   makepkg -l y -c n $PKGSTORE/$PKGSERIES/$PACKAGE-$VERSION-$ARCH-$BUILD.tgz
fi

# Perform any final checks on the package:
cd $PKG
slackhlinks     # search for any hard links
