#!/bin/bash

# traceroute.SlackBuild
# Heavily based on the original Slackware build scripts,
# Modified by Stuart Winter <stuart@armedslack.org>
# 14-Jul-2004

# Paths to skeleton port's source & real Slackware source tree:
export CWD=$SLACKSOURCE/n/traceroute
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 ARCH_CFLAGS="-O2 -march=armv3 -mtune=xscale" ;;
   powerpc) export ARCH_CFLAGS="-O2" ;;
   sparc)   export ARCH_CFLAGS="-O2" ;;
   *)       export ARCH_CFLAGS="-O2" ;;
esac

# Extract source:
tar zxvf $CWD/$PACKAGE-$VERSION.tar.gz
cd $PACKAGE-$VERSION
slackhousekeeping

# Create package framework:
mkdir -p $PKG/usr/{bin,man/man8,doc/$PACKAGE-$VERSION}

# Apply patches:
# This is Debian's.  Slackware uses an older Debian diff. ARMedslack doesn't cos 
# it won't compile without this newer diff - ./configure doesn't know about ARM Linux and
# it all goes down hill from there.
zcat $PORTCWD/patches/traceroute_1.4a12-15.diff.gz | patch -p1
# God knows where this Slackware patch came from, but it's in there:
zcat $CWD/traceroute.no.domain.strip.diff.gz | patch -p1
#
# These patches are taken from Fedora's src rpm.  I don't normally patch stuff into
# ARMedslack's packages if they aren't in Slackware, but traceroute is clearly wrong.
# This allows tracerouting to llanfairpwllgwyngyllgogerychwyrndrobwllllantysiliogogogoch.co.uk
zcat $PORTCWD/patches/traceroute-1.4a5-llanfairpwllgwyngyllgogerychwyrndrobwllllantysiliogogogoch.patch.gz | patch -p1

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

# Build: 
make CCOPT="-g $ARCH_CFLAGS -D_GNU_SOURCE -Wall" || failmake

# Start fakeroot server:
start_fakeroot

# Install binaries:
install -m4711 traceroute $PKG/usr/bin

# Install man pages:
install -m644 traceroute.8 $PKG/usr/man/man8

# Install docs:
cp -a CHANGES FILES INSTALL README VERSION \
      $PKG/usr/doc/$PACKAGE-$VERSION

# Slackware policies:
cd $PKG
slackstripall   # strip all .a archives and all ELFs
slackgzpages -i # compress man & info pages and delete usr/info/dir
slackchown      # root:bin binaries
slack644docs    # set correct perms on docs
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
