#!/bin/sh
# Set initial variables:
CWD=`pwd`
if [ "$TMP" = "" ]; then
  TMP=/tmp
fi
PKG=$TMP/package-nfs-utils

VERSION=1.0.10
ARCH=${ARCH:-i486}
BUILD=${BUILD:-3}

if [ "$ARCH" = "i386" ]; then
  SLKCFLAGS="-O2 -march=i386 -mcpu=i686"
elif [ "$ARCH" = "i486" ]; then
  SLKCFLAGS="-O2 -march=i486 -mtune=i686"
elif [ "$ARCH" = "s390" ]; then
  SLKCFLAGS="-O2"
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2"
fi

if [ ! -d $TMP ]; then
  mkdir -p $TMP # location to build the source
fi
rm -rf $PKG
mkdir -p $PKG

# Explode the package framework:
cd $PKG
explodepkg $CWD/_nfs-utils.tar.gz

# Add startup script:
cat $CWD/rc.nfsd > $PKG/etc/rc.d/rc.nfsd.new

cd $TMP
rm -rf nfs-utils-$VERSION
tar xjvf $CWD/nfs-utils-$VERSION.tar.bz2
cd nfs-utils-$VERSION

chown -R root:root .
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 {} \;

CFLAGS="$SLKCFLAGS" \
./configure \
  --prefix=/usr \
  --with-statedir=/var/lib/nfs \
  --enable-nfsv3 \
  --enable-nfsv4=no \
  --enable-gss=no \
  --enable-secure-statd \
  --program-prefix= \
  --program-suffix= \
  $ARCH-slackware-linux

make || exit 1

strip utils/exportfs/exportfs
cat utils/exportfs/exportfs > $PKG/usr/sbin/exportfs
cat utils/exportfs/exportfs.man | gzip -9c > $PKG/usr/man/man8/exportfs.8.gz
cat utils/exportfs/exports.man | gzip -9c > $PKG/usr/man/man5/exports.5.gz
strip utils/lockd/lockd
cat utils/lockd/lockd > $PKG/sbin/rpc.lockd
cat utils/lockd/lockd.man | gzip -9c > $PKG/usr/man/man8/lockd.8.gz
strip utils/mountd/mountd
cat utils/mountd/mountd > $PKG/usr/sbin/rpc.mountd
cat utils/mountd/mountd.man | gzip -9c > $PKG/usr/man/man8/mountd.8.gz
strip utils/nfsd/nfsd
cat utils/nfsd/nfsd > $PKG/usr/sbin/rpc.nfsd
cat utils/nfsd/nfsd.man | gzip -9c > $PKG/usr/man/man8/nfsd.8.gz
strip utils/nfsstat/nfsstat
cat utils/nfsstat/nfsstat > $PKG/usr/sbin/nfsstat
cat utils/nfsstat/nfsstat.man | gzip -9c > $PKG/usr/man/man8/nfsstat.8.gz
strip utils/nhfsstone/nhfsstone
cat utils/nhfsstone/nhfsgraph > $PKG/usr/sbin/nhfsgraph
cat utils/nhfsstone/nhfsnums > $PKG/usr/sbin/nhfsnums
cat utils/nhfsstone/nhfsrun > $PKG/usr/sbin/nhfsrun
cat utils/nhfsstone/nhfsstone > $PKG/usr/sbin/nhfsstone
cat utils/nhfsstone/nhfsgraph.man | gzip -9c > $PKG/usr/man/man8/nhfsgraph.8.gz
cat utils/nhfsstone/nhfsnums.man | gzip -9c > $PKG/usr/man/man8/nhfsnums.8.gz
cat utils/nhfsstone/nhfsrun.man | gzip -9c > $PKG/usr/man/man8/nhfsrun.8.gz
cat utils/nhfsstone/nhfsstone.man | gzip -9c > $PKG/usr/man/man8/nhfsstone.8.gz

strip utils/showmount/showmount
cat utils/showmount/showmount > $PKG/usr/sbin/showmount
cat utils/showmount/showmount.man | gzip -9c > $PKG/usr/man/man8/showmount.8.gz
strip utils/statd/statd
cat utils/statd/statd > $PKG/sbin/rpc.statd
cat utils/statd/statd.man | gzip -9c > $PKG/usr/man/man8/statd.8.gz

mkdir -p $PKG/usr/doc/nfs-utils-$VERSION/nhfsstone
cp -a utils/nhfsstone/DISCLAIMER utils/nhfsstone/README utils/nhfsstone/README.linux \
  $PKG/usr/doc/nfs-utils-$VERSION/nhfsstone
mkdir -p $PKG/usr/doc/nfs-utils-$VERSION/statd
cp -a \
  utils/statd/COPYING utils/statd/COPYRIGHT utils/statd/TODO \
  $PKG/usr/doc/nfs-utils-$VERSION/statd
cp -a \
  COPYING ChangeLog README \
  $PKG/usr/doc/nfs-utils-$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/nfs-utils-$VERSION-$ARCH-$BUILD.tgz