#!/bin/sh
# Set initial variables:
CWD=`pwd`
if [ "$TMP" = "" ]; then
  TMP=/tmp
fi
PKG=$TMP/package-hotplug

VERSION=2004_01_05
ARCH=noarch
BUILD=3

if [ ! -d $TMP ]; then
  mkdir -p $TMP # location to build the source
fi
rm -rf $PKG
mkdir -p $PKG # place for the package to be built

cd $TMP
rm -rf hotplug-$VERSION
tar xjvf $CWD/hotplug-$VERSION.tar.bz2
cd hotplug-$VERSION
rm -f etc/hotplug/usb.agent.orig etc/hotplug/usb.agent.rej
chown -R root.root .
find . -perm 775 | xargs chmod 755
find . -perm 664 | xargs chmod 644
# Expand the list of modules to not load automatically:
zcat $CWD/hotplug.blacklist.diff.gz | patch -p1
# Fix net.agent to use rc.inet1:
zcat $CWD/hotplug.netagent.diff.gz | patch -p1
# Use usbfs, not usbdevfs.
zcat $CWD/hotplug.usbfs.diff.gz | patch -p1
# Use {usb/pci}modules to find modules.
zcat $CWD/hotplug.lister.diff.gz | patch -p1
# quell a pointless error message
zcat $CWD/hotplug.inputrc.diff.gz | patch -p1
# Log entry/exit to rc.hotplug:
zcat $CWD/hotplug.logging.diff.gz | patch -p1
# Don't stuff the logs full of warnings about attempts to load unneeded modules:
zcat $CWD/hotplug.logstooverbose.diff.gz | patch -p1
mkdir -p $PKG/usr/lib/hotplug/firmware
mkdir -p $PKG/sbin
cat sbin/hotplug > $PKG/sbin/hotplug
chmod 755 $PKG/sbin/hotplug
chown -R root.bin $PKG/sbin
cp -a etc $PKG
mv $PKG/etc/rc.d/init.d/hotplug $PKG/etc/rc.d/rc.hotplug
rmdir $PKG/etc/rc.d/init.d
mkdir -p $PKG/usr/man/man8
cat hotplug.8 | gzip -9c > $PKG/usr/man/man8/hotplug.8.gz
mkdir -p $PKG/usr/doc/hotplug-$VERSION
cp -a ChangeLog README $PKG/usr/doc/hotplug-$VERSION
mkdir -p $PKG/var/log/setup
cat $CWD/setup.hotplug > $PKG/var/log/setup/setup.hotplug
chmod 755 $PKG/var/log/setup/setup.hotplug
mv $PKG/etc/hotplug/blacklist $PKG/etc/hotplug/blacklist.new
mv $PKG/etc/hotplug/usb.usermap $PKG/etc/hotplug/usb.usermap.new
mkdir -p $PKG/install
zcat $CWD/doinst.sh.gz > $PKG/install/doinst.sh
cat $CWD/slack-desc > $PKG/install/slack-desc

# Build the package:
cd $PKG
makepkg -l y -c n $TMP/hotplug-$VERSION-$ARCH-$BUILD.tgz

# Clean up the extra stuff:
if [ "$1" = "--cleanup" ]; then
  rm -rf $TMP/hotplug-v$VERSION
  rm -rf $PKG
fi
