#!/bin/bash

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

# Extract source:
tar xvvf $CWD/$PKGNAM-$VERSION.tar.?z*
cd $PKGNAM-$VERSION || exit 1
slackhousekeeping

# Add PATH setting to /sbin/dhclient-script
zcat $CWD/dhclient-script.PATH.diff.gz | patch -p1 || exit 1

# Configure:
CFLAGS="$SLKCFLAGS" \
./configure \
  --prefix=/usr \
  --sysconfdir=/etc \
  --localstatedir=/var \
  --infodir=/usr/info \
  --mandir=/usr/man \
  --libdir=/usr/lib${LIBDIRSUFFIX} \
  --docdir=/usr/doc/dhcp-$VERSION \
  --build=$ARCH-slackware-linux \
  --with-srv-lease-file=/var/state/dhcp/dhcpd.leases \
  --with-srv6-lease-file=/var/state/dhcp/dhcpd6.leases \
  --with-cli-lease-file=/var/state/dhcp/dhclient.leases \
  --with-cli6-lease-file=/var/state/dhcp/dhclient6.leases || exit 1
# Default pid dir is still /var/run, so no need to declare it

# Build:
make $NUMJOBS || failmake

# Install into package framework:
make install DESTDIR=$PKG || exit 1

# DHCP libraries need not be included, yet.
rm -rf $PKG/usr/{include,lib${LIBDIRSUFFIX}}

# We need this in /sbin
mkdir -p $PKG/sbin
mv $PKG/usr/sbin/dhclient $PKG/sbin

# Install the dhclient-script for linux
cat client/scripts/linux > $PKG/sbin/dhclient-script
chmod 700 $PKG/sbin/dhclient-script

mkdir -p $PKG/var/state/dhcp
# The daemon should create these automatically as needed
#touch $PKG/var/state/dhcp/dhcpd.leases.new
#touch $PKG/var/state/dhcp/dhcpd6.leases.new
#touch $PKG/var/state/dhcp/dhclient.leases.new
#touch $PKG/var/state/dhcp/dhclient6.leases.new

cat << EOF > $PKG/etc/dhcpd.conf.new
# dhcpd.conf
#
# Configuration file for ISC dhcpd (see 'man dhcpd.conf')
#
EOF

cat << EOF > $PKG/etc/dhclient.conf.new
# dhclient.conf
#
# Configuration file for ISC dhclient (see 'man dhclient.conf')
#
EOF

# Copy docs:
mkdir -p $PKG/usr/doc/dhcp-$VERSION/examples
cp -a \
  LICENSE README RELNOTES \
  $PKG/usr/doc/dhcp-$VERSION
mv $PKG/etc/dhcpd.conf $PKG/etc/dhclient.conf \
  $PKG/usr/doc/dhcp-$VERSION/examples

# 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
