#!/bin/bash

# hotplug.SlackBuild
# Heavily based on the original Slackware build scripts,
# Modified by Stuart Winter <stuart@armedslack.org>
# 04-Nov-2004

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

# Paths to skeleton port's source & real Slackware source tree:
export CWD=$SLACKSOURCE/$PKGSERIES/$PACKAGE
export PORTCWD=$PWD

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

# Extract source:
tar jxvvf $CWD/$PACKAGE-$VERSION.tar.bz2
cd $PACKAGE-$VERSION
slackhousekeeping

# Fix net.agent to use rc.inet1:
zcat $CWD/hotplug.netagent.diff.gz | patch -p1 --verbose || failpatch
# Patch net.agent again to shut down a DHCP lease via rc.inet1:
zcat $CWD/hotplug.netagent.dhcpcd.down.diff.gz | patch -p1 --verbose || failpatch
# quell a pointless error message
zcat $CWD/hotplug.inputrc.diff.gz | patch -p1 --verbose || failpatch
# Log entry/exit to rc.hotplug:
zcat $CWD/hotplug.logging.diff.gz | patch -p1 --verbose || failpatch
# Don't stuff the logs full of warnings about attempts to load unneeded modules:
zcat $CWD/hotplug.logstooverbose.diff.gz | patch -p1 --verbose || failpatch
# Quiet this thing down.  It should never be spitting out messages on the console.
zcat $CWD/hotplug.quiet.modprobe.diff.gz | patch -p1 --verbose || failpatch
# In theory, no lister program should be needed for 2.6.x.
# In practice, it doesn't work...
zcat $CWD/hotplug.listerslop.diff.gz | patch -p1 --verbose || failpatch
#
# If we're using a new enough kernel, try to use udevsend/udevstart after the
# initial setup with hotplug.  That leaves the system in a state where HAL will
# function as expected.
zcat $CWD/hotplug.udevsend.diff.gz | patch -p1 --verbose || failpatch
# If we're using a new udev, skip rc.hotplug entirely.  No wonder people were
# reporting that the new udev wasn't speeding anything up -- it was still
# running all those old shell scripts.
zcat $CWD/hotplug.skipfornewudev.diff.gz | patch -p1 --verbose || failpatch
#
# Under certain circumstances dhcpcd will exit right away (e.g. an infinite
# lease time), so nullify any output from it at shutdown as it is unlikely
# to be useful:
zcat $CWD/hotplug.netagent.dhcpcd.quiet.diff.gz | patch -p1 --verbose || failpatch

# Junk removal:
find . -name "*~"     | xargs rm -f
find . -name "*.orig" | xargs rm -f
find . -name "*.rej"  | xargs rm -f

# Create package framework:
mkdir -p $PKG/lib/firmware
mkdir -p $PKG/sbin
mkdir -p $PKG/etc/hotplug/{pci,usb}

# Install:
install -m755 sbin/hotplug $PKG/sbin
cp -a etc $PKG
mv $PKG/etc/rc.d/init.d/hotplug $PKG/etc/rc.d/rc.hotplug
rm -rf $PKG/etc/rc.d/init.d

# Install man pages:
mkdir -p $PKG/usr/man/man8
install -m644 hotplug.8 $PKG/usr/man/man8

# Install docs:
mkdir -p $PKG/usr/doc/$PACKAGE-$VERSION
cp -a ChangeLog README* $PKG/usr/doc/$PACKAGE-$VERSION

# Install Slackware scripts:
mkdir -p $PKG/var/log/setup
install -m755 $CWD/setup.hotplug $PKG/var/log/setup

# Black list:
install -m644 $CWD/config/blacklist $PKG/etc/hotplug/blacklist.new
rm -f $PKG/etc/hotplug/blacklist
mv $PKG/etc/hotplug/usb.usermap $PKG/etc/hotplug/usb.usermap.new

# 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

# Build the package:
if [ $PORTARCH = arm ]; then
   slackmp # run makepkg
  else
   makepkg -l y -c n $PKGSTORE/$PKGSERIES/$PACKAGE-$VERSION-$ARCH-$BUILD.tgz
fi

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