#!/bin/sh
CWD=`pwd`
TMP=${TMP:-/tmp}
PKG=$TMP/package-dhcpcd

VERSION=2.0.4
ARCH=${ARCH:-i486}
BUILD=2

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

if [ ! -d $TMP ]; then
  mkdir -p $TMP
fi
rm -rf $PKG
mkdir -p $PKG
cd $TMP
rm -rf dhcpcd-$VERSION
tar xjvf $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

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
make -j3
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
makepkg -l y -c n $TMP/dhcpcd-$VERSION-$ARCH-$BUILD.tgz