#!/bin/bash

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

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

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

# Temporary build locations:
export TMPBUILD=$TMP/build-$PKGNAM
export PKG=$TMP/package-$PKGNAM
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 zxvvf $CWD/$PKGNAM-$VERSION.tar.gz
cd $PKGNAM-$VERSION
slackhousekeeping

# Create package framework:
mkdir -pm755 $PKG/usr/{bin,man/man8,doc/$PKGNAM-$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/sources/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/sources/traceroute-1.4a5-llanfairpwllgwyngyllgogerychwyrndrobwllllantysiliogogogoch.patch.gz | patch -p1

# Configure:
./configure \
   --prefix=/usr \
   --build=$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 -pm644 traceroute.8 $PKG/usr/man/man8

# Install docs:
cp -a CHANGES FILES INSTALL README VERSION \
      $PKG/usr/doc/$PKGNAM-$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
slackmp         # run makepkg -l y -c n

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