#!/bin/bash

##########################################################################
# Script : kernel-modules.SlackBuild
# Purpose: Create a Slackware a/kernel-modules package
# Author : Stuart Winter <mozes@armedslack.org>
# Date...: 20-Sep-2005
##########################################################################
# Changelog
############
# 20-Sep-2005 - v1.00
#       * First version for Linux 2.6.13.1
##########################################################################

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

# Paths to skeleton port's source & real Slackware source tree:
export CWD=$SLACKSOURCE/$PKGSERIES/kernel-modules
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

# Create the package framework:
cd $PKG
mkdir -p install etc/rc.d

# Extract the modules from the archive that was created by the k/kernel-2.6.SlackBuild
# script:
tar jxvvf $PORTCWD/sources/$SLKARCH-kernel-modules-$VERSION.tar.bz2

# Install the Slackware Kernel module loading script, 'rc.modules':
install -m755 $CWD/rc.modules.new etc/rc.d/rc.modules-${VERSION}.new

# If we're building a RiscPC package, we will add some RiscPC specific modules:
case "$SLKARCH" in
  riscpc) 
cat << EOF >> $PKG/etc/rc.d/rc.modules-${VERSION}.new
#
# RiscPC specific Kernel modules.
#
# Regular RiscPC mouse support:
/sbin/modprobe rpcmouse
# Onboard sound:
/sbin/modprobe vidc_mod
EOF
;;

  versatile)
cat << EOF >> $PKG/etc/rc.d/rc.modules-${VERSION}.new
#
# ARM Versatile specific Kernel modules.
#
# ps/2 Mouse support:
/sbin/modprobe ps2mouse
EOF
;;

esac

# Install the package description:
install -m644 $PORTCWD/slack-descs/$SLKARCH install/slack-desc

# Install the post install script.  This script is taken from the real
# a/kernel-modules/ SlackBuild script but I prefer to keep these things as individual
# scripts rather than create them from inside build scripts:
#install -m644 $PORTCWD/doinst.sh install/
# Write out the doinst.sh:
cat << EOF > $PKG/install/doinst.sh
config() {
  NEW="\$1"
  OLD="\$(dirname \$NEW)/\$(basename \$NEW .new)"
  # If there's no config file by that name, mv it over:
  if [ ! -r \$OLD ]; then
    mv \$NEW \$OLD
  elif [ "\$(cat \$OLD | md5sum)" = "\$(cat \$NEW | md5sum)" ]; then # toss the redundant copy
    rm \$NEW
  fi
  # Otherwise, we leave the .new copy for the admin to consider...
}

config etc/rc.d/rc.modules-${VERSION}.new

# If rc.modules is a real file, back it up:
if [ -r etc/rc.d/rc.modules -a ! -L etc/rc.d/rc.modules ]; then
  cp -a etc/rc.d/rc.modules etc/rc.d/rc.modules.bak
fi
# Make rc.modules a symlink if it's not already, but do not replace
# an existing symlink.  You'll have to decide to point at a new version
# of this script on your own...
if [ ! -L etc/rc.d/rc.modules ]; then
  ( cd etc/rc.d ; rm -rf rc.modules )
  ( cd etc/rc.d ; ln -sf rc.modules-${VERSION} rc.modules )
fi

# A good idea whenever kernel modules are added or changed:
if [ -x sbin/depmod ]; then
  chroot . /sbin/depmod -a ${VERSION} 1> /dev/null 2> /dev/null
fi

EOF

# This is usually a symlink to where the kernel was compiled
# (/usr/src) but Slackware does not have this:
rm -f lib/modules/$VERSION*/{build,source}

# Update the dependencies list:
depmod -b $PKG $VERSION

# If necessary, start the fakeroot server so we can set file/dir ownerships:
start_fakeroot

# Apply some of the generic Slackware packaging policies:
cd $PKG
slackslack      # chown -R root:root, chmod -R og-w, slackchown, slack644docs

# 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
