#!/bin/bash

# dhcp.SlackBuild
# Heavily based on the original Slackware build scripts,
# Modified by Stuart Winter <stuart@armedslack.org>
# 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=armv4"
            export LIBDIRSUFFIX="" ;;
   *)       export SLKCFLAGS="-O2" ;;
esac

# Extract source:
tar xvvf $CWD/$PKGNAM-$VERSION.tar.gz
cd $PKGNAM-$VERSION
slackhousekeeping

# Configure:
./configure || failconfig

# Build:
make DEBUG="$SLKCFLAGS" $NUMJOBS || failmake

# Install into package framework:
make DEBUG="$SLKCFLAGS" install DESTDIR=$PKG || failinstall
rm -rf $PKG/usr/local
install -vpm700 client/scripts/linux $PKG/sbin/dhclient-script
install -vpm644 $PORTCWD/*.conf.new $PKG/etc
mkdir -p $PKG/var/state/dhcp
rm -f $PKG/var/state/dhcp/dhcpd.leases
touch $PKG/var/state/dhcp/dhcpd.leases.new

# Install docs:
mkdir -p $PKG/usr/doc/$PKGNAM-$VERSION/examples
cp -a LICENSE README RELNOTES \
     $PKG/usr/doc/$PKGNAM-$VERSION

cp -a client/dhclient.conf server/dhcpd.conf \
      $PKG/usr/doc/$PKGNAM-$VERSION/examples

# This stuff isn't in Slackware's package so it gets deleted:
#( cd $PKG
#  rm -rf usr/{man/man{1,3},bin,local} 
#  rm -f usr/man/man5/{dhcp-eval.5,dhcp-options.5} )

# 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

# Make package install script:
cat << EOF > $PKG/install/doinst.sh
#!/bin/sh
config() {
  NEW="\$1"
  OLD="\$(dirname \$NEW)/\$(basename \$NEW .new)"
  # If there's no config file by that name, mv it over:
  if [ ! -r \$OLD ]; then
    mv \$NEW \$OLD
  elif [ "\$(cat \$OLD | md5sum)" = "\$(cat \$NEW | md5sum)" ]; then # toss the redundant copy
    rm \$NEW
  fi
  # Otherwise, we leave the .new copy for the admin to consider...
}
config etc/dhcpd.conf.new
config etc/dhclient.conf.new
config var/state/dhcp/dhcpd.leases.new
rm -f var/state/dhcp/dhcpd.leases.new
EOF

slackmp         # run makepkg -l y -c n

# Perform any final checks on the package:
cd $PKG
slackhlinks     # search for any hard links

