#!/bin/bash
ulimit -s unlimited
shopt -s extglob

# network-scripts.SlackBuild
# Heavily based on the original Slackware build scripts,
# Modified by Stuart Winter <mozes@slackware.com>
#
# Copyright 2006, 2007, 2008, 2009, 2010, 2012, 2013, 2015  Patrick J. Volkerding, Sebeka, MN, USA
# All rights reserved.
#
# Redistribution and use of this script, with or without modification, is
# permitted provided that the following conditions are met:
#
# 1. Redistributions of this script must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#
#  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
#  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
#  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO
#  EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
#  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
#  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
#  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
#  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
#  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
#  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

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

# Paths to skeleton port's source & real Slackware source tree:
slackset_var_cwds

# Temporary build locations:
export TMPBUILD=$TMP/build-$PKGNAM
export PKG=$TMP/package-$PKGNAM
mkpkgdirs # Delete & re-create temporary directories then cd into $TMPBUILD


mkdir -p $PKG/etc/rc.d
( cd $PKG/etc/rc.d
  for file in rc.inet1.conf rc.inet1 rc.inet2 rc.ip_forward ; do
    install -pm644 -oroot -groot $CWD/scripts/$file ${file}.new
  done
  chmod 755 rc.inet1.new rc.inet2.new
  chmod 600 rc.inet1.conf.new )
( cd $PKG/etc
  for file in HOSTNAME host.conf hosts.allow hosts.deny hosts.equiv hosts networks nntpserver protocols resolv.conf ; do
    install -pm644 -oroot -groot $CWD/scripts/$file ${file}.new
  done )
mkdir -p $PKG/sbin
install -pm755 -oroot -groot $CWD/scripts/netconfig $PKG/sbin/netconfig
mkdir -p $PKG/var/log/setup
install -pm755 -oroot -groot $CWD/scripts/setup.netconfig $PKG/var/log/setup

# Set the default timeout to be 15 seconds which works for the BananaPi:
#sed -i -e 's?10 seconds?15 seconds?g' -e 's?\${DHCP_TIMEOUT\[\$i\]:-10\}?\$\{DHCP_TIMEOUT\[\$i\]:-15\}?g' $PKG/etc/rc.d/rc.inet1.new
# This is now upstream but left in for reference in case someone asks.
# Stop using zeroconf.  On the SheevaPlugs, for some reason the zeroconf IP most usually wins so
# the machine continues booting without having NFS mounts working and so on.
# The --waitip (wait for an IP before backgrounding) option doesn't help because we *got* an IP from
# zeroconf, it's just that it was useless.
#sed -i 's?/sbin/dhcpcd -t ?/sbin/dhcpcd -Lt ?g' $PKG/etc/rc.d/rc.inet1.new
sed -i 's/^#\(DHCP_NOIPV4LL.*\)/\1/' $PKG/etc/rc.d/rc.inet1.conf.new

# Use the MAC address rather than a random number to form the "xid" - this is better for ARM devices
# where the amount of entropy is low due to the low number of i/o.
sed -i 's?dhcpcd -?dhcpcd -H?g' $PKG/etc/rc.d/rc.inet1.new

# Add manpages:
mkdir -p $PKG/usr/man/man{5,8}
install -vpm644 $CWD/manpages/rc.inet1.conf.5 $PKG/usr/man/man5/
install -vpm644 $CWD/manpages/rc.inet1.8 $PKG/usr/man/man8/

# Add default module configs for bonding and ipv6
mkdir -p $PKG/lib/modprobe.d/
cat $CWD/modprobe.d/bonding.conf > $PKG/lib/modprobe.d/bonding.conf
cat $CWD/modprobe.d/ipv6.conf > $PKG/lib/modprobe.d/ipv6.conf

# Drop the documentation in /usr/doc/network-scripts-$VERSION:
mkdir -p $PKG/usr/doc/$PKGNAM-$VERSION
cp -a $CWD/README* $PKG/usr/doc/$PKGNAM-$VERSION

# Patch to fix rc.inet1.new for LXC:
( cd $PKG
  cat $CWD/0000-rc.inet1.new-lxc-additions.patch | patch -p1 --verbose || exit 1
  rm -f etc/rc.d/rc.inet1.new.orig ) || failpatch

# Patch netconfig to fix leaving a previously enabled rc.networkmanager
# executable even if networkmanager is no longer selected:
( cd $PKG
  cat $CWD/0001-netconfig-turn-off-rc.networkmanager-if-networkmanager-is-not-selected.patch | patch -p1 --verbose || exit 1
  rm -f sbin/netconfig.orig ) || failpatch

# Patch netconfig to add hostname-labeled loopback entries when DHCP
# is selected:
( cd $PKG
  cat $CWD/0002-add-hostname-loopback-entries-in-etc-hosts-for-dhcp.patch | patch -p1 --verbose || exit 1
  rm -f sbin/netconfig.orig) || failpatch

# Apply generic Slackware packaging policies:
cd $PKG
slackstripall   # strip all .a archives and all ELFs
#slackstriprpaths     # strip rpaths
slack_delete_lafiles # delete usr/lib{,64}/*.la
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
