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

# 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
#
# Copyright 2008-2026  Patrick J. Volkerding, Sebeka, MN, USA
# All rights reserved.
#
# Redistribution and use of this script, with or without modification, is
# permitted provided that the following conditions are met:
#
# 1. Redistributions of this script must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#
#  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ''AS IS'' AND ANY EXPRESS OR IMPLIED
#  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
#  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO
#  EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
#  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
#  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
#  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
#  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
#  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
#  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

# 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 xf $CWD/$PKGNAM-$VERSION.tar.!(*sign|*asc|*sig)
cd $PKGNAM-*/ || failextract
slackhousekeeping

# Apply patches from Slackware x86 tree:
zcat $CWD/nfs-utils.lwrap.needs.lnsl.diff.gz | patch -p1 --verbose || exit 1

#auto_apply_patch $PORTCWD/sources/nfs-utils-2.6.2-nfsrahead.patch || failpatch

# ARM patches:
# https://www.linuxquestions.org/questions/showthread.php?p=6021853#post6021853
#for pf in \
#  patch.imapd.c.gz \
#  patch.nfsd_path.c.gz \
#  patch.xstat.c.gz ; do
#  auto_apply_patch $PORTCWD/sources/patches/$pf || exit 1
#done

# Create package framework:
mkdir -vpm755 $PKG/etc/{rc.d,exports.d}

# Add defaults file:
mkdir -p $PKG/etc/default
install -vpm644 $CWD/nfs.default $PKG/etc/default/nfs.new

# 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

# Add /etc/exports:
install -vpm644 $CWD/exports $PKG/etc/exports.new

# Configure:
CFLAGS="$SLKCFLAGS" \
./configure \
   --prefix=/usr \
   --libdir=/usr/lib${LIBDIRSUFFIX} \
   --mandir=/usr/man \
   --with-statedir=/var/lib/nfs \
   --with-pluginpath=/usr/lib${LIBDIRSUFFIX}/libnfsidmap \
   --disable-static \
   --enable-mountconfig \
   --enable-nfsdcltrack=yes \
   --enable-nfsv4=yes \
   --enable-blkmapd \
   --enable-gss=yes \
   --enable-svcgss=yes \
   --enable-tirpc=yes \
   --program-prefix= \
   --program-suffix= \
   --build=${SLK_ARCH_BUILD} || failconfig
#
#   --disable-nfsv4 --disable-nfsv41 --disable-gss \

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

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

# Move udev rule:
mkdir -vpm755 $PKG/lib
mv -fv $PKG/usr/lib/udev $PKG/lib
rmdir $PKG/usr/lib 2> /dev/null

# Add /etc/idmapd.conf:
install -vpm644 support/nfsidmap/idmapd.conf $PKG/etc/idmapd.conf.new
# Add /etc/idmapd.conf.d directory:
mkdir -vpm755 $PKG/etc/idmapd.conf.d

# 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 ; do
    mv -fv ${config_file} ${config_file}.new
  done )

# 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 . )

# Make recovery directory. The location expected may be found in
# /proc/fs/nfsd/nfsv4recoverydir, so if that file exists we will get the
# location from there. If not, we'll use the usual directory path.
if [ -r /proc/fs/nfsd/nfsv4recoverydir ]; then
  NFSV4RECOVERYDIR="$(cat /proc/fs/nfsd/nfsv4recoverydir)"
else
  NFSV4RECOVERYDIR="/var/lib/nfs/v4recovery"
fi
mkdir -p ${PKG}${NFSV4RECOVERYDIR}

# Create log directory:
mkdir -p $PKG/var/log/nfsd

# Add database directory for nfsdcltrack:
mkdir -vpm755 $PKG/var/lib/nfs/nfsdcltrack

# Copy documentation:
mkdir -vpm755 $PKG/usr/doc/$PKGNAM-$VERSION/{statd,nfsidmap}
cp -fav \
  AUTHORS COPYING* INSTALL NEWS README* \
  $PKG/usr/doc/$PKGNAM-$VERSION
cp -fav \
  utils/statd/COPYING utils/statd/COPYRIGHT utils/statd/TODO \
  $PKG/usr/doc/$PKGNAM-$VERSION/statd
cp -fav \
  support/nfsidmap/{AUTHORS,COPYING,README} \
  $PKG/usr/doc/$PKGNAM-$VERSION/nfsidmap
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

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

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