#!/bin/bash
ulimit -s unlimited
shopt -s extglob

# pciutils.SlackBuild
# by Stuart Winter <mozes@slackware.com> for the Slackware porting Project.
# Heavily 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:
slackset_var_cwds

# Temporary build locations:
export TMPBUILD=$TMP/build-$PKGNAM
export PKG=$TMP/package-$PKGNAM
mkpkgdirs # Delete & re-create temporary directories then cd into $TMPBUILD

# Extract source:
tar xvvf $CWD/$PKGNAM-$VERSION.tar.!(*sign|*asc|*sig)
cd $PKGNAM-$VERSION || exit 1
slackhousekeeping

# Apply patches:
zcat $CWD/pciutils.dontcompress.diff.gz | patch -p1 || exit 1

# Build/install the library and utilities:
make clean
make install install-lib \
  PREFIX=/usr \
  SBINDIR=/sbin \
  LIBDIR=/usr/lib${LIBDIRSUFFIX} \
  MANDIR=/usr/man \
  IDSDIR=/usr/share/hwdata \
  PCI_IDS=pci.ids \
  PCI_COMPRESSED_IDS=0 \
  SHARED=yes \
  OPT="$SLKCFLAGS" \
  DESTDIR=$PKG || exit 1

# Installation fix ups:
chmod 755 $PKG/usr/lib${LIBDIRSUFFIX}/libpci.so.*
# Move libraries, as they might be needed by programs that bring a network
# mounted /usr online:
mkdir $PKG/lib${LIBDIRSUFFIX}
( cd $PKG/usr/lib${LIBDIRSUFFIX}
  for file in lib*.so.?.* ; do
    mv $file ../../lib${LIBDIRSUFFIX}
    ln -sf ../../lib${LIBDIRSUFFIX}/$file .
  done
  cp -a lib*.so.? ../../lib${LIBDIRSUFFIX}
)

# Add a symlink to the old location:
if [ ! -x $PKG/sbin/lspci ]; then
  if [ -x $PKG/usr/bin/lspci ]; then
    ( cd $PKG/sbin ; ln -sf ../usr/bin/lspci . )
  fi
fi

mkdir -p $PKG/usr/sbin
mv $PKG/sbin/update-pciids $PKG/usr/sbin/update-pciids

# Use pci.ids from hwdata package
rm -fv $PKG/usr/share/hwdata/pci.ids
# Remove empty directories:
rmdir $PKG/usr/share/hwdata $PKG/usr/share

# Install documentation:
mkdir -p $PKG/usr/doc/pciutils-$VERSION
cp -fav \
  COPYING* README* TODO \
  $PKG/usr/doc/pciutils-$VERSION
changelogliposuction ChangeLog $PKGNAM $VERSION # Trim down a "ChangeLog" file

# Apply generic Slackware packaging policies:
cd $PKG
slackstripall   # strip all .a archives and all ELFs
slack_delete_lafiles # delete usr/lib{,64}/*.la
#slackstriprpaths     # strip rpaths
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
