#!/bin/bash

# iputils.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/$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

# ARMedslack is using the Debian source and diff here.
# I'm not sure why but I suspect the original does not build on
# ARM without patches.

# Extract source:
cd $TMPBUILD
tar zxvvf $PORTCWD/sources/iputils_*orig*.tar.gz
cd iputils*
slackhousekeeping

# Apply Debian patch:
zcat $PORTCWD/sources/iputils_*diff*.gz | patch -p1 --verbose || failpatch

# Build:
#make || failmake
#make symlink
#make IPV4_TARGETS=tracepath ping arping clockdiff rarpd rdisc \
#     IPV6_TARGETS=tracepath6 traceroute6 ping6 || failmake
make -j4 || failmake
make clockdiff rarpd rdisc || failmake
( cd doc ; make man )

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

# Install into package:
mkdir -p $PKG/{sbin,bin,usr/{bin,sbin,man/man8}}
install -vpm755 clockdiff arping \
                $PKG/sbin
install -vpm4711 ping6 ping \
                 $PKG/bin/
install -vpm755 rdisc rarpd \
                $PKG/usr/sbin/
install -vpm755 tracepath6 tracepath \
                 $PKG/usr/bin/
install -vpm4711 traceroute6 \
                 $PKG/usr/bin/
( cd doc
  install -m644 arping.8 clockdiff.8 ping.8 rarpd.8 rdisc.8 tracepath.8 traceroute6.8 \
                $PKG/usr/man/man8
  cd $PKG/usr/man/man8
  ln -sf ping.8 ping6.8
  ln -sf tracepath.8 tracepath6.8 )

# Copy docs:
mkdir -p $PKG/usr/doc/$PKGNAM-$VERSION
cp -a RELNOTES \
      $PKG/usr/doc/$PKGNAM-$VERSION

# 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
slack644docs    # 
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
