#!/bin/bash

# net-tools.SlackBuild
# Heavily based on the original Slackware build scripts,
# Modified by Stuart Winter <stuart@armedslack.org>
# Please see changelog.txt for revisions to this package's build script history.

# 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 xvvf $CWD/$PKGNAM-$VERSION.tar.bz2
cd $PKGNAM-$VERSION
slackhousekeeping

# Apply patches:
zcat $CWD/net-tools_1.60-19.diff.gz | patch -p1 --backup || failpatch
zcat $CWD/net-tools.diff.gz | patch -p1 --backup || failpatch

# Copy docs:
mkdir -p $PKG/usr/doc/net-tools-1.60
cp -a README README.ipv6 $PKG/usr/doc/net-tools-1.60
chmod 644 $PKG/usr/doc/net-tools-1.60/*

# Build:
HAVE_IP_TOOLS=1 HAVE_MII=1 make || failmake
HAVE_IP_TOOLS=1 HAVE_MII=1 make hostname || failmake

# Install into package:
mkdir -p $PKG/sbin $PKG/bin $PKG/usr/sbin
cat arp > $PKG/sbin/arp
cat ifconfig > $PKG/sbin/ifconfig
cat rarp > $PKG/sbin/rarp
cat route > $PKG/sbin/route
cat hostname > $PKG/bin/hostname
cat mii-tool > $PKG/sbin/mii-tool
cat nameif > $PKG/sbin/nameif
cat netstat > $PKG/bin/netstat
cat plipconfig > $PKG/sbin/plipconfig
cat slattach > $PKG/usr/sbin/slattach
cat ipmaddr > $PKG/sbin/ipmaddr
cat iptunnel > $PKG/sbin/iptunnel
cd man/en_US
mkdir -vpm755 $PKG/usr/man/man{1,5,8}
for page in dnsdomainname.1 domainname.1 hostname.1 nisdomainname.1 \
  ypdomainname.1 ; do
  cat $page | gzip -9c > $PKG/usr/man/man1/$page.gz
done
cat ethers.5 | gzip -9c > $PKG/usr/man/man5/ethers.5.gz
for page in arp.8 ifconfig.8 mii-tool.8 nameif.8 netstat.8 rarp.8 route.8 \
  slattach.8 plipconfig.8 ; do
  cat $page | gzip -9c > $PKG/usr/man/man8/$page.gz
done
( cd $PKG/bin
  ln -vsf hostname dnsdomainname
  ln -vsf hostname nisdomainname
  ln -vsf hostname ypdomainname  )

# This is a little Slackware-specific tool used in some of the network
# related scripts to calculate network and broadcast addresses:
( cd $PKG/bin
  cc -O2 -o ipmask $CWD/ipmask.c
  chmod 755 ipmask
)
install -vpm644 $CWD/ipmask.8 $PKG/usr/man/man8/
chmod 755 $PKG/sbin/* $PKG/bin/* $PKG/usr/sbin/*

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