#!/bin/sh

# Slackware build script for dhcpcd

# Copyright 2008  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.

VERSION=${VERSION:-2.0.8}
ARCH=${ARCH:-i486}
BUILD=${BUILD:-1}

CWD=$(pwd)
TMP=${TMP:-/tmp}
PKG=$TMP/package-dhcpcd

if [ "$ARCH" = "i486" ]; then
  SLKCFLAGS="-O2 -march=i486 -mtune=i686"
elif [ "$ARCH" = "s390" ]; then
  SLKCFLAGS="-O2"
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2 -fPIC"
fi

rm -rf $PKG
mkdir -p $TMP $PKG
cd $TMP
rm -rf dhcpcd-$VERSION
tar xvf $CWD/dhcpcd-$VERSION.tar.bz2
cd dhcpcd-$VERSION

# This changes the default location where the .pid and
# other files are written from /var/run to /etc/dhcpc.
# The latter directory was long traditional, and (more
# importantly) you can't be sure that /var is mounted
# yet when dhcpcd is invoked.
zcat $CWD/dhcpcd.etc.dhcpc.diff.gz | patch -p1 --verbose || exit 1

# Default to less noise to the console, and begin lines with a
# capital letter where it looks better:
zcat $CWD/dhcpcd.console.messages.diff.gz | patch -p1 --verbose || exit 1

chown -R root:root .
find . -perm 777 -exec chmod 755 {} \;
find . -perm 664 -exec chmod 644 {} \;
CFLAGS="$SLKCFLAGS" \
./configure \
  --prefix=/usr \
  --sbindir=/sbin \
  --sysconfdir=/etc \
  --localstatedir=/var \
  --enable-debug=no
make -j6
make install DESTDIR=$PKG

# This script has been installed non-executable for several dhcpcd
# releases leading me to believe this is intentional on the part of
# the developers.  Indeed, the script is called a "sample" in the
# included comments, and there's been at least one security issue
# related to its use in the past, so it's probably a safer bet to
# not install it so that it works "out of the box".  However, making
# it non-executable is not the best solution for this as it leads
# to error messages in the system logs which I hear about from time
# to time.  Instead, we'll make it executable, but will give it a
# suffix of -sample so that dhcpcd won't attempt to run it.
#
# As far as I can tell it does nothing more than add some extra
# verbosity to the logs anyway, and rc.inet1 already does that.
# There's really no need that I can see to enable the script.
mv $PKG/etc/dhcpc/dhcpcd.exe $PKG/etc/dhcpc/dhcpcd.exe-sample
chmod 755 $PKG/etc/dhcpc/dhcpcd.exe-sample
( cd $PKG
  find . | xargs file | grep "executable" | grep ELF | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null
  find . | xargs file | grep "shared object" | grep ELF | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null
)

mkdir -p $PKG/usr/doc/dhcpcd-$VERSION
cp -a AUTHORS COPYING* ChangeLog INSTALL NEWS README \
  $PKG/usr/doc/dhcpcd-$VERSION

gzip -9 $PKG/usr/man/man?/*.?

mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc

cd $PKG
/sbin/makepkg -l y -c n $TMP/dhcpcd-$VERSION-$ARCH-$BUILD.tgz

