#!/bin/bash

# udev.SlackBuild
# by Stuart Winter <mozes@slackware.com> for ARMedslack.
# 99.9999999"% based on the original Slackware build script ;-)
# 08-Jun-2004
#
# Slackware build script for udev
#
# Copyright 2006, 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.


# 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)     SLKCFLAGS="-march=armv5te" ;;
   powerpc) SLKCFLAGS="" ;;
   sparc)   SLKCFLAGS="" ;;
   *)       SLKCFLAGS="" ;;
esac

# Extract source:
tar xvvf $CWD/$PKGNAM-$VERSION.tar.xz
cd $PKGNAM-$VERSION || exit 1
slackhousekeeping

# Fix ata_id (remove with udev-186+)
zcat $CWD/udev-ata_id-fixup-all-8-not-only-6-bytes-of-the-fw_rev.patch.gz | patch -p1 || exit 1

# Fix 42-usb-hid-pm.rules (remove with udev-187+)
zcat $CWD/fix-42-usb-hid-pm.rules.diff.gz | patch -p1 || exit 1

# Fixup the rule generator for our use
zcat $CWD/rule_generator.diff.gz | patch -p1 || exit 1

# Patch 60-cdrom_id.rules to create a full set of symlinks.
# This is needed for a hotplugged optical device since the generated
# rules will not be used until the second time the device is plugged.
zcat $CWD/60-cdrom_id.rules.diff.gz | patch -p1 --verbose || exit 1

# Linux 2.6.38+ headers don't have v4l_id anymore, so we remove it.
#auto_apply_patch $PORTCWD/sources/remove-v4l_id.diff.xz || exit 1

# static libudev is needed for lvm2
LDFLAGS="$LDFLAGS -lrt" \
./configure \
   --prefix=/usr \
   --sysconfdir=/etc \
   --sbindir=/sbin \
   --bindir=/sbin \
   --with-rootprefix=/ \
   --libexecdir=/lib \
   --libdir=/usr/lib${LIBDIRSUFFIX} \
   --with-rootlibdir=/lib${LIBDIRSUFFIX} \
   --docdir=/usr/doc/udev-$VERSION \
   --mandir=/usr/man \
   --with-firmware-path=/lib/firmware/updates:/lib/firmware \
   --enable-introspection \
   --enable-floppy \
   --enable-static \
   --enable-rule_generator \
   --build=$ARCH-slackware-linux-gnueabi || failconfig

# Build:
make $NUMJOBS || make || failmake

# Install into package:
make install DESTDIR=$PKG || exit 1

# scsi_id and vol_id are needed by multipath
( cd $PKG/sbin
  ln -vfs ../lib/udev/scsi_id . )

# Put udevd back in /sbin
mv $PKG/lib/udev/udevd $PKG/sbin
ln -vfs /sbin/udevd $PKG/lib/udev/udevd

# Create some more package framework:
mkdir -vpm755 \
  $PKG/run \
  $PKG/{etc,lib}/modprobe.d \
  $PKG/etc/rc.d \
  $PKG/etc/udev/rules.d \
  $PKG/lib/firmware

# Yes, these will clobber existing config files.
cp -fav $CWD/config/udev.conf $PKG/etc/udev/udev.conf
# Copy Slackware custom rules
cp -fav $CWD/config/rules.d/* $PKG/lib/udev/rules.d/
# Now the init script and module-init-tools stuff
cp -fav $CWD/config/rc.d/rc.udev.new $PKG/etc/rc.d/rc.udev.new
cp -fav $CWD/config/modprobe.d/*.conf $PKG/lib/modprobe.d/
cp -fav $CWD/config/modprobe.d/README $PKG/etc/modprobe.d/

chown -R root:root $PKG/etc $PKG/lib/modprobe.d
find $PKG/etc $PKG/lib/modprobe.d -type f -exec chmod 644 {} \;
find $PKG/etc -type d -exec chmod 755 {} \;
chmod 755 $PKG/etc/rc.d/rc.udev.new

# Add extra device nodes to the package that udev doesn't make:
tar xvf $CWD/udev-fixed-devices.tar.gz -C $PKG

chown -R root:root $PKG/lib/udev
chmod 755 $PKG/lib/udev/*

# Add documentation:
mkdir -p $PKG/usr/doc/udev-$VERSION
cp -fav \
    COPYING* README* TODO \
    $PKG/usr/doc/udev-$VERSION
# Don't package the API docs:
rm -rf $PKG/usr/share/gtk-doc ; rmdir $PKG/usr/share 2>/dev/null

# 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
slackmp         # run makepkg -l y -c n

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