#!/bin/bash

# nfs-utils.SlackBuild
# Heavily based on the original Slackware build scripts &
# Slack390's prototype script by Mark Post.
# Modified by Stuart Winter <mozes@slackware.com>
# 26-Sep-2004

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

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

# 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.xz
cd $PKGNAM-$VERSION || exit 1
slackhousekeeping

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

# Add startup script:
# Off by default, unless there's a previous version that's turned on:
install -vpm644 $CWD/rc.nfsd $PKG/etc/rc.d/rc.nfsd.new

# Apply patches from Slackware x86 tree:
zcat $CWD/nfs-utils.lwrap.needs.lnsl.diff.gz | patch -p1 --verbose || exit 1
# Fix problems on machines without IPv6:
zcat $CWD/ignore_unsupported_address_types_in_nfssvc_setfds.diff.gz | patch -p1 -l --verbose || exit 1

# Apply test patches:
# Taken from http://pkgs.fedoraproject.org/cgit/nfs-utils.git/
# git clone git://pkgs.fedoraproject.org/nfs-utils.git
# These were applied when I was investigating an issue with nfs-utils.. I forgot what
# the problem was but it wasn't fixed by any of these patches, and I never removed them.
#for i in \
# nfs-utils-1.3.4-rc2.patch \
# nfs-utils-1.3.3-netgroup-fix.patch \
# nfs-utils-1.2.1-exp-subtree-warn-off.patch \
# nfs-utils-1.2.3-sm-notify-res_init.patch \
# nfs-utils-1.2.5-idmap-errmsg.patch \
# nfs-utils-1.3.2-systemd-gssargs.patch \
# ; do
#   auto_apply_patch $PORTCWD/sources/${i}.xz || exit 1
#done

# No NFSv4 yet -- it requires additional libraries.
# Configure:
CFLAGS="$SLKCFLAGS" \
./configure \
   --prefix=/usr \
   --mandir=/usr/man \
   --with-statedir=/var/lib/nfs \
   --enable-mountconfig \
   --disable-nfsv4 \
   --disable-gss \
   --enable-tirpc \
   --program-prefix= \
   --program-suffix= \
   --build=$ARCH-slackware-linux-gnueabi || failconfig
#
#   --disable-nfsv4 --disable-nfsv41 --disable-gss \

# Build:
make $NUMJOBS || make || failmake

# Install into package:
make install DESTDIR=$PKG || failinstall

# Add nfsmount.conf and man page:
install -vpm644 utils/mount/nfsmount.conf $PKG/etc/nfsmount.conf.new
mkdir -vpm755 $PKG/usr/man/man5
install -vpm644 utils/mount/nfsmount.conf.man $PKG/usr/man/man5/nfsmount.conf.5
# Set default protocol version 3:
sed -i "s/# Defaultvers=4/Defaultvers=3/g" $PKG/etc/nfsmount.conf.new
# Add the vers=3 so that NFS version 3 is actually selected as the default. I have NO IDEA why
# this doesn't break on x86 but it was a last ditch attempt that worked here on ARM:
sed -i '/Defaultvers=3/avers=3' $PKG/etc/nfsmount.conf.new

# These might be in use:
( cd $PKG/var/lib/nfs
  for config_file in etab rmtab state xtab ; do
    mv -fv ${config_file} ${config_file}.new
  done
)

# No NFSv4 yet, so remove these:
rm -fv $PKG/sbin/*nfs4

# This should move to support /usr on NFS:
mkdir -vpm755 $PKG/sbin
mv $PKG/usr/sbin/rpc.statd $PKG/sbin
( cd $PKG/usr/sbin ; ln -sf ../../sbin/rpc.statd . )

# Copy documentation:
mkdir -vpm755 $PKG/usr/doc/nfs-utils-$VERSION/statd
cp -fav \
  AUTHORS COPYING* INSTALL NEWS README* \
  $PKG/usr/doc/nfs-utils-$VERSION
cp -fav \
  utils/statd/COPYING utils/statd/COPYRIGHT utils/statd/TODO \
  $PKG/usr/doc/nfs-utils-$VERSION/statd
changelogliposuction ChangeLog $PKGNAM $VERSION # Trim down a "ChangeLog" file

# 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

# Chown /var/lib/nfs so that rpc.statd runs as rpc:rpc:
chown -R rpc:rpc $PKG/var/lib/nfs

slackdesc       # install slack-desc and doinst.sh
slackmp         # run makepkg -l y -c n

