#!/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

#
# Build module-init-tools statically:
# LDFLAGS=-static ./configure && make

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

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

# 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=armv4 -mtune=xscale" ;;
   powerpc) SLKCFLAGS="" ;;
   sparc)   SLKCFLAGS="" ;;
   *)       SLKCFLAGS="" ;;
esac

# Extract source:
cd $TMPBUILD
tar xvvf $CWD/$PKGNAM-$VERSION.tar.bz2
cd $PKGNAM-$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 if something
# still looks there, and allows a smoother transition towards using
# /etc/modprobe.d/ instead.
zcat $CWD/modprobe.favor.etc.modprobe.d.diff.gz | patch -p1 --verbose || exit 1

# Exclude *.{old,new,bak} files (and a few others that obviously aren't
# valid for this... 
zcat $CWD/modprobe.ignore_some_suffixes.diff.gz | patch -p1 --verbose || exit 1

# We get (pointless) errors in early stages of the boot if modprobe checks for
# a mounted /sys before it actually gets mounted:
zcat $CWD/modprobe.no_sys_check.diff.gz | patch -p1 --verbose || exit 1

# Configure:
./configure \
   --prefix=/ \
   --enable-zlib \
   --bindir=/sbin \
   --docdir=/usr/doc/module-init-tools-$VERSION \
   --mandir=/usr/man \
   --build=$ARCH-slackware-linux || failconfig
#   --host=$ARCH-slackware-linux \
#   --target=$ARCH-slackware-linux || failconfig

# Build:
make $NUMJOBS || failmake

# Install into package:
make install DESTDIR=$PKG
# We don't need this
rm -f $PKG/sbin/generate-modprobe.conf

# Links lsmod into /bin
mkdir -p $PKG/bin ; ( cd $PKG/bin ; ln -sf /sbin/lsmod . )

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

mkdir -p $PKG/etc/modprobe.d/
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.
# Instead, create files in the /etc/modprobe.d/ directory
# containing modprobe options.
#
# For more information, see "man modprobe.conf".
EOF

# Install docs:
mkdir -p $PKG/usr/doc/$PKGNAM-$VERSION
# Most of these are out of date
cp -a \
  AUTHORS CODING COPYING FAQ HACKING INSTALL NEWS README TODO \
  $PKG/usr/doc/module-init-tools-$VERSION

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

# 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
