#!/bin/bash

# module-init-tools.SlackBuild
# by Stuart Winter <stuart@armedslack.org> for the Slackware porting Project.
# Heavily based on the original Slackware build script.
# 08-Jun-2004

# Paths to skeleton port's source & real Slackware source tree:
export CWD=$SLACKSOURCE/$PKGSERIES/$PACKAGE
export PORTCWD=$PWD

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

# Update this:
VMODUTILS=2.4.27

# 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)     ARCH_CFLAGS="-march=armv3 -mtune=xscale" ;;
   powerpc) ARCH_CFLAGS="" ;;
   sparc)   ARCH_CFLAGS="" ;;
   *)       ARCH_CFLAGS="" ;;
esac

##### Build old modutils for 2.4.x.  This will eventually go away...

# Extract source:
tar jxvvf $CWD/modutils-$VMODUTILS.tar.bz2
cd modutils-*
slackhousekeeping

# insmod should use stderr for usage hints, not stdout:
zcat $CWD/insmod-hint-use-stderr.diff.gz | patch -p1 --verbose --backup -E --suffix=.orig || failpatch

# Install docs:
mkdir -p $PKG/usr/doc/modutils-$VMODUTILS
cp -a COPYING CREDITS ChangeLog NEWS README TODO \
      depmod/create_syms insmod/insmod_ksymoops_clean \
      $PKG/usr/doc/modutils-$VMODUTILS

# Configure:
CFLAGS= \
./configure \
  --enable-zlib \
  --enable-insmod-static \
  $ARCH-slackware-linux || failconfig

# Build:
make || failmake

# Install man pages:
( cd man
  mkdir -p $PKG/usr/man/man{1,2,5,8}
  install -m644 kernelversion.1 $PKG/usr/man/man1
  install -m644 modules.conf.5 $PKG/usr/man/man5
  install -m644 *.2 $PKG/usr/man/man2
  install -m644 *.8 $PKG/usr/man/man8
  rm -f $PKG/usr/man/man8/kerneld*

  # Move some of these manpages to .old:
  mv $PKG/usr/man/man8/depmod.8 $PKG/usr/man/man8/depmod.old.8
  mv $PKG/usr/man/man8/insmod.8 $PKG/usr/man/man8/insmod.old.8
  mv $PKG/usr/man/man8/lsmod.8 $PKG/usr/man/man8/lsmod.old.8
  mv $PKG/usr/man/man8/modinfo.8 $PKG/usr/man/man8/modinfo.old.8
  mv $PKG/usr/man/man8/modprobe.8 $PKG/usr/man/man8/modprobe.old.8
  mv $PKG/usr/man/man8/rmmod.8 $PKG/usr/man/man8/rmmod.old.8 )

# Install binaries:
mkdir -p $PKG/sbin
install -m755 depmod/depmod $PKG/sbin/depmod.old
install -m755 insmod/insmod $PKG/sbin/insmod.old
install -m755 insmod/insmod.static $PKG/sbin/insmod.static.old
install -m755 insmod/modinfo $PKG/sbin/modinfo.old
install -m755 genksyms/genksyms $PKG/sbin
install -m755 insmod/kernelversion $PKG/sbin
install -m755 insmod/insmod_ksymoops_clean $PKG/sbin

# Create symlinks:
( cd $PKG
  ( cd sbin ; rm -rf ksyms )
  ( cd sbin ; ln -sf insmod.old ksyms )
  ( cd sbin ; rm -rf lsmod.old )
  ( cd sbin ; ln -sf insmod.old lsmod.old )
  ( cd sbin ; rm -rf modprobe.old )
  ( cd sbin ; ln -sf insmod.old modprobe.old )
  ( cd sbin ; rm -rf rmmod.old )
  ( cd sbin ; ln -sf insmod.old rmmod.old )
  ( cd sbin ; rm -rf kallsyms )
  ( cd sbin ; ln -sf insmod.old kallsyms )
)

####### Build module-init-tools for 2.6.x+ ############################

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

# Look for /etc/modprobe.d/ first rather than bailing on the idea if /etc/modprobe.conf
# exists.  IMHO, this is a better default behavior because it allows /etc/modprobe.conf
# to remain behind for 2.4 kernels and allows a smoother transition towards using
# /etc/modprobe.d/.
zcat $CWD/modprobe.favor.etc.modprobe.d.diff.gz | patch -p1 --verbose --backup --suffix=.orig || failpatch

# Configure:
CFLAGS= \
./configure \
  --prefix=/ \
  --enable-zlib \
  $ARCH-slackware-linux || failconfig

# Build:  This requires the docbook2man thing from sgmltools which as yet
# (02-Jan-05) I haven't built because it's in srpm format and last time I checked,
# didn't know anything about ARM.
make -i # || failmake

# Install binaries:
install -m755 depmod generate-modprobe.conf insmod insmod.static \
              lsmod modinfo modprobe rmmod \
              $PKG/sbin

# Setup symlinks:
mkdir -p $PKG/bin
( cd $PKG/bin ; ln -sf /sbin/lsmod . )
( cd $PKG/bin ; ln -sf /sbin/lsmod.old . )

# Install man pages:
mkdir -p $PKG/usr/man/man{5,8}
install -m644 *.8 $PKG/usr/man/man8
install -m644 *.5 $PKG/usr/man/man5

# Install docs:
mkdir -p $PKG/usr/doc/$PACKAGE-$VERSION
cp -a AUTHORS COPYING ChangeLog FAQ INSTALL NEWS README TODO \
      $PKG/usr/doc/$PACKAGE-$VERSION

mkdir -p $PKG/etc/cron.hourly
mkdir -p $PKG/etc/modprobe.d/
zcat $CWD/kmod.gz > $PKG/etc/cron.hourly/kmod
chmod 755 $PKG/etc/cron.hourly/kmod

cat << EOF > $PKG/etc/modules.conf.new
# /etc/modules.conf (Linux 2.4 modules config file)
#
# This file is for setting module options when using a 2.4 kernel.
#
# For more information, see "man modules.conf".
EOF

cat << EOF > $PKG/etc/modprobe.conf.new
# /etc/modprobe.conf (old location for Linux 2.6+ config)
#
# The use of this config file is deprecated if you are using a
# 2.6.x or newer kernel.  Instead, create files in the
# /etc/modprobe.d/ directory containing modprobe options.
#
# For more information, see "man modprobe.conf".
EOF

if [ -f $PKG/usr/man/man5/modprobe.conf.5.gz -a ! -e $PKG/usr/man/man5/modprobe.d.5.gz ]; then
   ( cd $PKG/usr/man/man5 ; ln -sf modprobe.conf.5.gz modprobe.d.5.gz )
fi

##############################################################

# 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
