#!/bin/bash

# lynx.SlackBuild
# Heavily based on the original Slackware build script,
# Modified by Stuart Winter <stuart@armedslack.org> for the Slackware porting Project.
# 09-Jul-2004

# Version of source archive (not necessarily aligned with the Slackware package name);
# the Slackware package version matches the latest official patch level.
# The package version is stored in <archname>/build
SRCVER=2.8.5rel.1

# 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

# Extract source:
tar jxvvf $CWD/${PACKAGE}${SRCVER}.tar.bz2
cd $PACKAGE*
slackhousekeeping

# Fix /usr/local paths:
zcat $CWD/lynx.path.diff.gz | patch -p1 --verbose || failpatch
# Apply recolorizing patch:
zcat $CWD/lynx.cfg.diff.gz | patch -p1 --verbose --backup || failpatch
# Apply official patches:
zcat $CWD/2.8.5rel.2.patch.gz | patch -p1 --verbose || failpatch
zcat $CWD/2.8.5rel.3.patch.gz | patch -p1 --verbose || failpatch
zcat $CWD/2.8.5rel.4.patch.gz | patch -p1 --verbose || failpatch
zcat $CWD/2.8.5rel.5.patch.gz | patch -p1 --verbose || failpatch

# Configure:
./configure \
   --prefix=/usr \
   --libdir=/usr/lib/lynx \
   --enable-default-colors \
   --sysconfdir=/etc \
   --with-screen=ncurses \
   --enable-gzip-help \
   --with-zlib \
   --enable-read-eta \
   --enable-scrollbar \
   --with-ssl \
   --enable-color-style \
   --enable-prettysrc \
   --enable-source-cache \
   --enable-nsl-fork \
   --enable-nls \
   --enable-persistent-cookies \
   --enable-vertrace \
   --disable-full-paths \
   --enable-addrlist-page \
   --enable-charset-choice \
   --enable-cjk \
   --enable-htmlized-cfg \
   --enable-justify-elts \
   --enable-locale-charset \
   --enable-externs \
   --enable-cgi-links \
   --enable-change-exec \
   --enable-exec-links \
   --enable-exec-scripts \
   --enable-internal-links \
   --with-bzlib || failconfig

# Build:
make || failmake

# Install into package:
make install DESTDIR=$PKG
make install-help DESTDIR=$PKG
make install-doc DESTDIR=$PKG

# Correct $DESTDIR brokenness:
rm -f $PKG/usr/lib/lynx/lynx_help/COPYHEADER \
      $PKG/usr/lib/lynx/lynx_help/COPYING
( cd $PKG/usr/lib/lynx/lynx_help
  ln -sf ../lynx_doc/COPYHEADER .
  ln -sf ../lynx_doc/COPYING . )
( mkdir -p $PKG/usr/doc
  cd $PKG/usr/doc
  rm -rf lynx-$VERSION
  ln -sf /usr/lib/lynx lynx-$VERSION )

# 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 (root:bin), 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
