#!/bin/bash

# bind.SlackBuild
# Heavily based on the original Slackware build script,
# with modifications from Mark K. Post from Slack390.
# Modified by Stuart Winter <stuart@armedslack.org> ARMedslack.
# 08-July-2004

# 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" ;;
   alpha)   export SLKCFLAGS="-O2" ;;
   x86_64)  export SLKCFLAGS="-O2" ;;
   i486)    export SLKCFLAGS="-O2" ;;
   hppa)    export SLKCFLAGS="-O2" ;;
   mips)    export SLKCFLAGS="-O2" ;;
   *)       export SLKCFLAGS="-O2" ;;
esac

# Extract source:
tar zxvvf $CWD/$PACKAGE*tar.gz
cd $PACKAGE-*
slackhousekeeping

# Apply patches:
zcat $CWD/bind.so_bsdcompat.diff.gz | patch -p1 --verbose || failpatch
# We want to use /var/run/named/, not just /var/run/.
# This allows changing the ownership of that directory if we want to run
# named as a non-root user.
zcat $CWD/bind.var.run.named.diff.gz | patch -p1 --verbose --backup --suffix=.orig || failpatch

# Configure:
CFLAGS="$SLKCFLAGS" \
LDFLAGS=-s \
./configure \
   --prefix=/usr \
   --sysconfdir=/etc \
   --localstatedir=/var \
   --with-libtool \
   --enable-shared \
   --enable-threads \
   --disable-threads \
   --with-openssl=/usr \
   $ARCH-slackware-linux || failconfig

# Build:
make || failmake

# Start the fakeroot server:
start_fakeroot

# Create package framework:
( cd $PKG 
  explodepkg $CWD/_bind.tar.gz 
  mkdir -p $PKG/usr/doc/$PACKAGE-$VERSION )

# Install:
make install DESTDIR=$PKG
( cd $PKG/usr/sbin && rm -rf lwresd && ln -sf named lwresd )
install -m755 isc-config.sh $PKG/usr/bin
install -m755 contrib/named-bootconf/named-bootconf.sh \
              $PKG/usr/sbin/

# Install caching examples:
install -m644 $CWD/caching-example/named.conf $PKG/etc/named.conf.new
install -m644 $CWD/caching-example/localhost.zone \
              $CWD/caching-example/named.ca \
              $CWD/caching-example/named.local \
              $PKG/var/named/caching-example/

# These are either empty dirs in the Slackware .tgz or they're
# not in the .tgz:
( cd $PKG
  rm -rf usr/include usr/man/man3
  rm -f usr/lib/*.{a,la}
  rm -f usr/lib/*.so )

# Install docs:
cp -a doc/arm doc/misc CHANGES COPYRIGHT FAQ README \
      $PKG/usr/doc/$PACKAGE-$VERSION
( cd $PKG && slack644docs )
# This one should have the correct perms of the config file:
install -m600 bin/rndc/rndc.conf misc/rndc.conf-sample

# One format of this is plenty.  Especially get rid of the bloated PDF.
( cd $PKG/usr/doc/bind-$VERSION/arm
  rm -f Makefile* *.pdf *.xml README.SGML latex-fixup.pl )

# Add sample config files for a simple caching nameserver:
install -m644 $CWD/caching-example/named.conf $PKG/etc/named.conf.new
install -m644 $CWD/caching-example/localhost.zone \
              $CWD/caching-example/named.ca \
              $CWD/caching-example/named.local \
              $PKG/var/named/caching-example

# Slackware policies:
cd $PKG
slackstripall   # strip all .a archives and all ELFs
slackgzpages -i # compress man & info pages and delete usr/info/dir
slackchown      # chmod root:root binaries
slackdesc       # install slack-desc and doinst.sh

# Set the version number to the package's version number (as it occasionally differs)
#VERSION=$PVER

# 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
