#!/bin/sh
CWD=`pwd`
TMP=/tmp
PKG=$TMP/package-pciutils
VERSION=3.0.0
ARCH=alpha
BUILD=1

rm -rf $PKG
mkdir -p $PKG
( cd $TMP
rm -rf pciutils-$VERSION
tar xzf $CWD/pciutils-$VERSION.tar.gz
cd pciutils-$VERSION
chown -R root:root .
find . -perm 666 -exec chmod 644 {} \;
find . -perm 664 -exec chmod 644 {} \;
find . -perm 600 -exec chmod 644 {} \;
find . -perm 444 -exec chmod 644 {} \;
find . -perm 400 -exec chmod 644 {} \;
find . -perm 440 -exec chmod 644 {} \;
find . -perm 777 -exec chmod 755 {} \;
find . -perm 775 -exec chmod 755 {} \;
find . -perm 511 -exec chmod 755 {} \;
find . -perm 711 -exec chmod 755 {} \;
find . -perm 555 -exec chmod 755 {} \;
# Change prefix from /usr/local to /usr:
zcat $CWD/pciutils.prefix.diff.gz | patch -p0 --verbose || exit 1
# Fetch the latest pci.ids:
rm -f pci.ids
sh update-pciids.sh
make || exit
strip lspci setpci
mkdir -p $PKG/sbin $PKG/usr/sbin $PKG/usr/share $PKG/usr/man/man8 $PKG/usr/lib $PKG/usr/include/pci
cat lspci > $PKG/sbin/lspci
cat setpci > $PKG/sbin/setpci
chmod 755 $PKG/sbin/*
cat pci.ids > $PKG/usr/share/pci.ids
zcat $CWD/pciutils.ids.dest.diff.gz | patch -p1 --verbose || exit
cat update-pciids.sh > $PKG/usr/sbin/update-pciids
chmod 755 $PKG/usr/sbin/update-pciids
for file in config.h header.h pci.h types.h ; do
  cp -a lib/$file $PKG/usr/include/pci
done
cat lib/libpci.a > $PKG/usr/lib/libpci.a
gzip -9c lspci.8 > $PKG/usr/man/man8/lspci.8.gz
gzip -9c setpci.8 > $PKG/usr/man/man8/setpci.8.gz
mkdir -p $PKG/usr/doc/pciutils-$VERSION
cp -a README ChangeLog TODO $PKG/usr/doc/pciutils-$VERSION
chmod 644 $PKG/usr/doc/pciutils-$VERSION/*
( cd $PKG
 find . | xargs file | grep "executable" | grep ELF | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null
 find . | xargs file | grep "shared object" | grep ELF | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null
)
mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc
# Build the package:
cd $PKG
makepkg -l y -c n $TMP/pciutils-$VERSION-$ARCH-$BUILD.tgz
) 2>&1 | tee $TMP/pciutils.build.log
