#!/bin/bash 

##########################################################################
# Script : kernel-modules.SlackBuild
# Purpose: Create a Slackware a/kernel-modules package
# Author : Stuart Winter <mozes@slackware.com>
# Date...: 20-Sep-2005
##########################################################################
# Changelog
############
# 20-Sep-2005 - v1.00
#       * First version for Linux 2.6.13.1
# 29-Mar-2009 - v1.01
#       * Revised to create separate module packages for each supported
#         architecture.
##########################################################################
# Caveats:
#  - You build all Kernel module packages for all archs.  It's just easier
#    to script this way.  Usually each kernel is settled by the time it's
#    released, so when a kernel update is performed, it's for all archs
#    anyway.

# 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-$PKGNAM

# Determine which kernel modules we have for this $VERSION of the Kernel
# and get building packages!
find $PORTCWD/sources \
  -type f \
  -name "*-kernel*${VERSION}*.xz" \
  -printf "%f\n" | sort | cut -d- -f1 | uniq | while read ARCHTYPE ; do

  export PKG=$TMP/package-${PKGNAM}_$ARCHTYPE
  mkpkgdirs # Delete & re-create temporary directories then cd into $TMPBUILD
  cd $PKG # Enter into the package directory for this architecture

  # In /etc/rc.d/rc.S, it runs /etc/rc.d/rc.modules-$FULLKERNELNAME
  # which in ARMedslack's stock kernels, is 2.6.x.x-versatile or -riscpc
  # which means that we need to make our rc.modules script this name.
  # But since we only need one rc.modules with some arch-specific checks,
  # we can symlink it a la (this seems a bit cheap and kludgy but 
  # I want to avoid patching the Slackware scripts as much as possible).
  # Create symlinks for each architecture we've found who has kernel modules
  # for this $VERSION of the Kernel we're building:
  ( mkdir -vpm755 etc/rc.d
    cd etc/rc.d

    # Install the Slackware Kernel module loading script, 'rc.modules':
    install -vpm755 $CWD/rc.modules.new rc.modules-${VERSION}.new
    # This broken symlink will be fixed by the install/doinst.sh script
    # after package installation.  However, if you install all the modules packages
    # of all supported archs then you will see dangling symlinks on your
    # filesystem.
    ln -vfs rc.modules-${VERSION} rc.modules-${VERSION}-${ARCHTYPE} )

    # Update /etc/rc.d/rc/rc.modules to contain additional loading for
    # some h/w specific stuff:
    # This means that each architecture's modules package will have the same
    # additional code, and that's intended since I don't know which package 
    # you'll install!
    # 
#cat << EOF >> etc/rc.d/rc.modules-${VERSION}.new
##
## RiscPC specific Kernel modules.
##
#if grep -q RiscPC /proc/cpuinfo ; then
#   # Regular RiscPC mouse support:
#   /sbin/modprobe rpcmouse
#   # Onboard sound:
#   /sbin/modprobe vidc_mod
#fi
cat << EOF >> etc/rc.d/rc.modules-${VERSION}.new
#
# ARM Versatile specific Kernel modules.
#
if grep -q ARM-Versatile /proc/cpuinfo ; then
   # ps/2 Mouse support:
   /sbin/modprobe psmouse
fi
EOF

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

   # Place the Kernel modules into the package for this particular
   # architecture:
   tar xvvf $PORTCWD/sources/$ARCHTYPE-kernel-modules-$VERSION.tar.xz -C.

   # Compress Kernel modules.  Helps conserve disk space on the 
   # embedded devices with small on-board storage:
   find ./lib/modules -type f -name "*.ko" -exec gzip -9f {} \;
   for i in $(find ./lib/modules -type l -name "*.ko") ; do ln -vfs $( readlink $i ).gz $i.gz ; rm -fv $i ; done

   # Install the package description:
   install -D -vpm644 $PORTCWD/slack-descs/$ARCHTYPE install/slack-desc

   # Install the post install script.
   # This is taken from Slackware's kernel-modules.SlackBuild script:
cat << EOF > 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 -favv etc/rc.d/rc.modules etc/rc.d/rc.modules.bak
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 a symlink to where the kernel was compiled
   # We wipe these and create new ones pointing to /usr/src
   # (our kernels were compiled in a temporary location, so the symlink would be broken)
   # 
   ( cd $PKG
     #find . -type l -name source -print0 | xargs -0 rm -f
     #find . -type l -name build  -print0 | xargs -0 rm -f 
     cd lib/modules/*
     # If you install the kernel source package, these symlinks will work:
     #ln -vfs ../../../usr/src/$( find . -name source -printf "%l\n" | rev | cut -d/ -f1 | rev ) source
     #ln -vfs ../../../usr/src/$( find . -name build -printf "%l\n" | rev | cut -d/ -f1 | rev ) build
     # if ln -f worked, we could do the above but since it doesn't wipe the existing
     # symlink, we'll do this:
     LINKLOC=$( find . -name source -printf "%l\n" | rev | cut -d/ -f1 | rev )
     rm -rf source
     ln -vfs ../../../usr/src/$LINKLOC source
     LINKLOC=$( find . -name build -printf "%l\n" | rev | cut -d/ -f1 | rev )
     rm -rf build
     ln -vfs ../../../usr/src/$LINKLOC build
)

   # Update the dependencies list:
   # depmod -b $PKG $VERSION
   # Generate modules.dep files for each ARM architecture we have inside lib/modules
   # (which is only 1 for each individual .tgz, but this is legacy from the all-in-one script
   # and works properly):
   ( cd $PKG
     find lib/modules -type d -mindepth 1 -maxdepth 1 -printf "%f\n" | xargs -i depmod {} -b. )

   # 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

   # Create the package in a subshell so that if we are adjusting the VERSION
   # (removing the "-") for the package name, then it doesn't affect the next
   # pass of this code; otherwise if we're building modules package for >1
   # architecture, it fails to match the version string to the filename.
   ( # Replace version number with a so it doesn't get confused with
     # the package name.
     # This is incase we're using any '-rc' releases.
     export VERSION="$( echo $VERSION | sed 's?-??g' )"

     # Set the package name to the particular architecture we're currently processing:
     # I don't know why I had this - it makes no sense to have a package named "kernel-modules-kirkwood-2.6.38.2_kirkwood-arm-2.tgz"
     # export SLACKPACKAGE=$PKGNAM-$ARCHTYPE-${VERSION}-$PKGARCH-$BUILD.tgz
     # I do now, this matches the naming scheme for Slackware x86's "smp" kernel package.
     # Even though the package name is different thus indeed creates a separate package,
     # it breaks the installer build script and since the x86 package will remain with the
     # same naming convention, it makes no sense to break it all here!
     export SLACKPACKAGE=$PKGNAM-$ARCHTYPE-${VERSION}_${ARCHTYPE}-$PKGARCH-$BUILD.tgz
     # run makepkg -l y -c n 
     slackmp )

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

# Finished with this arch, on to the next!
done

#EOF
