#!/bin/bash

# udev.SlackBuild
# by Stuart Winter <stuart@armedslack.org> for ARMedslack.
# 99.9999999"% based on the original Slackware build script ;-)
# 08-Jun-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

# Determine the CFLAGS for the known architectures:
case $PORTARCH in
   arm)     SLKCFLAGS="-march=armv3 -mtune=xscale" ;;
   powerpc) SLKCFLAGS="" ;;
   sparc)   SLKCFLAGS="" ;;
   *)       SLKCFLAGS="" ;;
esac

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

# Build:
make EXTRAS="extras/firmware extras/scsi_id extras/volume_id \
                extras/ata_id extras/usb_id extras/dasd_id \
                extras/edd_id extras/cdrom_id" || failmake

# Install into package:
mkdir -p $PKG/lib/udev
make install DESTDIR=$PKG

# Copy all helper programs to /lib/udev
cp -favv extras/firmware/firmware.sh $PKG/lib/udev/firmware_helper
cp -favv extras/path_id/path_id $PKG/lib/udev
cp -favv extras/scsi_id/scsi_id $PKG/lib/udev
cp -favv extras/ata_id/ata_id $PKG/lib/udev
cp -favv extras/usb_id/usb_id $PKG/lib/udev
cp -favv extras/dasd_id/dasd_id $PKG/lib/udev
cp -favv extras/edd_id/edd_id $PKG/lib/udev
cp -favv extras/cdrom_id/cdrom_id $PKG/lib/udev

# libvolume, needed by vol_id (used for persistent links) and by HAL
mkdir -p $PKG/usr/lib/pkgconfig
mkdir -p $PKG/usr/include
cp -favv extras/volume_id/vol_id $PKG/lib/udev
cp -favv extras/volume_id/lib/libvolume_id.so.0.69.0 $PKG/lib || exit 1
cp -favv extras/volume_id/lib/libvolume_id.pc $PKG/usr/lib/pkgconfig/
cp -favv extras/volume_id/lib/libvolume_id.a $PKG/usr/lib/
cp -favv extras/volume_id/lib/libvolume_id.h $PKG/usr/include/
( cd $PKG/lib
  ln -fs libvolume_id.so.0.69.0 libvolume_id.so.0.69
  ln -fs libvolume_id.so.0.69.0 libvolume_id.so.0
  ln -fs libvolume_id.so.0.69.0 libvolume_id.so )

# Into the Slackware location:
mv $PKG/usr/share/man $PKG/usr
rmdir $PKG/usr/share

rm -r $PKG/etc/udev
mkdir -p $PKG/etc/udev/rules.d \
         $PKG/lib/firmware \
         $PKG/etc/modprobe.d \
         $PKG/etc/rc.d

# Yes, these will clobber existing config files.
cp -favv $CWD/config/udev.conf $PKG/etc/udev/udev.conf
cp -favv $CWD/config/rules.d/udev.rules $PKG/etc/udev/rules.d/udev.rules
cp -favv $CWD/config/rc.d/rc.udev.new $PKG/etc/rc.d/rc.udev.new
cp -favv $CWD/config/modprobe.d/blacklist.new $PKG/etc/modprobe.d/blacklist.new
cp -favv $CWD/config/modprobe.d/isapnp.new $PKG/etc/modprobe.d/isapnp.new

chmod 644 $PKG/etc/udev/udev.conf \
  $PKG/etc/udev/rules.d/udev.rules \
  $PKG/etc/modprobe.d/blacklist.new \
  $PKG/etc/modprobe.d/isapnp.new
chmod 755 $PKG/etc/rc.d/rc.udev.new

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

# Add various helper scripts:
for file in $CWD/config/scripts/* ; do
  cp -a $file $PKG/lib/udev/
done
chmod 755 $PKG/lib/udev/*

# Copy docs:
mkdir -p $PKG/usr/doc/$PACKAGE-$VERSION
cp -favv COPYING FAQ README TODO RELEASE-NOTES docs \
         $PKG/usr/doc/$PACKAGE-$VERSION

# Copy manpages of helper applications in /extra
find extras/ -name "*\.8" -exec cp -a {} $PKG/usr/man/man8 \;

# 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
