#!/bin/bash

# net-tools.SlackBuild
# Heavily based on the original Slackware build scripts,
# Modified by Stuart Winter <mozes@slackware.com>
# 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 $ARCH in
   arm)     export SLKCFLAGS="-O2 -march=armv5te"
            export LIBDIRSUFFIX="" ;;
   *)       export SLKCFLAGS="-O2" ;;
esac

# Extract source:
#tar xvvf $CWD/$PKGNAM-$VERSION.tar.xz
tar xvvf $PORTCWD/sources/$PKGNAM-$VERSION.tar.xz
cd $PKGNAM-* || exit 1
slackhousekeeping

zcat $CWD/net-tools.config.h.gz > config.h

# Patch to fix FTBFS:
sed -i 's? HAVE_HWSTRIP 1? HAVE_HWSTRIP 0?g' config.h
# http://lists.linuxfromscratch.org/pipermail/blfs-support/2012-August/070428.html
find -name "*.c" -o -name "*.h" | while read src; do
   sed -i "s?<linux/if_ether.h>?<netinet/if_ether.h>?g" $src
   sed -i "s?<linux/if_tr.h>?<netinet/if_tr.h>?g" $src
   sed -i "s?<linux/if_fddi.h>?<netinet/if_fddi.h>?g" $src
done

# Copy docs:
mkdir -vpm755 $PKG/usr/doc/$PKGNAM-$VERSION
cp -fav \
   COPYING* README* \
   $PKG/usr/doc/$PKGNAM-$VERSION

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

# 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
